欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Studio教程11-RecycleView的使用

程序员文章站 2022-11-21 21:03:36
[TOC] 1. RecyclerView 1.1. Add support library 1.2. 将RecyclerView添加到布局 此文件命名为: 1.3. 主actiivty中如何调用recycleview对象 2. 实例 本节中的所有代码已上传到: ......

目录

1. recyclerview

1.1. add support library

dependencies {
    implementation 'com.android.support:recyclerview-v7:28.0.0'
}

1.2. 将recyclerview添加到布局

此文件命名为:activity_recycle_demo.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"
    tools:context=".recycledemoactivity">

    <linearlayout
        android:id="@+id/layout01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <button
            android:id="@+id/linearlayout_recycle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="线性布局"/>

        <button
            android:id="@+id/grid_recycle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="网格布局"/>

        <button
            android:id="@+id/staggeredlayout_recycle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="瀑布布局"/>

    </linearlayout>


    <android.support.v7.widget.recyclerview
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.recyclerview>

</linearlayout>

1.3. 主actiivty中如何调用recycleview对象

recycledemoactivity.java

package com.haoch95.fristapp;

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.support.v7.widget.defaultitemanimator;
import android.support.v7.widget.divideritemdecoration;
import android.support.v7.widget.gridlayoutmanager;
import android.support.v7.widget.linearlayoutmanager;
import android.support.v7.widget.recyclerview;
import android.support.v7.widget.staggeredgridlayoutmanager;
import android.view.menu;
import android.view.menuitem;
import android.view.view;
import android.widget.button;
import android.widget.textview;
import android.widget.toast;

import java.util.arraylist;
import java.util.list;

public class recycledemoactivity extends appcompatactivity {

    private button linearbt, gridbt, starbt;

    private recyclerview mrecyclerview;
    private recycleadapter mmyadapter;
    private recyclerview.layoutmanager mlayoutmanager;
    private list<string> list;


    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_recycle_demo);

        linearbt = (button)findviewbyid(r.id.linearlayout_recycle);
        gridbt = (button)findviewbyid(r.id.grid_recycle);
        starbt = (button)findviewbyid(r.id.staggeredlayout_recycle);
        // recyclerview的三部曲 01: 获取recyclerview对象
        mrecyclerview = (recyclerview)findviewbyid(r.id.my_recycler_view);

        initdata();

        linearbt.setonclicklistener(new buttonclicked());
        gridbt.setonclicklistener(new buttonclicked());
        starbt.setonclicklistener(new buttonclicked());




//        // 添加水平分割线
//        mrecyclerview.additemdecoration(new divideritemdecoration(this,divideritemdecoration.vertical));

    }

    private void initdata() {
        list = new arraylist<>();
        for (int i=0;i<20;i++){
            list.add("item" + i);
        }
    }

    class buttonclicked implements view.onclicklistener {
        @override
        public void onclick(view v) {
            switch (v.getid()){
                case r.id.linearlayout_recycle:
                    mlayoutmanager = new linearlayoutmanager(getapplicationcontext());
                    break;
                case r.id.grid_recycle:
                    mlayoutmanager = new gridlayoutmanager(getapplicationcontext(),4);
                    break;
                case r.id.staggeredlayout_recycle:
                    //如果传入 staggeredgridlayoutmanager.vertical则该布局支持纵向滑动,那么前面的 4 则指的是 4列。
                    //如果传入 staggeredgridlayoutmanager.horizontal,则该布局支持横向滑动,那么前面的4 则指的是 4行
                    mlayoutmanager = new staggeredgridlayoutmanager(4,staggeredgridlayoutmanager.vertical);
                    break;
                default:
                    mlayoutmanager = new linearlayoutmanager(getapplicationcontext());
                    break;
            }
            //recyclerview的三部曲 02:设置布局管理器
            mrecyclerview.setlayoutmanager(mlayoutmanager);
            mrecyclerview.additemdecoration(new divideritemdecoration(getapplicationcontext(),divideritemdecoration.vertical));
            // recyclerview的三部曲 03: 添加适配器,适配器需要重写
            mmyadapter = new recycleadapter(list, getapplicationcontext());
            mrecyclerview.setadapter(mmyadapter);
            //设置item增加、移除动画
            mrecyclerview.setitemanimator(new defaultitemanimator());

        }
    }

    @override
    public boolean oncreateoptionsmenu(menu menu) {
        getmenuinflater().inflate(r.menu.menu, menu);
        return true;
    }

    @override
    public boolean onoptionsitemselected(menuitem item) {
        switch (item.getitemid()) {
            case r.id.add:
                mmyadapter.adddata(1);
                break;
            case r.id.delete:
                mmyadapter.removedata(1);
                break;
        }
        return true;
    }
}

1.4. 重写定义adapter

recycleadapter

package com.haoch95.fristapp;

import android.content.context;
import android.support.annotation.nonnull;
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 java.util.list;
import java.util.random;

public class recycleadapter extends recyclerview.adapter<recycleadapter.myviewholder> {

    private list<string> mlist;
    private context context;

    public recycleadapter(list<string> list, context context){
        this.mlist = list;
        this.context = context;
    }

    @override
    public int getitemcount() {
        return mlist.size();
    }


    public class myviewholder extends recyclerview.viewholder {

        public textview mview;

        public myviewholder(@nonnull view itemview) {
            super(itemview);
            mview = itemview.findviewbyid(r.id.text_view);
        }
    }

    //创建viewholder
    //oncreateviewholder方法创建一个viewholder,viewholder可以理解为一条数据的展示布局,这里我们自定义类linearviewholder创建一个只有textview的item
    //这里我们需要创建每条布局使用的layout:recycle_item
    @nonnull
    @override
    public myviewholder oncreateviewholder(@nonnull viewgroup viewgroup, int i) {
        return new myviewholder(layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.recycle_item, viewgroup, false));
    }


    //填充视图
    //onbindviewholder方法为item的ui绑定展示数据
    @override
    public void onbindviewholder(@nonnull final myviewholder holder, final int position) {
        holder.mview.settext(mlist.get(position));

        holder.itemview.setonclicklistener(new view.onclicklistener(){

            @override
            public void onclick(view v) {
                toast.maketext(context, "点击子项"+position, toast.length_short).show();
            }
        });

    }

    public void adddata(int position) {
        mlist.add(position, "insert one");
        notifyiteminserted(position);
    }

    public void removedata(int position) {
        mlist.remove(position);
        notifyitemremoved(position);
    }

    public void clearall(){
        mlist.clear();
        notifydatasetchanged();
    }


}

2. 实例

本节中的所有代码已上传到:

Android Studio教程11-RecycleView的使用