小白开发程序之路(2-11-2)RecyclerView

匿名类实现接口

在HorRecyclerViewActivity.java中给mRvHor添加适配器时使用了匿名类实现HorAdaptor中的OnItemClickListener接口。
关于匿名类实现接口的相关知识可在菜鸟教程Java匿名类学习👇

https://www.runoob.com/java/java-anonymous-class.html

代码

垂直视图和网格视图差不多,所以只贴了网格视图的代码

RecyclerViewActivity

package com.example.myapplication.recyclerview;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.myapplication.R;

public class RecyclerViewActivity extends AppCompatActivity {
    private Button mBtnLiner,mBtnHor,mBtnGrid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        mBtnLiner=findViewById(R.id.btn_linear);
        mBtnHor=findViewById(R.id.btn_hor);
        mBtnGrid=findViewById(R.id.btn_grid);
        mBtnLiner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(RecyclerViewActivity.this, LinearRecyclerViewActivity.class);
                startActivity(intent);

            }
        });
        mBtnHor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(RecyclerViewActivity.this, HorRecyclerViewActivity.class);
                startActivity(intent);

            }
        });
        mBtnGrid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(RecyclerViewActivity.this, GridRecyclerViewActivity.class);
                startActivity(intent);
            }
        });
    }
}

activity_recycler_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">
    <Button
        android:id="@+id/btn_linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="列表视图"/>
    <Button
        android:id="@+id/btn_hor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="水平滚动"/>
    <Button
        android:id="@+id/btn_grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="网格视图"/>

</LinearLayout>

效果1

在这里插入图片描述

GridRecyclerViewActivity.java

package com.example.myapplication.recyclerview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.Toast;

import com.example.myapplication.R;

public class GridRecyclerViewActivity extends AppCompatActivity {
    private RecyclerView mRvGrid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_recycler_view);
        mRvGrid=findViewById(R.id.rv_grid);
        mRvGrid.setLayoutManager(new GridLayoutManager(GridRecyclerViewActivity.this,3));
        //spanCount表示一行显示几个
        mRvGrid.setAdapter(new GridAdapter(GridRecyclerViewActivity.this, new GridAdapter.OnItemCLickListener() {
            @Override
            public void onClick(int pos) {
                Toast.makeText(GridRecyclerViewActivity.this,"click"+pos,Toast.LENGTH_SHORT).show();
            }
        }));
    }
}

activity_grid_recycler_view.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">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

GridAdapter.java

package com.example.myapplication.recyclerview;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import com.example.myapplication.R;

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.LinearViewHolder> {
    public static Object OnItemCLickListener;
    private Context mcontext;
    private OnItemCLickListener mListener;
//    private List<String> list;
    public GridAdapter(Context context, OnItemCLickListener listener){
        this.mcontext=context;
        this.mListener=listener;

    }
    @NonNull
    @Override
    public GridAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new LinearViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.layout_grid_recyclerview_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull GridAdapter.LinearViewHolder holder, final int position) {
        holder.textView.setText("新的蕾姆已经出现");
        //加个点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mcontext,"点击"+position,Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return 80;
    }
    
    class LinearViewHolder extends ViewHolder{
        private TextView textView;
        public LinearViewHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.tv_title);
        }
    }
    public interface OnItemCLickListener{
        void onClick(int pos);
    }
}

layout_grid_recycler_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:gravity="center"
        android:textColor="#03A9F4"
        android:textSize="20sp"
        android:text="蕾姆"
        android:background="#FFFEF2"/>
<!--layout 宽度和高度那里不同的模式会有不同的效果,可以切换试试看-->

</LinearLayout>

效果2

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值