1. 程式人生 > >Android : Recyclerview的使用以及介面回撥

Android : Recyclerview的使用以及介面回撥

先上效果圖
Recyclerview是沒有點選事件的,所以介面回撥就是寫的點選事件

在這裡插入圖片描述
在這裡插入圖片描述
需要用到的jar包

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

android.support.v7.widget.RecyclerView用這個來代替ListView

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_height="match_parent"
    tools:context=".MainActivity">

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

</android.support.constraint.ConstraintLayout>

介面卡需要用到的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/TV_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="麻辣隔壁" />
</LinearLayout>

主要程式碼,也是介面卡裡面的程式碼

package com.bwie.administrator.jiekouhuidiaolianxi1.adapter;

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

import com.bwie.administrator.jiekouhuidiaolianxi1.R;

import java.util.ArrayList;
//此處要繼承RecyclerView裡面的Adapter類
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private ArrayList<String> mdatas;

    public MyAdapter(Context context, ArrayList<String> mdatas) {
        this.context = context;
        this.mdatas = mdatas;
    }
    //建立一個holder類
    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView MyRec;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            //獲取xml中的資料進行優化
            MyRec = itemView.findViewById(R.id.TV_name);
        }
    }

    @NonNull
    @Override//此處相當於BaseAdapter的getView
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = View.inflate(context, R.layout.item, null);
        v.setOnClickListener(this);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }
    @Override//
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        //獲取點選頁面的下標索引  相當於baseAdapter中給holder類賦值
        viewHolder.MyRec.setText(mdatas.get(i));
        viewHolder.itemView.setTag(i);
    }
    @Override//相當於BaseAdapter的getCount
    public int getItemCount() {
        return mdatas.size();
    }
    //定義一個點選事件的介面
    public interface OnItemClick {
        void onItemClick(View view, int position);
    }

    private OnItemClick onItemClick;

    //實現那個介面
    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }
    //進行判斷
    @Override
    public void onClick(View v) {
        if (onItemClick != null) {
            //此處取出值
            onItemClick.onItemClick(v, (int) v.getTag());
        }
    }
}

mainActivity裡面的程式碼

package com.bwie.administrator.jiekouhuidiaolianxi1;

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

import com.bwie.administrator.jiekouhuidiaolianxi1.adapter.MyAdapter;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ArrayList<String> datas = new ArrayList<>();
    private MyAdapter adapter;
    private RecyclerView MyRec;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化資料
        initData();
        //查詢控制元件
        initView();
        //建立介面卡
        adapter = new MyAdapter(MainActivity.this,datas);
        //設定介面卡
        MyRec.setAdapter(adapter);
        //點選提示
        adapter.setOnItemClick(new MyAdapter.OnItemClick() {
            @Override
            public void onItemClick(View view, int position) {
			//點選事件觸發後,吐司提示                
			Toast.makeText(MainActivity.this,datas.get(position),Toast.LENGTH_SHORT).show();
            }
        });
    }
    //初始化資料
    private void initData() {
        for (int i = 1; i <= 20; i++) {
            datas.add("我最帥" + i);
        }
    }

    private void initView() {
        MyRec = (RecyclerView) findViewById(R.id.MyRec);
        //建立一個線性佈局管理器
        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        MyRec.setLayoutManager(manager);
    }
}

以上