RecycleView得使用之--- BaseQuickAdapter ---加载图片Demo (适用于 单一的图片列表)

   包括: a。 加载列表  b。分割线  c。 数据为空时空布局  d。动画Item   e。条目点击事件。

1. 项目的Gradle 中引入   

//更新超级适配器版本。
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'

2. 布局文件。

  a。 主文件布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
   <android.support.v7.widget.RecyclerView
       android:id="@+id/rel_recyclerview"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
b。 Item的布局文件
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:scaleType="fitXY"
    android:id="@+id/iv_img"
    />

</ImageView>
c。空布局
 
 
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ll_root_empty"
    android:orientation="vertical"
    >
   <TextView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:text="内容为空。。。。。。"
       style="@style/text_"
       />
</LinearLayout>

2. 主类 调用。

public class RecyclerViewActivity extends BaseActivity implements BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.rel_recyclerview)
    RecyclerView mRecyclerView;
    private List<Integer> mImageList = new ArrayList<>();

    @Override
    protected View bindView() {
        View view = LayoutInflater.from(this).inflate(R.layout.activity_recyclerview, null);
        return view;
    }

    @Override
    protected void initData() {
        mImageList.add(R.drawable.books_image_1);
        mImageList.add(R.drawable.books_image_2);
        mImageList.add(R.drawable.books_image_3);
        mImageList.add(R.drawable.books_image_4);
        mImageList.add(R.drawable.books_image_5);
        mImageList.add(R.drawable.books_image_6);
        mImageList.add(R.drawable.books_image_1);
        mImageList.add(R.drawable.books_image_2);
        mImageList.add(R.drawable.books_image_3);
        mImageList.add(R.drawable.books_image_4);
        mImageList.add(R.drawable.books_image_5);
        mImageList.add(R.drawable.books_image_6);

        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        ImageAdapter mImageAdapter = new ImageAdapter(R.layout.item_img, mImageList);
        mRecyclerView.setNestedScrollingEnabled(true);

        AbSpacesItemDecoration decoration = new AbSpacesItemDecoration(4);
        mRecyclerView.addItemDecoration(decoration);

        mRecyclerView.setAdapter(mImageAdapter);

        // 数据为空时布局。
        mImageAdapter.setEmptyView(R.layout.empty_img, mRecyclerView);

        // 自定义动画如此轻松
        mImageAdapter.openLoadAnimation(new BaseAnimation() {
            @Override
            public Animator[] getAnimators(View view) {
                return new Animator[]{
                        ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1),
                        ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1)
                };
            }
        });

        mImageAdapter.setOnItemChildClickListener(this);

//        mImageAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//
//            }
//        });
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        UIUtils.showToast("第" + position + "被点击了");
    }

}
 
 

3. Adapter 类 

public class ImageAdapter extends BaseQuickAdapter <Integer,BaseViewHolder>{

    public ImageAdapter(int layoutResId, @Nullable List<Integer> data) {
        super(layoutResId, data);

    }

    @Override
    protected void convert(BaseViewHolder helper, Integer item) {
        helper.setImageResource(R.id.iv_img,item);
        helper.addOnClickListener(R.id.iv_img);
    }
}
4.工具类 分割线。

扫描二维码关注公众号,回复: 1762953 查看本文章
public class AbSpacesItemDecoration extends RecyclerView.ItemDecoration {

    private int space;

    public AbSpacesItemDecoration(int space) {
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.left = space;
        outRect.right = space;
        outRect.bottom = space;
        //注释这两行是为了上下间距相同
//        if(parent.getChildAdapterPosition(view)==0){
        outRect.top = space;
//        }
    }
}



猜你喜欢

转载自blog.csdn.net/qq_39792615/article/details/80782056