RecyclerView瀑布流+点击事件(简易版)

1.首先,导依赖

implementation'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'

2.在包里配置

allprojects {
    repositories {
        google()
        jcenter()
        **maven { url "https://jitpack.io" }**
    }
}

3.设置适配器

//设置数据源
ArrayList results = (ArrayList) showBean.getData();
  	StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        bwieRcy.setLayoutManager(layoutManager);
//设置适配器
        MyAdapter ada=new MyAdapter(R.layout.list,results);
        bwieRcy.setAdapter(ada);

4.适配器

public class MyAdapter extends BaseQuickAdapter {
public MyAdapter(int layoutResId, @Nullable List data) {
    super(layoutResId, data);
}

@Override
protected void convert(BaseViewHolder helper, ShowBean.DataBean item) {
    /*//获取文字
    //获取图片   记得强转一下helper.getView(R.id.image)*/
    helper.setText(R.id.list_tv,item.getNews_title());
    Glide.with(mContext).load(item.getPic_url()).into((ImageView) helper.getView(R.id.list_iv));
}
}

5.布局

//依赖implementation 'com.android.support:design:28.0.0'





6.点击事件(很简单)

//单击
ada.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Toast.makeText(getActivity(),"啦啦啦",+0).show();
                }
            });
 //长按
ada.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                    list.remove(position);
                    ada.notifyDataSetChanged();
                    return false;
                }
            });

下面的是难的!

1.适配器

public class PubuAdapter extends RecyclerView.Adapter {
ArrayList data;
Context context;
public void Setdata(ArrayList data, Context context){

    this.data=data;
    this.context=context;

}
@NonNull
@Override
public SubVihoder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(context).inflate(R.layout.list, null);
    SubVihoder vihoder = new SubVihoder(view);

    return vihoder;
}

@Override
public void onBindViewHolder(@NonNull SubVihoder subVihoder, int i) {
    subVihoder.text.setText(data.get(i).getNews_title());
    Glide.with(context).load(data.get(i).getPic_url()).into(subVihoder.image);
}

@Override
public int getItemCount() {
    return data.size();
}

class SubVihoder extends RecyclerView.ViewHolder{

    private  ImageView image;
    private  TextView text;

    public SubVihoder(@NonNull View itemView) {
        super(itemView);
        image = itemView.findViewById(R.id.list_iv);
        text = itemView.findViewById(R.id.list_tv);
    }
}
}

2.设置适配器

//数据源
    ArrayList data = (ArrayList) jsonBean.getData();
    //设置给RecyclerView
    StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    bwieRcy.setLayoutManager(manager);
    //设置适配器
    PubuAdapter adapter = new PubuAdapter();
    adapter.Setdata(data,getActivity());
    bwieRcy.setAdapter(adapter);

6.接口回调(接口回调)

你可能感兴趣的:(RecyclerView瀑布流+点击事件(简易版))