前言

简单记录一下RecyclerView的使用,记录于此方便自己查阅。

正文

这里只是简单记录一下。

xml

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/list_rv_view"
    android:layout_width="1000dp"
    android:layout_height="600dp"
    android:layout_weight="1" />

java

ItemAdapter

适配器

public class MediaAdapter extends RecyclerView.Adapter<MediaAdapter.ViewHolder> {
    private List<FileInfo> mList = new ArrayList<>();
    
    @NonNull
    @Override
    public MediaAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(MediaApp.getContext()).inflate(R.layout.layout_item_media, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MediaAdapter.ViewHolder holder, int position) {
        if (getItemCount() > 0) {
            //略
        }
    }

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

    @SuppressLint("NotifyDataSetChanged")
    public void updateList(List<FileInfo> list) {
        if (list == null || list.isEmpty()) {
            mList.clear();
        } else {
            mList = new ArrayList<>(list);
        }
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView mIvIcon;
        private final TextView mTvName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mIvIcon = itemView.findViewById(R.id.item_iv_icon);
            mTvName = itemView.findViewById(R.id.item_tv_name);
        }
    }
}

LinearLayoutManager

private RecyclerView mRecyclerView;
private LinearLayoutManager mLinearLayoutManager;

mRecyclerView = mRootView.findViewById(R.id.list_rv_view);

//线性垂直显示
mLinearLayoutManager = new LinearLayoutManager(MusicApp.getContext());
mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.addItemDecoration(new DividerItemDecoration(MusicApp.getContext(), DividerItemDecoration.VERTICAL));
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setAdapter(mItemAdapter);

GridLayoutManager

 mMediaAdapter = new MediaAdapter();
 //一行6个
 mRecyclerView.setLayoutManager(new GridLayoutManager(MediaApp.getContext(), 6));
 mRecyclerView.setAdapter(mItemAdapter);
指定间距

需要重写RecyclerView.ItemDecoration

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
    private final int left, top, right, bottom;

    public SpacesItemDecoration(int left, int top, int right, int bottom) {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }

    @Override
    public void getItemOffsets(Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        //看需求修改
        outRect.bottom = bottom;
        outRect.left = left;
        outRect.right = right;
        outRect.top = top;
    }
}

设置一下即可。

mRecyclerView.addItemDecoration(new SpacesItemDecoration(30, 30, 30, 30));

滚动指定位置

//position是指定位置
//方式一
mRecyclerView.scrollToPosition(position);
//方式二
mLinearLayoutManager.scrollToPosition(position);
//方式三
//position是指定位置 ,y是固定高度位置
mLinearLayoutManager.scrollToPositionWithOffset(position, y);

参考文章

  1. 个人常用的ListView方法简介

相关文章

暂无评论

none
暂无评论...