Android中关于RecycleView的使用,代替ListView,可以提高效率

我们平时在Android的开发当中,用得最多的应该是ListView了,但是,在Android5.0以后,Google推出了RecycleView这个类。这个类在平时的开发当中,可以显著地提高我们的效率。而且可以滑动。下面我们就来学习一个这个类。其中,cellData类定义了每一个Item中的数据。ViewHolder是我自定义的一个ViewHolder。list_cell.xml定义的是RecycleView中的每一项的试图。我们先来看一下俩个布局文件:
 
  
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
            android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
                    android:id="@+id/tvTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

                    android:id="@+id/tvContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    <ImageView
        android:id="@+id/image"
        android:src="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

content_main中的布局文件:
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="learnrv.lg.com.learnrv.MainActivity"
    tools:showIn="@layout/activity_main">

            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />


不过,在这个Demo中,其实这个主布局文件是没有用上的,没有任何意义。可以忽略。
 
  
CellData中的内容:
 
  
public class CellData {
    private int imageView;
    private String title;
    private String content;
    private Button btn;

    public CellData(String title, String content) {
        this.title=title;
        this.content=content;

    }

    public CellData(String title, String content, int imageView) {
        this(title, content);
        this.imageView=imageView;

    }

    public CellData(String title, String content, int imageView, Button btn) {
        this(title, content, imageView);
        this.btn=btn;
    }

    public int getImageView() {
        return imageView;
    }

    public String getContent() {
        return content;
    }

    public String getTitle() {
        return title;
    }


}

 
  
 
  
 /*定义一个对外公开的接口*/
public  interface  MyItemClickListener{
    public void onItemClick(View v, int position);

}

 
  
 
  
/*这里是自定义的ViewHolder,*/
    /*getPosition()viewHolder的内置的方法*/
public  class ViewHolder extends  RecyclerView.ViewHolder{
    private TextView tvTitle,tvContent;
    private ImageView imageView;
    private Button btn;
    private View root;
    /*考虑了父类的构造器*/
    public ViewHolder(View root) {
        super(root);
        this.root=root;
        tvTitle = (TextView) root.findViewById(R.id.tvTitle);
        tvContent = (TextView) root.findViewById(R.id.tvContent);
        imageView = (ImageView) root.findViewById(R.id.image);
        btn = (Button) root.findViewById(R.id.btnclick);
    }

    public View getRoot() {
        return root;
    }

    public TextView getTvContent() {
        return tvContent;
    }

    public TextView getTvTitle() {
        return tvTitle;
    }

    public ImageView getImageView() {
        return imageView;
    }

    public Button getBtn() {
        return btn;
    }

    public void setImageView(ImageView imageView) {
        this.imageView = imageView;
    }

    public void setTvContent(TextView tvContent) {
        this.tvContent = tvContent;
    }


    public void setTvTitle(TextView tvTitle) {
        this.tvTitle = tvTitle;
    }

    public void setBtn(Button btn) {
        this.btn = btn;
    }
}

 
  
 
  
/*这里是自定义的适配器*/
    /*在这个类中,我们还像ListView中的那样,为RecycleView中的每一项都增加了列表项的点击事件*/
        public class MyAdapter  extends RecyclerView.Adapter {
            private MyItemClickListener myItemClickListener=null;
            private CellData[] data = new CellData[]{new CellData("李果", "这个男孩真不错",R.drawable.ic_launcher), new CellData("新闻", "这个新闻真不错",R.drawable.ic_launcher),new CellData("李果", "这个男孩真不错",R.drawable.ic_launcher), new CellData("新闻", "这个新闻真不错",R.drawable.ic_launcher),new CellData("李果", "这个男孩真不错",R.drawable.ic_launcher), new CellData("新闻", "这个新闻真不错",R.drawable.ic_launcher),new CellData("李果", "这个男孩真不错",R.drawable.ic_launcher), new CellData("新闻", "这个新闻真不错",R.drawable.ic_launcher),new CellData("李果", "这个男孩真不错",R.drawable.ic_launcher), new CellData("新闻", "这个新闻真不错",R.drawable.ic_launcher),new CellData("李果", "这个男孩真不错",R.drawable.ic_launcher), new CellData("新闻", "这个新闻真不错",R.drawable.ic_launcher)};
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                 /*这是一个布局解释器*/
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell, null);
                return new ViewHolder(view);
            }

    /*在这个方法中,可以初始化一些基本组件,这是要注意的*/
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder vh = (ViewHolder) holder;
        CellData cellData = data[position];
        vh.getTvTitle().setText(cellData.getTitle());
        vh.getTvContent().setText(cellData.getContent());
        vh.getImageView().setImageResource(cellData.getImageView());
/*如果在每一个的Item中,我们都增加了一个Button,那么为了监听Button的点击事件,那么我们应该怎么办呢
* 必须要注意的一点是,在布局中,在Button的布局中,我们可以将Buttonfocusable的属性设置为false就可以了*/
        vh.getBtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myItemClickListener != null) {
                    myItemClickListener.onItemClick(v,position);
                    Log.v("Button--------->", "你点击的是" + position + "");
                }
            }
        });
        vh.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myItemClickListener != null) {
                    myItemClickListener.onItemClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.length;
    }

    public void setMyItemClickListener(MyItemClickListener myItemClickListener) {
        this.myItemClickListener = myItemClickListener;
    }
}
 
  
 
  
package learnrv.lg.com.learnrv;
/*Recycle的优点是当数据比较多的时候,那么数据是可以拖动的。
* 而且它的效率比listView的效率要高*/
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;

/*Android中已经使用RecycleView来代替ListView的使用了。这是要注意的*/
public class MainActivity extends AppCompatActivity {
    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        rv = new RecyclerView(this);
        MyAdapter myAdapter = new MyAdapter();
        myAdapter.setMyItemClickListener(new  MyItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Toast.makeText(MainActivity.this, "你点击的是第" + position + "", Toast.LENGTH_SHORT).show();
            }
        });
        /*setLayoutManager有三个布局可以设计*/
        /*瀑布流,网格布局,线性布局*/
//        rv.setLayoutManager(new GridLayoutManager(this,3));
//        rv.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.HORIZONTAL));
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        setContentView(rv);
        rv.setAdapter(myAdapter);
    }
}

 
  
 
  
最后显示的结果为:
 
  
更加详细内容的博客:点击打开链接;

你可能感兴趣的:(Android开发)