android RecyclerView的Grid布局案例

android RecyclerView的Grid布局案例,第1张

概述1、先创建activity_grid.xml和 activity_grid_item.xml <?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apkes/android"xmlns:app=&quot

1、先创建activity_grID.xml 和 activity_grID_item.xml 

<?xml version="1.0" enCoding="utf-8"?><androIDx.constraintlayout.Widget.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=".GrIDActivity">    <androIDx.recyclervIEw.Widget.RecyclerVIEw        androID:ID="@+ID/rv2"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        tools:layout_editor_absoluteX="103dp"        tools:layout_editor_absoluteY="44dp" /></androIDx.constraintlayout.Widget.ConstraintLayout>
<?xml version="1.0" enCoding="utf-8"?><androIDx.constraintlayout.Widget.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="wrap_content"    androID:layout_height="wrap_content"    tools:context=".GrIDItemActivity">    <TextVIEw        androID:ID="@+ID/textVIEw3"        androID:layout_wIDth="50dp"        androID:layout_height="50dp"        androID:text="@string/textvIEw2"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_totopOf="parent" /></androIDx.constraintlayout.Widget.ConstraintLayout>

2、创建Adapter

package com.example.myapplication;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;public class GrIDAdapter extends RecyclerVIEw.Adapter<GrIDAdapter.GrIDVIEwHolder> {    private Context context;    public GrIDAdapter(Context context){        this.context = context;    }    @NonNull    @OverrIDe    public GrIDAdapter.GrIDVIEwHolder onCreateVIEwHolder(@NonNull VIEwGroup parent, int vIEwType) {        return new GrIDVIEwHolder(LayoutInflater.from(context).inflate(R.layout.activity_grID_item,parent,false));    }    @OverrIDe    public voID onBindVIEwHolder(@NonNull GrIDAdapter.GrIDVIEwHolder holder, final int position) {        holder.tv.setText("Hello world");        holder.tv.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw vIEw) {                Toast.makeText(context,"click..."+position,Toast.LENGTH_SHORT).show();            }        });    }    @OverrIDe    public int getItemCount() {        return 30;    }    class GrIDVIEwHolder extends RecyclerVIEw.VIEwHolder{        private TextVIEw tv;        public GrIDVIEwHolder(@NonNull VIEw itemVIEw) {            super(itemVIEw);            tv = itemVIEw.findVIEwByID(R.ID.textVIEw3);        }    }}

3、GrIDActivity.java

package com.example.myapplication;import androIDx.appcompat.app.AppCompatActivity;import androIDx.recyclervIEw.Widget.GrIDLayoutManager;import androIDx.recyclervIEw.Widget.RecyclerVIEw;import androID.os.Bundle;public class GrIDActivity extends AppCompatActivity {    private RecyclerVIEw rv;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_grID);        rv = findVIEwByID(R.ID.rv2);        rv.setLayoutManager(new GrIDLayoutManager(GrIDActivity.this,3));        rv.setAdapter(new GrIDAdapter(GrIDActivity.this));    }}

4、效果:

 

总结

以上是内存溢出为你收集整理的android RecyclerView的Grid布局案例全部内容,希望文章能够帮你解决android RecyclerView的Grid布局案例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1110378.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存