• Android Studio [RecyclerView/列表视图]


    LinearRecyclerViewActivity.java

    package com.xdw.a122.recyclerview;
    
    import android.graphics.Rect;
    import android.support.annotation.NonNull;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.widget.Toast;
    
    import com.xdw.a122.R;
    
    public class LinearRecyclerViewActivity extends AppCompatActivity {
        private RecyclerView mRvmain;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_linear_recycler_view);
            mRvmain=findViewById(R.id.rv_main);
            mRvmain.setLayoutManager(new LinearLayoutManager(LinearRecyclerViewActivity.this));
            mRvmain.addItemDecoration(new SimpleDividerItemDecoration(this,20, 5));
            mRvmain.setAdapter(new LinearAdapter(LinearRecyclerViewActivity.this, new LinearAdapter.OnItemClickListener() {
                @Override
                public void onClick(int pos) {
                    Toast.makeText(LinearRecyclerViewActivity.this,"Click"+pos,Toast.LENGTH_LONG).show();
                }
            }));
    
        }
    
        class MyDecoration extends RecyclerView.ItemDecoration{
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(0,0,0,getResources().getDimensionPixelOffset(R.dimen.dividerHeight));
            }
        }
    }

    LinearAdapter.java

    package com.xdw.a122.recyclerview;
    
    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.design.animation.Positioning;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.xdw.a122.R;
    
    import java.util.List;
    
    public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinearViewHolder> {
        private Context mContext;
        private OnItemClickListener mlistener;  //分配空间
        //private List<String> list;
        public LinearAdapter(Context context,OnItemClickListener listener){
            this.mContext=context;             //内容
            this.mlistener=listener;          //传输位置
        }
        @NonNull
        @Override
        public LinearAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item,viewGroup,false));
        }
    
        @Override
        public void onBindViewHolder(@NonNull LinearAdapter.LinearViewHolder viewHolder, final int i) {
            viewHolder.textView.setText("Hello World");  //内容
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {  //监听器按键
                @Override
                public void onClick(View v) {
                    mlistener.onClick(i);       //监听器
                }
            });
        }
    
        @Override
        public int getItemCount() {
            return 30;
        }
        class LinearViewHolder extends RecyclerView.ViewHolder{
            public TextView textView;
            public LinearViewHolder(@NonNull View itemView) {
                super(itemView);
                textView=itemView.findViewById(R.id.tv_title);
            }
        }
        public interface OnItemClickListener{  //字节型监听器
            void onClick(int pos);
    
        }
    }

    RecyclerViewActivity

    package com.xdw.a122.recyclerview;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    import com.xdw.a122.R;
    
    public class RecyclerViewActivity extends AppCompatActivity {
        private Button mBtnLinear;
        private Button mBtnHor;
        private Button mBtnGrid;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_recycle_view);
            mBtnLinear=findViewById(R.id.btn_linear);
            mBtnLinear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(RecyclerViewActivity.this,LinearRecyclerViewActivity.class);
                    startActivity(intent);
                }
            });
            mBtnHor=findViewById(R.id.btn_hor);
            mBtnHor.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(RecyclerViewActivity.this,HorRecyclerViewActivity.class);
                    startActivity(intent);
                }
            });
            mBtnGrid=findViewById(R.id.btn_grid);
            mBtnGrid.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(RecyclerViewActivity.this,GridRecyclerViewActivity.class);
                    startActivity(intent);
                }
            });
        }
    }

    activity_layout_linear_item.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="wrap_content"
        >
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:textColor="#000"
            android:textSize="20sp"
            />
    </LinearLayout>

    activity_linear_recycler_view.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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=".recyclerview.LinearRecyclerViewActivity">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
    
            />
    </RelativeLayout>

    activity_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"
        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>

    结果:

  • 相关阅读:
    Convolutional Neural Network-week1编程题(一步步搭建CNN模型)
    Coursera Deep Learning笔记 卷积神经网络基础
    爬取b站周杰伦新歌mv弹幕 绘制词云
    Coursera Deep Learning笔记 结构化机器学习项目 (下)
    Golang-执行go get私有库提示”410 Gone“ 解决办法
    golang常用的http请求操作
    关于asyncio知识(四)
    关于asyncio知识(二)
    Python Every Class Needs a __repr__
    关于asyncio知识(一)
  • 原文地址:https://www.cnblogs.com/zlc364624/p/10713825.html
Copyright © 2020-2023  润新知