Android之RecyclerView

作者: 拨云见日aaa | 来源:发表于2019-09-29 07:02 被阅读0次

    一、简介

    recycler是一个更加强大的滚动控件,不仅可以实现ListView还可以实现GridView的功能,还可以实现瀑布流式的滚动效果。还可以通过LayoutManager改变滚动方向。

    二、用法

    首先需要一个适配器,自己写个适配器继承RecyclerView.Adapter类,需要重写三个方法onCreateViewHolder(ViewGroup parent,int viewType),onBindViewHolder(ViewHolder holder,int position),getItemCount方法,还需要创建一个内部类继承RecyclerView.ViewHolder.适配器类建好后,先创建RecyclerView类,然后创建LayoutManager类(有三种:LinearLayoutManager,GridLayoutManager,StaggeredGridLayoutManager),可以通过LayoutManager.setOrientation()改变滚动方向,然后RectclerView.setLayoutManager和setAdapter方法就可以显示出来了

    三、代码示例

    实现ListView效果(垂直方向)

    首先需要添加依赖,最新的Android不需要再添加v7包,而是添加AndroidX的包,如果用的eclipse默认导的v7包也可以用

     implementation 'androidx.appcompat:appcompat:1.1.0'//android studio默认导的包
     implementation 'androidx.recyclerview:recyclerview:1.0.0'//自己导的包
    

    每个Item的布局文件

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="match_parent">
        <ImageView
            android:src="@drawable/tomcat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    </LinearLayout>
    

    自己建适配器

    public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private List<User> list;
        public MyAdapter(List<User> list){
            this.list=list;
        }
    
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
            View view=View.inflate(parent.getContext(),R.layout.adapter_layout,null);
            MyViewHolder holder=new MyViewHolder(view);
            return holder;
        }
    
    
    
    
        public void onBindViewHolder(RecyclerView.ViewHolder holder,int position){
                      MyViewHolder holder1=(MyViewHolder)holder;
                      holder1.tv1.setText("姓名:"+list.get(position).getName());
                      holder1.tv2.setText("年龄:"+list.get(position).getAge()+"");
        }
    
        public int getItemCount(){
            return list.size();
        }
        public class MyViewHolder extends RecyclerView.ViewHolder{
            TextView tv1;
            TextView tv2;
            public MyViewHolder( View itemView) {
                super(itemView);
                tv1=itemView.findViewById(R.id.tv1);
                tv2=itemView.findViewById(R.id.tv2);
            }
        }
    }
    

    activity的代码

    public class MainActivity extends AppCompatActivity {
    RecyclerView rv;
    List<User> list;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initList();
            rv=findViewById(R.id.rv);
            LinearLayoutManager layoutManager=new LinearLayoutManager(this);
            rv.setLayoutManager(layoutManager);
            MyAdapter adapter=new MyAdapter(list);
            rv.setAdapter(adapter);
        }
        public void initList(){
            list=new ArrayList<>();
            for(int i=0;i<20;i++){
                User user=new User();
                user.setName("jty"+i);
                user.setAge(20+i);
                list.add(user);
            }
        }
    }
    

    运行效果


    垂直方向的ListView
    实现ListView效果(水平方向)

    只需改一下activity中的代码

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initList();
            rv=findViewById(R.id.rv);
            LinearLayoutManager layoutManager=new LinearLayoutManager(this);
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);//改一下这里为水平就可以了
            rv.setLayoutManager(layoutManager);
            MyAdapter adapter=new MyAdapter(list);
            rv.setAdapter(adapter);
        }
    

    运行效果


    ListView水平方向
    GridView(垂直方向)

    只需改一下LayoutManager

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initList();
            rv=findViewById(R.id.rv);
            GridLayoutManager layoutManager=new GridLayoutManager(this,3);
            rv.setLayoutManager(layoutManager);
            MyAdapter adapter=new MyAdapter(list);
            rv.setAdapter(adapter);
        }
    

    运行效果


    GridView垂直方向
    GridView(水平方向)

    依旧改一下LayoutManager的方向即可

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initList();
            rv=findViewById(R.id.rv);
            GridLayoutManager layoutManager=new GridLayoutManager(this,3);
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rv.setLayoutManager(layoutManager);
            MyAdapter adapter=new MyAdapter(list);
            rv.setAdapter(adapter);
        }
    

    运行效果


    Grid View水平方向
    实现瀑布流(垂直方向)
     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initList();
            rv=findViewById(R.id.rv);
            StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
            rv.setLayoutManager(layoutManager);
            MyAdapter adapter=new MyAdapter(list);
            rv.setAdapter(adapter);
        }
    

    运行效果


    瀑布流垂直方向
    实现瀑布流(水平方向)
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initList();
            rv=findViewById(R.id.rv);
            StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.HORIZONTAL);
            rv.setLayoutManager(layoutManager);
            MyAdapter adapter=new MyAdapter(list);
            rv.setAdapter(adapter);
        }
    

    运行效果


    瀑布流水平方向

    相关文章

      网友评论

        本文标题:Android之RecyclerView

        本文链接:https://www.haomeiwen.com/subject/hjjvuctx.html