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

1.首先,导依赖

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

2.在包里配置

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

3.设置适配器

//设置数据源
ArrayList<ShowBean.DataBean> results = (ArrayList<ShowBean.DataBean>) 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<ShowBean.DataBean,BaseViewHolder> {
public MyAdapter(int layoutResId, @Nullable List<ShowBean.DataBean> 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'
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/bwie_rcy"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

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<PubuAdapter.SubVihoder> {
ArrayList<JsonBean.DataBean> data;
Context context;
public void Setdata(ArrayList<JsonBean.DataBean> 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<JsonBean.DataBean> data = (ArrayList<JsonBean.DataBean>) 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.接口回调(接口回调)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值