RecyclerView的简单使用

来源:互联网 发布:超级优化主角几个老婆 编辑:程序博客网 时间:2024/05/20 14:43

实现效果:(没找图片,有些丑哈,为了上传动态图我还专门下载了个软件呢)


1.添加依赖

dependencies {    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'}
添加至工程build.gradle文件的dependencies下,点击sync同步

2.布局中添加

RecyclerView的添加和平常的布局添加没什么不同

<?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"    tools:context="com.rongxin.shopping.rongxinshopping.RecyclerActivity">    <android.support.v7.widget.RecyclerView        android:id="@+id/recycler_rec"        android:layout_width="match_parent"        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></LinearLayout>

3.创建Adapter继承RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>,创建一个布局放子控件

注意:后面的<MyRecyclerAdapter.ViewHolder>最好写上,你如果用的BufferKnife自动生成ViewHolder,则先不添加,生成后再修改也可以。

布局my_recycler_layout.xml:(很简单的一个ImageView)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/image_myRec"        android:background="@mipmap/ic_launcher_round"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        /></LinearLayout>
Adapter:(ImageView和ViewHolder由BufferKnife自动生成的,手写把ImageView提出来就行)

紫色是我后来添加上去的,没有这些可以在my_recycler_layout.xml的TextView中设置固定宽高

我想实现的效果是图片为屏幕宽度的1/4,所以在逻辑代码中写的

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {    private Context context;    private int[] imgs;    private LinearLayout.LayoutParams params;//紫色    private int width;//紫色    public MyRecyclerAdapter(int[] imgs, Context context) {        this.imgs = imgs;        this.context = context;        width = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();//紫色        params = new LinearLayout.LayoutParams(width / 4, width / 4);//紫色    }    //创建View    @Override    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view = LayoutInflater.from(context).inflate(R.layout.my_recycler_layout, null, false);        ViewHolder viewHolder = new ViewHolder(view);        return viewHolder;    }    //绑定VIewHolder    @Override    public void onBindViewHolder(ViewHolder holder, int position) {        holder.image.setLayoutParams(params);//紫色        holder.image.setBackgroundResource(imgs[position]);    }    @Override    public int getItemCount() {        return imgs.length;    }    //编写的ViewHolder    static class ViewHolder extends RecyclerView.ViewHolder {        @InjectView(R.id.image_myRec)        ImageView image;        public ViewHolder(View view) {            super(view);            ButterKnife.inject(this, view);        }    }}

4.在逻辑代码中实例化RecyclerView和LayoutManager以及MyRecyclerAdapter

public class RecyclerActivity extends AppCompatActivity {    @InjectView(R.id.recycler_rec)    RecyclerView mRecyclerRec;    private RecyclerView.LayoutManager manager;    private MyRecyclerAdapter recyclerAdapter;    private int[] imgs = {R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_recycler);        ButterKnife.inject(this);        initData();    }    private void initData() {        //参数1:context,参数二:哪种LinearLayout横向还是竖向展示,参数三:数据是否反向展示        manager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);        mRecyclerRec.setLayoutManager(manager);        recyclerAdapter = new MyRecyclerAdapter(imgs,this);        mRecyclerRec.setAdapter(recyclerAdapter);    }}
LayoutManager:

LinearLayoutManager、GridLayoutManager、StaggeredGridLayoutManager,知道写第一个后面几个差不多的。

然后运行就可以了,以上实现的是横向可滑动的效果。





原创粉丝点击