最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)RecyclerView嵌套流式布局的詳細(xì)過程

 更新時間:2022年12月24日 11:46:11   作者:wayne214  
最近在做需求的時候,碰到有各種篩選項的界面,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)RecyclerView嵌套流式布局的詳細(xì)過程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

Android開發(fā)中,列表頁面是常見需求,流式布局的標(biāo)簽效果也是常見需求,那么兩者結(jié)合的效果啥樣呢?這篇文章簡單實(shí)現(xiàn)一下。

實(shí)現(xiàn)過程

  • 添加流式布局依賴,在app/build.gradle文件中添加如下代碼
implementation 'com.google.android.flexbox:flexbox:3.0.0'
  • 新建Activity文件RecyclerViewActivity.class
package com.example.androidstudy;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.Toast;

import com.example.androidstudy.adapter.MyRecyclerAdapter;
import com.example.androidstudy.bean.TestData;

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

public class RecyclerViewActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private MyRecyclerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        initViews();
        initListener();
    }

    private void initListener() {
        adapter.setItemCellClicker(tag -> Toast.makeText(RecyclerViewActivity.this, tag, Toast.LENGTH_SHORT).show());
    }

    private void initViews() {
        recyclerView = findViewById(R.id.recyclerview);
        // 設(shè)置布局管理器
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        List<String> sss = new ArrayList<>();
        sss.add("重型卡車1");
        sss.add("重車11");
        sss.add("重型卡車3445");
        sss.add("重型卡車6677");
        List<String> sss1 = new ArrayList<>();
        sss1.add("輕型卡車1");
        sss1.add("輕車11");
        sss1.add("輕型卡車3445");
        sss1.add("輕型卡車6677");

        List<String> sss2 = new ArrayList<>();
        sss2.add("其他1");
        sss2.add("其他2");
        List<TestData> list = new ArrayList<>();
        list.add(new TestData("重型",sss));
        list.add(new TestData("輕型", sss1));
        list.add(new TestData("其他", sss2));
        // 實(shí)例化Adapter對象
        adapter = new MyRecyclerAdapter(this, list);
        // 設(shè)置Adapter
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
}

Activity頁面布局activity_recycler_view.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=".RecyclerViewActivity">

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

</androidx.constraintlayout.widget.ConstraintLayout>
  • 創(chuàng)建Adapter文件MyRecyclerAdapter.class
package com.example.androidstudy.adapter;

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.androidstudy.R;
import com.example.androidstudy.bean.TestData;
import com.google.android.flexbox.FlexboxLayout;

import java.util.List;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder>{

    private List<TestData> data;
    private Context myContext;

    public MyRecyclerAdapter(Context context, List<TestData> data) {
        this.myContext = context;
        this.data = data;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cell, parent, false);
        return new MyViewHolder(inflate);
    }

    public interface ItemCellClicker{
        void onItemClick(String tag);
    }

  // 流式布局標(biāo)簽點(diǎn)擊事件
    public ItemCellClicker itemCellClicker;
  // 設(shè)置點(diǎn)擊事件回調(diào)
    public void setItemCellClicker(ItemCellClicker itemCellClicker){
        this.itemCellClicker = itemCellClicker;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        TextView title = holder.itemView.findViewById(R.id.tv_title);
        FlexboxLayout flexboxLayout = holder.itemView.findViewById(R.id.flexbox_layout);

        TestData data = this.data.get(position);
        List<String> tags = data.getTag();
        flexboxLayout.removeAllViews();
        // flexbox布局動態(tài)添加標(biāo)簽
        for (int i = 0; i < tags.size(); i++) {
            String temp = tags.get(i);
            View tagView = LayoutInflater.from(myContext).inflate(R.layout.item_tag_cell, null, false);
            TextView tag = tagView.findViewById(R.id.tv_tag);
            tag.setText(temp);
            // 設(shè)置標(biāo)簽點(diǎn)擊事件
            tag.setOnClickListener(view -> itemCellClicker.onItemClick(temp));
            flexboxLayout.addView(tagView);
        }
        title.setText(data.getTitle());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}

列表項布局item_cell.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="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".MyActivity">

    <TextView
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:id="@+id/tv_title"
        android:text="Hello android"
        android:textSize="20sp"
        android:textColor="@color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <!--流式布局-->
    <com.google.android.flexbox.FlexboxLayout
        android:id="@+id/flexbox_layout"
        android:orientation="horizontal"
        app:flexWrap="wrap"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

列表中標(biāo)簽布局item_tag_cell.xml

<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="wrap_content"
    android:padding="10dp"
    tools:context=".MyActivity">

    <TextView
        android:id="@+id/tv_tag"
        android:paddingHorizontal="12dp"
        android:background="@drawable/item_tag_bg"
        android:gravity="center"
        android:text="Hello android"
        android:textSize="20sp"
        android:textColor="@color/black"
        android:layout_width="wrap_content"
        android:layout_height="32dp"/>

</LinearLayout>

效果

總結(jié)

到此這篇關(guān)于Android實(shí)現(xiàn)RecyclerView嵌套流式布局的文章就介紹到這了,更多相關(guān)Android RecyclerView嵌套流式布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android通過AIDL在兩個APP之間Service通信

    Android通過AIDL在兩個APP之間Service通信

    這篇文章主要為大家詳細(xì)介紹了Android通過AIDL在兩個APP之間Service通信,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android仿微信滑動彈出編輯、刪除菜單效果、增加下拉刷新功能

    Android仿微信滑動彈出編輯、刪除菜單效果、增加下拉刷新功能

    這篇文章主要介紹了Android仿微信滑動彈出編輯、刪除菜單效果、增加下拉刷新功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android下如何使用百度地圖sdk

    Android下如何使用百度地圖sdk

    百度地圖 Android SDK是一套基于Android 2.1(v1.3.5及以前版本支持android 1.5以上系統(tǒng))及以上版本設(shè)備的應(yīng)用程序接口
    2013-07-07
  • Android自定義HorizontalScrollView打造超強(qiáng)Gallery效果

    Android自定義HorizontalScrollView打造超強(qiáng)Gallery效果

    這篇文章主要介紹了Android自定義HorizontalScrollView打造圖片橫向滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android編程中@id和@+id的區(qū)別分析

    Android編程中@id和@+id的區(qū)別分析

    這篇文章主要介紹了Android編程中@id和@+id的區(qū)別,較為詳細(xì)的分析講述了Android中資源引入的原理及使用@id和@+id引入資源的具體用法,總結(jié)了二者的區(qū)別,需要的朋友可以參考下
    2016-01-01
  • Android View滑動的實(shí)現(xiàn)分析示例

    Android View滑動的實(shí)現(xiàn)分析示例

    View滑動是Android實(shí)現(xiàn)自定義控件的基礎(chǔ),同時在開發(fā)中難免會遇到View的滑動處理,其實(shí)不管是那種滑動方法,基本思路是類似的;當(dāng)點(diǎn)擊事件傳到View時,系統(tǒng)記下觸摸點(diǎn)的坐標(biāo),手指移動時系統(tǒng)記下移動后的左邊并算出偏移量,通過偏移量來修改View的坐標(biāo)
    2022-08-08
  • arcgis android之定位功能的示例代碼

    arcgis android之定位功能的示例代碼

    這篇文章主要介紹了arcgis android之定位功能的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Android Scroller完全解析

    Android Scroller完全解析

    這篇文章主要為大家詳細(xì)介紹了Android Scroller源碼,區(qū)分scrollTo()和scrollBy(),感興趣的小伙伴們可以參考一下
    2016-10-10
  • Android 調(diào)用發(fā)送短信的方法

    Android 調(diào)用發(fā)送短信的方法

    這篇文章主要介紹了Android 調(diào)用發(fā)送短信的方法的相關(guān)資料,主要實(shí)現(xiàn)Android 調(diào)用短信的使用,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • Kotlin Flow操作符及基本使用詳解

    Kotlin Flow操作符及基本使用詳解

    這篇文章主要為大家介紹了Kotlin Flow操作符及基本使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08

最新評論

武清区| 通道| 宜都市| 新沂市| 会泽县| 南投县| 寿阳县| 岱山县| 南安市| 三明市| 莆田市| 株洲县| 七台河市| 汝州市| 高淳县| 隆子县| 汉沽区| 祥云县| 保山市| 砀山县| 林西县| 天津市| 长沙市| 壶关县| 桑日县| 南部县| 庆元县| 霍林郭勒市| 左权县| 昭通市| 嘉善县| 孟连| 阿克苏市| 上思县| 利津县| 鄂托克旗| 白银市| 始兴县| 四子王旗| 平果县| 陵水|