67.Recycler View

Posted L1nus

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了67.Recycler View相关的知识,希望对你有一定的参考价值。

android RecycleView is more advanced version of ListView with improved performance and other benifits.Using RecyclerView and CardView together,both lists and grids can be created very easily. Here is the complete information anbout RecycleView and other examples.

First , File -> progect Structure -> app dependicies -> add -> cardView

Second , add Recycle View(com.android.support:v7:25.0.0)

Third , new java class ,named Data.

  public class Data{

    private int ImageId;

    private String description;

    

    public Data(){

    }

    public Data(int ImageId, String description){

      this.ImageId = imageId;

      this.decription = description

    }

    alt+insert-> getter & setter

  }

Third , new item_layout.xml  with root element android.support.v7.widget.CardView , then add LinearLayout in CardView with vertical orientation

Fourth , add ImageView and Description in the LinearLayout.use default image and text to make the layout clearly for developer to design.

Fifth , Add android.support.v7.widget.RecyclerView  in main_activity.xml layout file,named recycle ,and scrollbars = "vertical"

Six , new java Class RecycleViewAdapter

  class RecycleViewHolder extends RecycleView.ViewHolder{

    public ImageView imageview;

    public TextView textView;

    public RecycleViewHolder(View itemView){

      super(itemView);

    imageview = itemView.findviewById(R.id.imageView);

    textView = itemView.findviewById(R.id.textView);

    }

  }

  public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewHolder>{

   private List <Data> listData = new ArrayList<Data>();

     public RecyclerViewAdapter(List<Data> ListData){

    this.listData = listData;    

    }

    

  @override

  onCreateViewHolder

  LayoutInflater inflater = LayoutInflater.from(parent.getContext());

  View itemView = inflater.inflate(R.layout.item,parent,false);

  return new RecyclerViewHolder(itemView);

  @override

  onBindViewHolder

  holder.imageView.setImageResource(listData.get(positon).getImageId());

  holder.textView.setText(listData).get(position).getText);

  @override

  getItemCount

  

  }

Seven rewritet  MainActivity ,

    Private RecyclerView recyclerView;

    private RecycleViewAdapter adapter;

    private RecycleView.LayoutManager layoutManager;

    

    privater List<Data> listData = new ArrayList<Data> ();

    

    recycleView.setHasFixedSize(true);

    layoutManger = new LinearLayoutManager(this);

    recycleView.setLayoutManager(layoutManager)

    adapter = new RecycleViewAdapter(listData);

    recycleView.setAdapter(adapter);

 

更加详细的用法,推荐一篇很不错的博文:http://blog.csdn.net/lmj623565791/article/details/45059587

以上是关于67.Recycler View的主要内容,如果未能解决你的问题,请参考以下文章