android原生開發recyclerview基礎例項

^ _ ^發表於2020-12-09

目錄
一、建立佈局檔案
二、建立recyclerview檔案
三、縮寫java程式碼

一、activity_recyclerview_test.xml檔案

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/iv_icon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/img_test"/>
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_marginLeft="10dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/holo_blue_dark"
        android:text="姓名"
        android:textSize="20sp"/>

    <TextView
        android:id="@+id/tv_introduces"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/holo_blue_dark"
        android:text="介紹"/>

</LinearLayout>

二、activity_recycler.xml檔案

<?xml version="1.0" encoding="utf-8"?>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rec"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

三、RecylerViewActivity.java檔案程式碼

package com.example;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demo.R;

public class RecylerViewActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private HomeAdapter mAdapter;
private String[] names = {“小樣”,“小紅”,“小黃”};
private int[] icons = {R.mipmap.a,R.mipmap.b,R.mipmap.c};
private String[] introduces = {“小紅是個勤奮的人”,“小紅非常愛乾淨”,“小黃特別好吃懶做”};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycler);
    mRecyclerView = findViewById(R.id.rec);
    //設定佈局方式
    mRecyclerView.setLayoutManager(new LinearLayoutManager(RecylerViewActivity.this));
    mAdapter = new HomeAdapter();
    mRecyclerView.setAdapter(mAdapter);
}

class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder>{


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(RecylerViewActivity.this).inflate(R.layout.activity_recyclerview_test,parent,false));
        return holder;
    }

    @Override
    //繫結資料到單元格佈局的控制元件上
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.tv_name.setText(names[position]);
        holder.tv_introduces.setText(introduces[position]);
        holder.iv_icon.setImageResource(icons[position]);
    }

    @Override
    public int getItemCount() {
        return names.length;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tv_name;
        TextView tv_introduces;
        ImageView iv_icon;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_introduces = itemView.findViewById(R.id.tv_introduces);
            iv_icon = itemView.findViewById(R.id.iv_icon);

        }
    }
}

}

相關文章