RecycleView(样例Phone)

来源:互联网 发布:化学物质数据库 编辑:程序博客网 时间:2024/06/08 03:24
acvitity_phone_list自定义布局模板
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_margin="5dp">    <ImageView        android:src="@drawable/ihone7plus"        android:id="@+id/img_phone"        android:layout_width="150dp"        android:layout_height="150dp"        android:layout_margin="10dp"/>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical">        <TextView            android:id="@+id/phone_name"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="ihone7Plus玫瑰金"            android:textSize="20dp"            android:layout_marginTop="50sp"/>        <TextView            android:id="@+id/phone_price"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="¥7188"            android:textSize="20dp"            android:textColor="#f45959" />        <TextView            android:id="@+id/phone_counter"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="50353条评价"            android:textSize="15dp"            android:textColor="@android:color/darker_gray"/>    </LinearLayout></LinearLayout>
新建实体类PhoneEntity
package com.example.entity;/** * Created by Administrator on 2017/2/16. */public class PhoneEntity {    private int img;    private String name,price,countNum;    public int getImg() {        return img;    }    public String getName() {        return name;    }    public String getPrice() {        return price;    }    public String getCountNum() {        return countNum;    }    public void setImg(int img) {        this.img = img;    }    public void setName(String name) {        this.name = name;    }    public void setPrice(String price) {        this.price = price;    }    public void setCountNum(String countNum) {        this.countNum = countNum;    }}
自定义适配器RecycleViewAdapter
package com.example.adapter;import android.content.Context;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;import com.example.entity.PhoneEntity;import com.example.jump.R;import java.util.List;/**继承RecycleView.Adapter<当前类的泛型.MyHolder>,重写里边三个方法 * Created by Administrator on 2017/2/20. */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;    //给自定义接口一个实例,并提供下边set方法    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使用父布局的布局参数,false使用自己的布局参数        View view=inflater.inflate(R.layout.activity_phone_list,parent,false);//使用自定义布局格式        MyHolder holder=new MyHolder(view);//将自定义的布局传给MyHolder内置处理        holder.itemView.setOnClickListener(this);//为当前item设置点击事件        return new MyHolder(view);    }    @Override    public void onBindViewHolder(MyHolder holder, int position) {        //获取对应位置的数据, 不能再此重新实例化        PhoneEntity phone=list.get(position);        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);//将item位置设置成标签,    }    @Override    public int getItemCount() {        return list.size();    }    //继承自定义接口,重写自定义接口中的方法,给item添加点击事件    @Override    public void onClick(View v) {        //调用自定义接口中的方法,给当前item添加点击事件        if(ItemClickListener!=null){            ItemClickListener.onitemclick(Integer.parseInt(v.getTag().toString()));        }    }    //创建内部类继承于RecycleView.ViewHolder实例化自定义布局中的属性    class MyHolder extends RecyclerView.ViewHolder{        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);        }    }//    自定义接口,通过触发不同位置item的方式,调用接口中的方法,实现点击事件的监听效果    public interface OnItemClick{        void onitemclick(int position);    }}
自定义RecyclePhoneAcvitity.java
package com.example.jump;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.widget.ImageView;import android.widget.Toast;import com.example.adapter.RecyclePhoneAdapter;import com.example.entity.PhoneEntity;import java.util.ArrayList;import java.util.List;public class RecyclerPhoneActivity extends AppCompatActivity {    private RecyclerView rv_phone;    private List<PhoneEntity> list;    private int[] ids=new int[]{R.drawable.ihone7plus,R.drawable.mi5s,R.drawable.leshi2,R.drawable.rongyao8};    private String[] phonenames=new String[]{"苹果7Plus,128G玫瑰金","小米5S全网通版,32G亚光金","乐视2原力金32GB内存","荣耀8全网通4GB32G珠光白"};    private String[] phoneprice=new String[]{"¥7188","¥1899","¥1099","¥2229"};    private String[] phonecounter=new String[]{"50353条评价","20353条评价","34553条评价","65053条评价"};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_recycler_phone);        rv_phone= (RecyclerView) findViewById(R.id.rv_phone);        intiPhone();    }    public void intiPhone(){        list=new ArrayList<>();        for (int i = 0; i < ids.length; i++) {            PhoneEntity phone=new PhoneEntity();            phone.setImg(ids[i]);            phone.setName(phonenames[i]);            phone.setPrice(phoneprice[i]);            phone.setCountNum(phonecounter[i]);            list.add(phone);        }        RecyclePhoneAdapter adapter=new RecyclePhoneAdapter(this,list);        adapter.setItemClickListener(new RecyclePhoneAdapter.OnItemClick() {            @Override            public void onitemclick(int position) {                Toast.makeText(RecyclerPhoneActivity.this,"点击:"+list.get(position).getName(),Toast.LENGTH_SHORT).show();            }        });        //设置布局管理器        rv_phone.setLayoutManager(new LinearLayoutManager(this));//像ListView一样展示        rv_phone.setAdapter(adapter);        //使用系统自带默认分割线        rv_phone.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.VERTICAL))    }}
RecyclePhoneAcvitity.java对应的布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_recycler_phone"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.jump.RecyclerPhoneActivity">    <android.support.v7.widget.RecyclerView        android:background="@android:color/holo_green_dark"        android:id="@+id/rv_phone"        android:layout_width="match_parent"        android:layout_height="wrap_content">    </android.support.v7.widget.RecyclerView></RelativeLayout>
0 0
原创粉丝点击