zoukankan      html  css  js  c++  java
  • android RecyclerView的Grid布局案例

    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、效果:

  • 相关阅读:
    功能类控件
    关系类控件-明细表
    WPF MenuItem 四种角色分析
    自定义的 ListBoxItem 自适应ListBox的宽度
    由DataGridTextColumn不能获取到父级DataContext引发的思考
    GDI+中发生一般性错误的解决办法(转帖)
    WPF中RadioButton绑定数据的正确方法
    C# 使用XML序列化对象(二)
    C# 使用XML序列化对象(一)
    WPF中RadioButton的分组
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/11407867.html
Copyright © 2011-2022 走看看