赞
踩
说来惭愧,在这么久的开发中一直很少使用到横向的列表效果,即使有类似场景也是使用了 ViewPager处理,特此在有时间的基础下给新人写个入门的dome玩玩 ~
Effect ~
众所周知,RecyclerView的使用中我们会设置布局管理者,所以此篇的关键,在于LinearLayoutManager 的设置方向,关键代码如下 ~
LinearLayoutManager layoutManager= new LinearLayoutManager(this);
//这行是关键,设置了布局方向,如未设置HORIZONTAL方向,则默认是垂直状态
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRv.setLayoutManager(hLayoutManager);
越来越懒了,这里我用的适配器是在目前项目中使用的 BaseQuickAdapter ~
build(project)
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
build(app)
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.35'
implementation 'com.android.support:recyclerview-v7:27.1.1'
MainActivity
package nkwl.com.crosswisedemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Toast; import com.chad.library.adapter.base.BaseQuickAdapter; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView mRvH = findViewById(R.id.rv_horizontal); RecyclerView mRvV = findViewById(R.id.rv_vertical); //模拟数据 List dataList = new ArrayList<Integer>(); dataList.add(R.mipmap.scenery); dataList.add(R.mipmap.scenery); dataList.add(R.mipmap.scenery); dataList.add(R.mipmap.scenery); dataList.add(R.mipmap.scenery); dataList.add(R.mipmap.scenery); dataList.add(R.mipmap.scenery); //横向 MyAdapter horizontalAdapter = new MyAdapter(R.layout.item_layout, dataList); LinearLayoutManager hLayoutManager = new LinearLayoutManager(this); hLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRvH.setLayoutManager(hLayoutManager); mRvH.setAdapter(horizontalAdapter); horizontalAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() { @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) { Toast.makeText(MainActivity.this, "横向 - 当前角标:" + position, Toast.LENGTH_SHORT).show(); } }); //竖向 MyAdapter verticalAdapter = new MyAdapter(R.layout.item_layout, dataList); LinearLayoutManager vLayoutManager = new LinearLayoutManager(this); vLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); mRvV.setLayoutManager(vLayoutManager); mRvV.setAdapter(verticalAdapter); verticalAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() { @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) { Toast.makeText(MainActivity.this, "竖向 - 当前角标:" + position, Toast.LENGTH_SHORT).show(); } }); } }
activity_main
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:background="#225" android:layout_width="match_parent" android:id="@+id/rv_horizontal" android:layout_height="wrap_content"/> <android.support.v7.widget.RecyclerView android:background="#255" android:layout_width="match_parent" android:id="@+id/rv_vertical" android:layout_height="match_parent"/> </LinearLayout>
MyAdapter
package nkwl.com.crosswisedemo; import android.support.annotation.Nullable; import com.chad.library.adapter.base.BaseQuickAdapter; import com.chad.library.adapter.base.BaseViewHolder; import java.util.List; /** * @author MrLiu * @date 2019/11/26 * desc */ public class MyAdapter extends BaseQuickAdapter<Integer,BaseViewHolder> { public MyAdapter(int layoutResId, @Nullable List data) { super(layoutResId, data); } @Override protected void convert(BaseViewHolder helper, Integer item) { helper.setImageResource(R.id.item_img,item); } }
item_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_margin="5dp"
android:scaleType="fitXY"
android:id="@+id/item_img"
android:layout_width="130dp"
android:src="@mipmap/scenery"
android:layout_height="130dp" />
</RelativeLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。