RecyclerView: control after 5.0, extension optimization, 3. The Adapter of RecyclerView is only responsible for creating the holder and setting the display data to the holder. Step analysis: 1, first add dependent libraries, File - > project stru - > select module, Dependes - > plus - > select recyvlerview package, add the relevant pages created Activity - layout: Create an entity class. Create an entity class. Create an entity class. Create an inner class MyHolder from ViewHolder. Instantiate the controller property b in the internal class, make the main class RecyclePhoneAdapter inherit from the Adapter under RecycleView and add the inner class generic to the adapter mainly to rewrite the 3 methods without forcing the return value type (do not set the generic return value as the parent class). RecyclePhoneAdapter adapter=new RecyclePhoneAdapter(this,list); Use and set the display style (// Set the layout manager rv_phone.setLayoutManager(new LinearLayoutManager(this)); // display rv_phone. SetAdapter (adapter) as ListView;) ps; / / use the system comes with a default line rv_phone. AddItemDecoration (new DividerItemDecoration (this, LinearLayoutManager. VERTICAL)); Example of customizing Adapter code:Copy the code
/** Create a ViewHolder and create a RecycleView for holder. MyHolder> create by Administrator on 2017/2/20. Create an inner class MyHolder from ViewHolder. 2, make the main class RecyclePhoneAdapter inherit from the Adapter under RecycleView and add the internal class to the adapter of the generic * mainly in order to rewrite 3 methods when need not strong return value type (do not set the generic return value as the parent class) * RecyclePhoneAdapter adapter=new RecyclePhoneAdapter(this,list); Use * and set the display style (// Set the layout manager rv_phone.setLayoutManager(new LinearLayoutManager(this)); // display rv_phone. SetAdapter (adapter) as ListView;) ps; / / use the system comes with a default line rv_phone. AddItemDecoration (new DividerItemDecoration (this, LinearLayoutManager. VERTICAL)); */ public class RecyclePhoneAdapter extends RecyclerView.Adapter<RecyclePhoneAdapter.MyHolder> implements View.OnClickListener{ private Context pContext; private List<PhoneEntity> list; private LayoutInflater inflater; private OnItemClick ItemClickListener=null; // Give an instance of the custom interface, Public void setItemClickListener(OnItemClick itemClickListener) {itemClickListener = itemClickListener; Public RecyclePhoneAdapter(Context pContext, List<PhoneEntity> List) {this.pContext = pContext; this.list = list; inflater=LayoutInflater.from(pContext); } @override public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { True uses the parent layout parameters,false uses your own layout parameters View View =inflater.inflate(r.layout.activity_phone_list,parent,false) MyHolder =new MyHolder(view); / / will be treated as a custom layout to MyHolder built-in holder. ItemView. SetOnClickListener (this); Return new MyHolder(view); } @override public void onBindViewHolder(MyHolder, int position) { PhoneEntity phone=list.get(position) cannot be re-instantiated here; holder.phone_name.setText(phone.getName()); holder.img_phone.setImageResource(phone.getImg()); holder.phone_price.setText(phone.getPrice()); holder.phone_counter.setText(phone.getCountNum()); holder.itemView.setTag(position); } @override public int getItemCount() {return list.size(); Override public void onClick(View v) {Override public void onClick(View v) { Add the click event to the current item if(ItemClickListener! =null){ ItemClickListener.onitemclick(Integer.parseInt(v.getTag().toString())); MyHolder extends RecyclerView.ViewHolder{private ImageView; private ImageView img_phone; private TextView phone_name; private TextView phone_price; private TextView phone_counter; public MyHolder(View itemView) { super(itemView); img_phone= (ImageView) itemView.findViewById(R.id.img_phone); phone_name= (TextView) itemView.findViewById(R.id.phone_name); phone_price= (TextView) itemView.findViewById(R.id.phone_price); phone_counter= (TextView) itemView.findViewById(R.id.phone_counter); Public interface OnItemClick{void OnItemClick (int position); public interface OnItemClick{void OnItemClick (int position); }Copy the code