RecyclerView循环复用控件(安卓开发学习笔记——31)

请添加图片描述
2.
在这里插入图片描述
3.
(1)横向布局:(LinearLayoutManager)

//item_view (新建):(运行虚拟机后,发现布局不对时,需要调整下面布局的宽度和高度)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="60dp"
    android:layout_height="60dp">

    <TextView

        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </TextView>

</LinearLayout>
//主活动布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycle_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.recyclerview.widget.RecyclerView>

</LinearLayout>
package com.example.test_1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    RecyclerView recyclerView;
    String[] dataArr = {"第一项", "第二项", "第三项", "第四项", "第五项", "第六项"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recycle_view);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);	
        recyclerView.setLayoutManager(layoutManager);

		/**recyclerView.setLayoutManager(new GridLayoutManager(this, 3));--3代表的是列数**/
		/**GridLayoutManager--设置Grid布局**/
		/**LinearLayoutManager--设置List布局**/

        MyAdapter adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);


    }

    private class MyAdapter extends RecyclerView.Adapter {

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
            MyViewHolder viewHolder = new MyViewHolder(itemView);

            return viewHolder;
        }


        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

                ((MyViewHolder) holder).textView.setText("编号:" + position);

        }

        @Override
        public int getItemCount() {
            return dataArr.length;

        }

        private class MyViewHolder extends RecyclerView.ViewHolder {

            private TextView textView;

            public MyViewHolder(View view) {
                super(view);
                textView = (TextView) view.findViewById(R.id.text_view);

            }
        }
    }
}

图-1:(List)
请添加图片描述
图-2:(List)
*注意:将Java代码对应位置的代码改为下面的代码,就可以得到图2的效果。

LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, true)*
请添加图片描述
图-3:(Grid)
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值