recyclerview怎么使用

這篇文章主要講解了“recyclerview怎么使用”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“recyclerview怎么使用”吧!

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比邵陽(yáng)網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式邵陽(yáng)網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋邵陽(yáng)地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。

基本使用

1、xml布局文件中個(gè)使用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>

2、activity中使用代碼

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。

3、自定義adapter,item監(jiān)聽(tīng)可以放在ViewHolder中實(shí)現(xiàn)

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListHolder> {
    List<Sample> sampleList;
    Context context;

    /**
     * 1、定義Adapter首先需要一個(gè)ViewHolder
     * 2、實(shí)現(xiàn)item監(jiān)聽(tīng)可以放在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è)布局傳入,再通過(guò)ViewHolder把這個(gè)布局里的每個(gè)控件設(shè)置進(jìn)來(lái)
     */
    @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)擊事件,是通過(guò)自定義ViewHolder實(shí)現(xiàn)的監(jiān)聽(tīng)事件接口,對(duì)比看的話這是一種比較優(yōu)雅的實(shí)現(xiàn)方案。 

感謝各位的閱讀,以上就是“recyclerview怎么使用”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)recyclerview怎么使用這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

名稱欄目:recyclerview怎么使用
文章轉(zhuǎn)載:http://muchs.cn/article40/jejpho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、面包屑導(dǎo)航、建站公司、全網(wǎng)營(yíng)銷推廣域名注冊(cè)、動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)