您好,登錄后才能下訂單哦!
這篇文章主要講解了“recyclerview怎么使用”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“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、設(shè)置adapter recyclerView.setAdapter(listAdapter); // 2、設(shè)置布局 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使用的關(guān)鍵是設(shè)置好對(duì)應(yīng)的adapter。
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListHolder> { List<Sample> sampleList; Context context; /** * 1、定義Adapter首先需要一個(gè)ViewHolder * 2、實(shí)現(xiàn)item監(jiān)聽可以放在ViewHolder中實(shí)現(xiàn) */ 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(); } /** * 給每個(gè)控件設(shè)置對(duì)應(yīng)的數(shù)據(jù) */ public void setData(Sample sample) { tvContent.setText(sample.getTvContent()); tvName.setText(sample.getTvName()); ivIcon.setImageResource(sample.getIcon()); } } /** * 構(gòu)造函數(shù) */ public ListAdapter(List<Sample> sampleList, Context context) { this.sampleList = sampleList; this.context = context; } /** * ViewHolder 首先用inflate方法解析布局,把整個(gè)布局傳入,再通過ViewHolder把這個(gè)布局里的每個(gè)控件設(shè)置進(jìn)來 */ @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中實(shí)現(xiàn)item的點(diǎn)擊事件,是通過自定義ViewHolder實(shí)現(xiàn)的監(jiān)聽事件接口,對(duì)比看的話這是一種比較優(yōu)雅的實(shí)現(xiàn)方案。
感謝各位的閱讀,以上就是“recyclerview怎么使用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)recyclerview怎么使用這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。