您好,登錄后才能下訂單哦!
這篇文章主要講解了“recyclerview怎么使用”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“recyclerview怎么使用”吧!
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".activity.RecyclerViewActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
public class RecyclerViewActivity extends AppCompatActivity { RecyclerView recyclerView; List<Sample> sampleList = new ArrayList<>(); int listSize = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycler_view); recyclerView = findViewById(R.id.recyclerView); initSampleList(); ListAdapter listAdapter = new ListAdapter(sampleList, this); // 垂直線性布局 // LinearLayoutManager layoutManager = new LinearLayoutManager(null); // 瀑布流布局 StaggeredGridLayoutManager staggeredGridManager = new StaggeredGridLayoutManager(2, 1); // 1、設置adapter recyclerView.setAdapter(listAdapter); // 2、設置布局 recyclerView.setLayoutManager(staggeredGridManager); } private void initSampleList() { for (int i = 0; i < listSize; i++) { Sample sample = new Sample(); sample.setIcon(R.drawable.ball); sample.setTvName("ball: " + i); sample.setTvContent("ball price: " + i * 100); sampleList.add(sample); Sample sample1 = new Sample(); sample1.setIcon(R.drawable.tao); sample1.setTvName("tao: " + i); sample1.setTvContent("tao price: " + i * 100); sampleList.add(sample1); Sample sample2 = new Sample(); sample2.setIcon(R.drawable.apple); sample2.setTvName("apple: " + i); sample2.setTvContent("apple price: " + i * 100); sampleList.add(sample2); } } }
可以看到,recyclerview使用的關鍵是設置好對應的adapter。
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListHolder> { List<Sample> sampleList; Context context; /** * 1、定義Adapter首先需要一個ViewHolder * 2、實現item監聽可以放在ViewHolder中實現 */ static class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView tvName; TextView tvContent; ImageView ivIcon; Context context; public ListHolder(@NonNull View itemView, Context context) { super(itemView); tvName = itemView.findViewById(R.id.list_name); tvContent = itemView.findViewById(R.id.list_content); ivIcon = itemView.findViewById(R.id.list_icon); itemView.setOnClickListener(this); this.context = context; } @Override public void onClick(View v) { Toast.makeText(context, getAdapterPosition() + "", Toast.LENGTH_SHORT).show(); } /** * 給每個控件設置對應的數據 */ public void setData(Sample sample) { tvContent.setText(sample.getTvContent()); tvName.setText(sample.getTvName()); ivIcon.setImageResource(sample.getIcon()); } } /** * 構造函數 */ public ListAdapter(List<Sample> sampleList, Context context) { this.sampleList = sampleList; this.context = context; } /** * ViewHolder 首先用inflate方法解析布局,把整個布局傳入,再通過ViewHolder把這個布局里的每個控件設置進來 */ @NonNull @Override public ListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View sampler = View.inflate(parent.getContext(), R.layout.layout_list,null); return new ListHolder(sampler, context); } @Override public void onBindViewHolder(@NonNull ListHolder holder, int position) { holder.setData(sampleList.get(position)); } @Override public int getItemCount() { if (sampleList != null) { return sampleList.size(); } return 0; } }
adapter中實現item的點擊事件,是通過自定義ViewHolder實現的監聽事件接口,對比看的話這是一種比較優雅的實現方案。
感謝各位的閱讀,以上就是“recyclerview怎么使用”的內容了,經過本文的學習后,相信大家對recyclerview怎么使用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。