1. 程式人生 > >RecyclerView展示資料

RecyclerView展示資料

匯入依賴:

implementation 'com.android.support:recyclerview-v7:28.0.0'

佈局引用:

<LinearLayout 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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/ceshilist"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</LinearLayout>

介面卡:

package com.example.administrator.recyclerviewdemo;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/*
 * @Comment :list的介面卡
 * @Created :2018/11/5 17:49
 * @Name : YJ
 */public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {

     private Context context;
     private List<String> list;

    public MyAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        MyHolder myHolder=new MyHolder(LayoutInflater.from(context).inflate(R.layout.item,viewGroup,false));
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyHolder myHolder, final int i) {
        myHolder.mText.setText("第"+i+"條資料");
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        private TextView mText;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            mText= itemView.findViewById(R.id.text);
        }
    }

}

程式碼引用:

package com.example.administrator.recyclerviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mCeshiList;
    private MyAdapter myAdapter;
    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCeshiList=findViewById(R.id.ceshilist);
        setList();
    }
    public void setList(){
        //新增資料
        list=new ArrayList<>();
        for (int i=0;i<50;i++){
            list.add("第"+i+"條資料");
        }
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        mCeshiList.setLayoutManager(linearLayoutManager);
        myAdapter=new MyAdapter(this,list);
        mCeshiList.setAdapter(myAdapter);
    }
}