RecyclerView 小记(一)

RecyclerView 的优点

RecyclerView 不但可以通过设置 LayoutManager 来快速实现 ListView、GridView和瀑布流等效果,而且可以设置横向和纵向显示,并且添加动画效果也很常简单。

实现步骤

  1. 添加包的引用
implementation 'com.android.support:recyclerview-v7:27.0.2'

// 我用了 CardView 所以也加上
implementation 'com.android.support:cardview-v7:27.0.2'
  1. 在 XML 里添加 RecyclerView
    我的 activity_main.xml 里把屏幕分成上下两部分,上半部分欲做竖向滑动,下半部分欲做横向滑动。



    

    
    

    
    




  1. 在 Activity 中进行设置
    private void findView() {
        mRecyclerViewVertical = (RecyclerView) findViewById(R.id.recycler_view_vertical);
        mRecyclerViewHorizontal = (RecyclerView) findViewById(R.id.recycler_view_horizontal);
    }

    private void initRecyclerView() {
        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerViewVertical.setHasFixedSize(true);
        mRecyclerViewHorizontal.setHasFixedSize(true);

        // use a linear layout manager - VERTICAL
        mLinearLayoutManagerVertical = new LinearLayoutManager(this);
        mLinearLayoutManagerVertical.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerViewLayoutManagerVertical = mLinearLayoutManagerVertical;
        mRecyclerViewVertical.setLayoutManager(mRecyclerViewLayoutManagerVertical);

        // use a linear layout manager - HORIZONTAL
        mLinearLayoutManagerHorizontal = new LinearLayoutManager(this);
        mLinearLayoutManagerHorizontal.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerViewLayoutManagerHorizontal = mLinearLayoutManagerHorizontal;
        mRecyclerViewHorizontal.setLayoutManager(mRecyclerViewLayoutManagerHorizontal);

        // specify an adapter (see also next example)
        mRecyclerViewVertical.setAdapter(new BasicRecyclerViewAdapter(mDataset, VIEW_TYPE_ORIENTATION_VERTICAL));
        mRecyclerViewHorizontal.setAdapter(new BasicRecyclerViewAdapter(mDataset, VIEW_TYPE_ORIENTATION_HORIZONTAL));
    }
  1. 重点是实现 Adapter
    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class BasicViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        private TextView textView;

        public BasicViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.text_view_info);
        }
    }

    // Create new views (invoked by the layout manager)
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;

        // create a new view
        if (VIEW_TYPE_ORIENTATION_VERTICAL == mOrientation) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_vertical, parent, false);
        } else if (VIEW_TYPE_ORIENTATION_HORIZONTAL == mOrientation) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        }

        // set the view's size, margins, paddings and layout parameters
        //...

        return new BasicViewHolder(view);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        if (holder instanceof BasicViewHolder) {
            ((BasicViewHolder) holder).textView.setText(mDataset.get(position));
        } else {
            Log.d("HHHH", "onBindViewHolder - holder instanceof");
        }
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }

完整代码

RecyclerViewDemo

延伸阅读

RecyclerView | Android Developers
Recycler View | Android Developers
Creating Lists and Cards | Android Developers

你可能感兴趣的:(RecyclerView 小记(一))