Android recyclerView实现二级横向显示

Posted 踏雪羽翼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android recyclerView实现二级横向显示相关的知识,希望对你有一定的参考价值。

1、效果图

2、主要实现比较简单,首先data数据类

package com.example.mytestproject.recycle;

import java.io.Serializable;
import java.util.List;

public class RecyclerItem implements Serializable 
    private int icon;
    private String name;
    private List<Integer> iconList;

    public int getIcon() 
        return icon;
    

    public void setIcon(int icon) 
        this.icon = icon;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public List<Integer> getIconList() 
        return iconList;
    

    public void setIconList(List<Integer> iconList) 
        this.iconList = iconList;
    

3、这里随意加载一些数据、和设置recyclerView横向显示

 mRecyclerView = findViewById(R.id.recyclerView);
        mAdapter = new HotelEntityAdapter(this);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        //设置header
//        manager.setSpanSizeLookup(new SectionedSpanSizeLookup(mAdapter,manager));
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
        for (int i=0;i<15;i++)
            mIconList.add(R.mipmap.item);
        

        for (int i=0;i<5;i++)
            RecyclerItem recyclerItem = new RecyclerItem();
            recyclerItem.setIcon(R.mipmap.title);
            recyclerItem.setIconList(mIconList);
            mItemList.add(recyclerItem);
        

        mAdapter.setData(mItemList);
        mAdapter.setOnItemClick(new HotelEntityAdapter.OnItemClickListener() 
            @Override
            public void onItemClick(int section, int position) 
                Toast.makeText(RecyclerActivity.this,section+"=="+position,Toast.LENGTH_SHORT).show();
            
        );

4、adapter的实现,传入数据后、加载两个布局显示、然后接口回调点击事件,代码比较简单。

package com.example.mytestproject.recycle;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.example.mytestproject.R;

import java.util.List;

public class HotelEntityAdapter extends SectionedRecyclerViewAdapter<HeaderHolder, DescHolder, RecyclerView.ViewHolder> 


    public List<RecyclerItem> mRecyclerItemList;
    private Context mContext;
    private LayoutInflater mInflater;

    private SparseBooleanArray mBooleanMap;

    public HotelEntityAdapter(Context context) 
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mBooleanMap = new SparseBooleanArray();
    

    public void setData(List<RecyclerItem> allTagList) 
        this.mRecyclerItemList = allTagList;
        mBooleanMap.put(0, true);
        notifyDataSetChanged();
    


    protected int getSectionCount() 
        int count = 0;
        if (mRecyclerItemList != null) 
            count = mRecyclerItemList.size();
        
        return count;
    

    @Override
    protected int getItemCountForSection(int section) 
        int count = mRecyclerItemList.get(section).getIconList().size();
        if (count >= 1 && !mBooleanMap.get(section)) 
            count = 0;
        
        if (section == 0 && mBooleanMap.get(section)) 
            count = mRecyclerItemList.get(section).getIconList().size();
        
        return HotelUtils.isEmpty(mRecyclerItemList.get(section).getIconList()) ? 0 : count;
    

    //是否有footer布局

    /**
     * 判断是否需要底部footer布局,在该例中,我们并不需要显示footer,所以默认返回false就可以,
     * 如果你对应的section需要展示footer布局,那么就在对应的section返回true就行了
     *
     * @param section
     * @return
     */
    @Override
    protected boolean hasFooterInSection(int section) 
        return false;
    

    @Override
    protected HeaderHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) 
        return new HeaderHolder(mInflater.inflate(R.layout.hotel_title_item, parent, false));
    


    @Override
    protected RecyclerView.ViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) 
        return null;
    

    @Override
    protected DescHolder onCreateItemViewHolder(ViewGroup parent, int viewType) 
        return new DescHolder(mInflater.inflate(R.layout.hotel_desc_item, parent, false));
    


    @Override
    protected void onBindSectionHeaderViewHolder(final HeaderHolder holder, final int section) 
        holder.titleView.setText(mRecyclerItemList.get(section).getName());
        holder.openView.setImageResource(mRecyclerItemList.get(section).getIcon());
        holder.llHotel.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                boolean isOpen = mBooleanMap.get(section);
                mBooleanMap.put(section, !isOpen);
                notifyDataSetChanged();
            
        );
    


    @Override
    protected void onBindSectionFooterViewHolder(RecyclerView.ViewHolder holder, int section) 

    

    /**
     * @param holder
     * @param section
     * @param position
     */
    @Override
    protected void onBindItemViewHolder(DescHolder holder, final int section, final int position) 
        holder.descView.setImageResource(mRecyclerItemList.get(section).getIconList().get(position));

        holder.descView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                if (onItemClickListener != null) 
                    onItemClickListener.onItemClick(section, position);
                
            
        );

    

    private OnItemClickListener onItemClickListener;

    public void setOnItemClick(OnItemClickListener listener) 
        this.onItemClickListener = listener;
    

    public interface OnItemClickListener 
        void onItemClick(int section, int position);
    

5、两个holder的代码主要就是一个图片显示,实现都是比较简单的。

package com.example.mytestproject.recycle;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.mytestproject.R;


public class HeaderHolder extends RecyclerView.ViewHolder 
    public TextView titleView;
    public ImageView openView;
    public LinearLayout llHotel;

    public HeaderHolder(View itemView) 
        super(itemView);
        initView();
    

    private void initView() 
        titleView = (TextView) itemView.findViewById(R.id.tv_title);
        openView = (ImageView) itemView.findViewById(R.id.tv_icon);
        llHotel = itemView.findViewById(R.id.ll_hotel);
    

package com.example.mytestproject.recycle;


import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.mytestproject.R;


public class DescHolder extends RecyclerView.ViewHolder 
    public ImageView descView;

    public DescHolder(View itemView) 
        super(itemView);
        initView();
    

    private void initView() 
        descView = (ImageView) itemView.findViewById(R.id.iv_icon);
    

这个主要是参考网上的一个二级页面稍作修改得来、想看具体实现代码可以自己下载https://download.csdn.net/download/u011324501/12615856,demo实现比较简单、里面还有自由绘图跟一个用户各种数据缓存的类。

以上是关于Android recyclerView实现二级横向显示的主要内容,如果未能解决你的问题,请参考以下文章

Kevin Learn RecyclerView--> 实现二级列表

Kotlin ExpandableListView可扩展二级列表

GPHONE:android如何实现横屏

Android - 为 RecyclerView 实现搜索过滤器

Android 11 设置开机默认系统横屏显示

android 怎样让drawerlayout设置的侧滑菜单的内容充满屏幕