美文网首页
RecyclerView使用指南

RecyclerView使用指南

作者: 西城鹿杖客 | 来源:发表于2018-03-20 00:53 被阅读5次

    是用来替代ListView和Gallery的

    可以实现列表和九宫格的横向和纵向的滑动以及瀑布流效果

    要想使用就要添加v7包的支持

    需要联合使用Adapter和ViewHolder和LayoutManager

    纵向和横向滑动效果的实现

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.hanhan.myapplication.MainActivity">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
    
    </LinearLayout>
    
    public class MainActivity extends AppCompatActivity {
        /**
         * rv相关联的类:
         * 1.布局管理器,用来设置rv的显示风格,如列表,网格,瀑布流,XXXLayoutManager
         * 2.适配器,Adapter
         * 3.ViewHolder
         */
    
        @BindView(R.id.rv)
        RecyclerView mRv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
    
            //1.设置布局管理器
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);//这个就是实现横向滑动
            mRv.setLayoutManager(linearLayoutManager);
    
            //2.设置适配器
            mRv.setAdapter(new MyRvAdapter());
        }
    
        private class MyRvAdapter extends RecyclerView.Adapter {
            //创建ViewHolder
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, parent, false);
                return new MyRvViewHolder(view);
            }
    
            //对ViewHolder里面的控件进行赋值
            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                MyRvViewHolder myRvViewHolder = (MyRvViewHolder) holder;
                myRvViewHolder.setData(position);
            }
    
            //RV的条目数量
            @Override
            public int getItemCount() {
                return 50;
            }
        }
    
        private class MyRvViewHolder extends RecyclerView.ViewHolder {
    
            private TextView mTextView;
            private int position;
    
            public MyRvViewHolder(View itemView) {
                super(itemView);
                mTextView = itemView.findViewById(R.id.tv_test);
            }
    
            public void setData(int position) {
                this.position = position;
                mTextView.setText("哈哈哈哈哈" + position);
            }
        }
    }
    
    <?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="wrap_content"
                  android:layout_margin="5dp"
                  android:background="#f00"
                  android:orientation="vertical">
        <!--条目的布局-->
        <TextView
            android:id="@+id/tv_test"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="hello"/>
    
    </LinearLayout>
    

    相关文章

      网友评论

          本文标题:RecyclerView使用指南

          本文链接:https://www.haomeiwen.com/subject/dlomqftx.html