• RecyclerView的使用


    基本使用

    效果如下

    MainActivity中的内容

    public class MainActivity extends AppCompatActivity {
        private RecyclerView mRecyclerView;
        private List<String> mData;
        private MySimpleAdapter mAdapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initData();
            initView();
            mAdapter = new MySimpleAdapter(this, mData);
            mRecyclerView.setAdapter(mAdapter);
    
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
            mRecyclerView.setLayoutManager(linearLayoutManager);
        }
    
        private void initView() {
            mRecyclerView = (RecyclerView) findViewById(R.id.id_rec);
    
        }
    
        private void initData() {
            mData = new ArrayList<String>();
            for (int i = 'A'; i <= 'z'; i++) {
                mData.add("" + (char) i);
            }
        }
    }

     Extras:

    设置分割线

    mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

    recyclerView 更新数据时,这样会导致间隔越来越大,
    可以这样使用
            if(recyclerView.getItemDecorationCount()==0)
                recyclerView.addItemDecoration(new DividerItemDecoration(context,DividerItemDecoration.VERTICAL));

    设置其他LayoutManager

    //mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.HORIZONTAL));
    //mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));

     其中RecyclerView的layout在main的layout代码如下

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
    
        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:id="@+id/id_rec"
    
            android:layout_height="match_parent" />
    </RelativeLayout>

    MySimpleAdapter代码如下

    public class MySimpleAdapter extends RecyclerView.Adapter<MyViewHolder> {
        private LayoutInflater mInflater;
        private Context mContext;
        private List<String> mData;
        public MySimpleAdapter(Context context, List<String> data) {
            this.mContext = context;
            this.mData = data;
            mInflater = LayoutInflater.from(mContext);
        }
    
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = mInflater.inflate(R.layout.item_single_textview, parent, false);
            MyViewHolder viewHolder = new MyViewHolder(view);
            return viewHolder;
        }
    
        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.tv.setText(mData.get(position));
        }
    
        @Override
        public int getItemCount() {
            return mData.size();
        }
    }
    
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.id_text);
        }
    }

    item_single_textview.xml内容如下

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#aaaaaa"
        android:layout_height="72dp"
        xmlns:tools="http://schemas.android.com/tools">
    
        <TextView
            android:layout_width="match_parent"
            android:id="@+id/id_text"
            android:gravity="center"
            tools:text="Hello,World"
            android:layout_height="match_parent" />0
    </FrameLayout>
  • 相关阅读:
    hdu 3006 The Number of set(思维+壮压DP)
    Mysql-SQL优化-统计某种类型的个数
    canvas.clipPath canvas.clipRect() 无效的原因
    linux下alias命令具体解释
    使用带粒子效果的 CAEmitterLayer
    Wordpress 建站(一)
    一个有趣的问题:ls -l显示的内容中total究竟是什么?
    (转)奇妙的数据挖掘
    android几个高速打包命令
    hdu3336解读KMP算法的next数组
  • 原文地址:https://www.cnblogs.com/noigel/p/12028176.html
Copyright © 2020-2023  润新知