码迷,mamicode.com
首页 > 其他好文 > 详细

67.Recycler View

时间:2017-09-22 01:01:55      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:adapter   line   eve   def   setimage   orm   imageview   name   cardview   

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

标签:adapter   line   eve   def   setimage   orm   imageview   name   cardview   

原文地址:http://www.cnblogs.com/L1nus/p/7572007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!