关于recyclerview的横向布局的宽度以及item的点击事件的添加

  • Post author:
  • Post category:其他


package adapter;

import android.annotation.TargetApi;
import android.os.Build;
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.ProgressBar;
import android.widget.TextView;

import com.example.lxy.yiyuangou.R;

import java.util.ArrayList;
import java.util.List;

import vo.HotProduct;

/**
 * Created by 栾先生 on 2016/5/11.
 */
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ItemViewHolder> {
    private int image[] = {R.drawable.home_btn_buy, R.drawable.home_btn_new, R.drawable.home_btn_show, R.drawable.home_btn_show, R.drawable.home_btn_show, R.drawable.home_btn_show};
	//HotProduct是一个JavaBean类
    private List<HotProduct> datas;
    private OnItemClickListener mOnItemClickListener;


    public ListAdapter() {
        datas = new ArrayList<>();
        for (int i = 1; i <= 6; i++) {
            HotProduct hp = new HotProduct("价值:500" + i, "500", "1000", "500");
            datas.add(hp);
        }
    }


    /**
     * 自定义viewholder
     */
    class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView iv;
        ProgressBar pb;
        TextView money, yi, zong, sheng;

        public ItemViewHolder(View v) {
            super(v);
            iv = (ImageView) v.findViewById(R.id.ren_iamge);
            yi = (TextView) v.findViewById(R.id.ren_yican);
            zong = (TextView) v.findViewById(R.id.ren_zong);
            sheng = (TextView) v.findViewById(R.id.ren_sheng);
            money = (TextView) v.findViewById(R.id.ren_money);
            pb = (ProgressBar) v.findViewById(R.id.ren_pb);

        }
    }


    @Override
    public ListAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //这么添加布局,item中的marchparent将会失效
//        ItemViewHolder v = new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.renqi_item, null));
       
        // 这么做为自适应的item  随着item的大小变化而变化
        ItemViewHolder v = new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.renqi_item, parent, false));

        return v;
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
    @Override
    public void onBindViewHolder(final ListAdapter.ItemViewHolder holder, int position) {

        ItemViewHolder vh = (ItemViewHolder) holder;
        vh.iv.setImageResource(image[position]);
        vh.money.setText(datas.get(position).getMoney());
        vh.yi.setText(datas.get(position).getYicanyu());
        vh.zong.setText(datas.get(position).getZong());
        vh.sheng.setText(datas.get(position).getSheng());



        if (mOnItemClickListener != null) {
            /**
             * 这里加了判断,itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建,如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法,都会创建两个监听事件对象,增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
//                                 Log.e("ListAdapter", "setOnClickListener");
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getPosition();
                        mOnItemClickListener.onItemClick(v, pos);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = holder.getPosition();
                        mOnItemClickListener.onItemLongClick(v, pos);
                        return true;
                    }
                });
            }
        }


    }

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


    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    /**
     * 处理item的点击事件和长按事件
     */
    public interface OnItemClickListener {
         public void onItemClick(View view, int position);

        public  void onItemLongClick(View view, int position);
    }
}



版权声明:本文为luanxuye原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。