RecyclerView可以用来替代老的ListView, 并提供了更强大的功能和可定制性。本文先简单的演示其基本用法,对比 Android 控件演示 - ListView
Activity 的界面文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
|
行描述文件: item.xml
1 2 3 4 5 6 7 8 9 10 11
| <?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:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
|
主程序文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class MainActivity extends AppCompatActivity {
@BindView(R.id.recyclerView) protected RecyclerView recyclerView;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
List<String> list = new ArrayList<>(); list.add("张三"); list.add("李四"); list.add("王二麻子");
recyclerView.setHasFixedSize(true); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(linearLayoutManager);
MyAdapter myAdapter = new MyAdapter(list); recyclerView.setAdapter(myAdapter); } }
|
对应的 Adapter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<String> nameList;
public MyAdapter(List<String> list) { nameList = list; }
@Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); View view = inflater.inflate(R.layout.item, parent, false); MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; }
@Override public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) { final String name = nameList.get(position); holder.textView.setText(name);
holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { remove(position); } }); }
@Override public int getItemCount() { if (nameList==null) { return 0; } else { return nameList.size(); } }
private void remove(int position) { nameList.remove(position); notifyItemRemoved(position); }
public class MyViewHolder extends RecyclerView.ViewHolder { public TextView textView;
public MyViewHolder(View itemVieww) { super(itemVieww); textView = itemView.findViewById(R.id.textView); } } }
|