zoukankan      html  css  js  c++  java
  • 寒假学习14

    Android Studio:RecyclerView

    RecyclerView能够灵活实现大数据集的展示,视图的复用管理比ListView更好,能够实现列表、网格、瀑布流等形式,且不同的ViewHolder能够实现item多元化的功能。

    MainActivity.java

    package com.example.myapplicationfirst;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerview.widget.RecyclerView;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.RadioButton;
    
    import com.example.myapplicationfirst.recyclerview.RecyclerViewActivity;
    
    public class MainActivity extends AppCompatActivity {
    
        //声明空间
        private Button mBtnTextView;
        private Button mBtnButton;
        private Button mBtnEditText;
        private Button mBtnRadioButton;
        private Button mBtnCheckBox;
        private Button mBtnRecyclerView;
    
        //寻找事件
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mBtnTextView = findViewById(R.id.btn_textview);
            mBtnButton = findViewById(R.id.btn_button);
            mBtnEditText = findViewById(R.id.btn_edittext);
            mBtnRadioButton = findViewById(R.id.btn_radiobutton);
            mBtnCheckBox = findViewById(R.id.btn_checkbox);
            mBtnRecyclerView = findViewById(R.id.btn_recyclerview);
            setListeners();
        }
    
        //监听事件
        private void setListeners(){
            OnClick onClick = new OnClick();
            mBtnTextView.setOnClickListener(onClick);
            mBtnButton.setOnClickListener(onClick);
            mBtnEditText.setOnClickListener(onClick);
            mBtnRadioButton.setOnClickListener(onClick);
            mBtnCheckBox.setOnClickListener(onClick);
            mBtnRecyclerView.setOnClickListener(onClick);
        }
    
        //点击事件
        private class OnClick implements View.OnClickListener{
    
            @Override
            public void onClick(View v) {
                Intent intent = null;
                switch (v.getId()){
                    case R.id.btn_textview:
                        //跳转到TextView演示界面
                        intent = new Intent(MainActivity.this,TextViewActivity.class);
                        break;
                    case R.id.btn_button:
                        //跳转到Button演示界面
                        intent = new Intent(MainActivity.this,ButtonActivity.class);
                        break;
                    case R.id.btn_edittext:
                        //跳转到EditText演示界面
                        intent = new Intent(MainActivity.this,EditTextActivity.class);
                        break;
                    case R.id.btn_radiobutton:
                        //跳转到RadioButton演示界面
                        intent = new Intent(MainActivity.this, RadioButtonActivity.class);
                        break;
                    case R.id.btn_checkbox:
                        //跳转到CheckBox演示界面
                        intent = new Intent(MainActivity.this, CheckBoxActivity.class);
                        break;
                    case R.id.btn_recyclerview:
                        //跳转到RecyclerView演示界面
                        intent = new Intent(MainActivity.this, RecyclerViewActivity.class);
                        break;
                }
                startActivity(intent);
            }
        }
    }

    RecyclerViewActivity.java

    package com.example.myapplicationfirst.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.myapplicationfirst.R;
    
    public class RecyclerViewActivity extends AppCompatActivity {
    
        private Button mBtnLinear;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_recycler_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);
                }
            });
        }
    }

    LinearRecyclerViewActivity.java

    package com.example.myapplicationfirst.recyclerview;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    
    import android.os.Bundle;
    
    import com.example.myapplicationfirst.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.setAdapter(new LinearAdapter(LinearRecyclerViewActivity.this));
        }
    }

    LinearAdapter.java

    package com.example.myapplicationfirst.recyclerview;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;
    
    import com.example.myapplicationfirst.R;
    
    public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinearViewHolder> {
    
        private Context mContext;
        public LinearAdapter(Context context){
            this.mContext = context;
        }
    
        @NonNull
        @Override
        public LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
            return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item,parent,false));
        }
    
        @Override
        public void onBindViewHolder(@NonNull LinearAdapter.LinearViewHolder holder, int position) {
            holder.textView.setText("Hello World");
        }
    
        @Override
        public int getItemCount() {
            return 30;
        }
    
        class LinearViewHolder extends RecyclerView.ViewHolder{
    
            private TextView textView;
    
            public LinearViewHolder(View itemView){
                super(itemView);
                textView = itemView.findViewById(R.id.tv_title);
            }
        }
    }

    res>layout>layout_linear_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/tv_type"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:textColor="#000"
            android:textSize="20sp"/>
    </LinearLayout>

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/btn_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textAllCaps="false"/>
    
        <Button
            android:id="@+id/btn_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"
            android:textAllCaps="false"/>
    
        <Button
            android:id="@+id/btn_edittext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="EditText"
            android:textAllCaps="false"/>
    
        <Button
            android:id="@+id/btn_radiobutton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="RadioButton"
            android:textAllCaps="false"/>
    
        <Button
            android:id="@+id/btn_checkbox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CheckBox"
            android:textAllCaps="false"/>
    
        <Button
            android:id="@+id/btn_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="RecyclerView"
            android:textAllCaps="false"/>
    
    </LinearLayout>

    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">
    
        <Button
            android:id="@+id/btn_linear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="列表视图"/>
    
    </LinearLayout>

    activity_linear_recycler_view.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryDark"/>
    
    </RelativeLayout>
  • 相关阅读:
    ABP AsyncHelper.RunSync 内部实现
    ABP 扩展 OrganizationUnit 数据实体(新增字段)
    ABP 调用 PUT 接口报错 405 Method Not Allowed
    ABP 扩展 Role 数据实体(新增字段)
    C# EntityFramework 自定义数据库表名(一)
    EPPlus.Core(OfficeOpenXml) 获取 Excel 数据集合
    C# 获取文件并将文件按创建/修改时间排序
    C# 计算两个日期之间的月份数(差值)
    C# Split 方法扩展支持双引号
    教你在 C# 代码中写出带高亮关键字的注释
  • 原文地址:https://www.cnblogs.com/vvxvv/p/12309381.html
Copyright © 2011-2022 走看看