基本使用
效果如下
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>