RecyclerView 多条目加载 和点击事件

 
compile 'com.android.support:recyclerview-v7:25.1.0'
////////////////////////////////////////////////////////////

//如果嵌套 ScrollowView  需要加上他
//recyclerView.setNestedScrollingEnabled(false);



//设置一个线性布局挂历起布局管理器 //HORIZONTAL设置水平方向 垂直方向 false true true反转 倒着显示 false顺序显示
 recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));

//设置一个网格管理器 //Vertical代表显示多少列 //Horizontal代表显示多少行

recyclerView.setLayoutManager(newGridLayoutManager(this,1,GridLayoutManager.HORIZONTAL,false));

//设置分割线recyclerView.addItemDecoration(newDividerItemDecoration(this,DividerItemDecoration.VERTICAL));

//设置适配器MyAdapter adapter = new MyAdapter(this,recylelist); recyclerView.setAdapter(adapter);


adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
    @Override
    public void setOnItemClick(View view, int position) {
        Log.d("ggg","点击");
    }
    @Override
    public void setOnItemLongClick(View view, int position) {
        Log.d("ggg","长按");
    }
});

/////////////////////////////////////////////////////


此多条目 第一种是的一条 第二种是剩下所有条



                                                   //此处可以改成自己的适配器名字
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context context;
    private List<Bean> list;
    private int Viewk_0=0;
    private int Viewk_1=1;
    private int Viewk_2=2;
    private int Viewk_3=3;
    private View view;
    private List<String> lo;


    public MyAdapter(Context context,List<Bean> list){
        this.context = context;
        this.list=list;
    }

    @Override
    public int getItemViewType(int position) {
        if(position*1==0){
            return Viewk_0;
        }else{
            return Viewk_1;
        }
    }

    /** * * @param parent * @param viewType * @return */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      

      if(viewType==Viewk_0){
           view = LayoutInflater.from(context).inflate(R.layout.itembanner,parent,false);
            ViewHolderA viewHolderA = new ViewHolderA(view);
            return viewHolderA;
       }else {
           view = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
            ViewHolderB viewHolderB = new ViewHolderB(view);
            return viewHolderB;
       }

    }
    /*   生命自己的一个事件回调接口*/
  
//如果要将这个接口移到外面去  那么久别用这个接口了  直接在
//onBindViewHolder中写点击逻辑

 public interface OnItemClickListener{

void setOnItemClick(View view,int position);

void setOnItemLongClick(View view,int position);

}

private OnItemClickListener onItemClickListener;

public void setOnItemClickListener(OnItemClickListener onItemClickListener){

this.onItemClickListener = onItemClickListener;

}

@Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

if(holder instanceof ViewHolderA){

}else if(holder instanceof ViewHolderB){

}

if(null != onItemClickListener)

{ /*回调点击事件*/ holder.itemView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

onItemClickListener.setOnItemClick(v,position);

}

});

holder.itemView.setOnLongClickListener(new View.OnLongClickListener(){

@Override

public boolean onLongClick(View v) {

onItemClickListener.setOnItemLongClick(v,position); return true; } }); } }

@Override public int getItemCount() {

return 4; }

class ViewHolderA extends RecyclerView.ViewHolder{

Banner banner;

public ViewHolderA(View itemView) {

super(itemView);

banner=itemView.findViewById(R.id.banner); } }

class ViewHolderB extends RecyclerView.ViewHolder{

TextView text;

TextView text2;

SimpleDraweeView simple;

public ViewHolderB(View itemView) {

super(itemView);

text = itemView.findViewById(R.id.text1);

text2 = itemView.findViewById(R.id.text2);

simple = itemView.findViewById(R.id.simple); }

}}

阅读更多
上一篇retrofit +rxjava简单封装 日志拦截器 应用拦截器
下一篇弹出滑动选择生日的Dialog
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭