Android GridView 有类别吗?

发布于 2024-12-04 12:51:09 字数 147 浏览 1 评论 0原文

是否可以在 Android 中使用 GridView 中的类别或某种标题?

我快速说明了我的想法:

在此处输入图像描述

非常感谢。

Is it possible to use categories or some sort of headers with a GridView in Android?

I put together a quick illustration of what I was thinking about:

enter image description here

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抱猫软卧 2024-12-11 12:51:09

您可以直接使用 Stickygridheaders 库或作为模型来创建您自己的小部件。

You can use Stickygridheaders library directly or as a model to create your own widget.

静若繁花 2024-12-11 12:51:09

也许此代码会对您有所帮助。这是SectionedGridRecyclerViewAdapter,结果如下所示:

在此处输入图像描述

probably this code will help you. This is SectionedGridRecyclerViewAdapter, result looks like this:

enter image description here

尐籹人 2024-12-11 12:51:09

我认为你可以做到这一点,但你必须实现 Jeff Shrkey 的 SeparatedListAdapter

没有一种简单的方法来创建这些单独的列表,因此我将分离列表适配器放在一起,它可以快速完成。总而言之,我们正在创建一个新的 BaseAdapter,它可以包含多个其他适配器,每个适配器都有自己的节标题。

I think You can do it but you have to implement Jeff Shrkey's SeparatedListAdapter

There isn’t an easy way of creating these separated lists, so I’ve put together SeparatedListAdapter which does it quickly. To summarize, we’re creating a new BaseAdapter that can contain several other Adapters, each with their own section headers.

浅紫色的梦幻 2024-12-11 12:51:09

您可以修改 Android RecyclerView 以在每行返回一个网格,请参阅此处

Add the `gridrecyclerview` library to your project
implementation 'com.github.koros:gridrecyclerview:1.0.2'

添加后,请按照以下步骤创建网格视图:

1. 实现 GridRecyclerViewHelper

首先创建一个实现 GridRecyclerViewHelper 的帮助器类。该类管理标题的创建、标题数据的绑定、网格视图的设置以及网格视图持有者的创建。


public class SampleGridRecyclerViewHelper implements GridRecyclerViewHelper<GridHeader> {
   @NonNull
   @Override
   public RecyclerView.ViewHolder getHeaderViewHolder(@NonNull ViewGroup parent) {
       ViewGroup view = (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.header_view, parent, false);
       return new HeaderViewHolder(view);
   }

   @Override
   public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, GridHeader headerItem) {
       ((HeaderViewHolder) holder).bind(headerItem);
   }

   @NonNull
   @Override
   public ViewGroup getGridView(GridHeader key, @NonNull ViewGroup parent) {
       switch (key.getKey()) {
           case ACTOR:
               return (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.actor_view, parent, false);
           case GENRE:
               return (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.genre_view, parent, false);
           case MOVIE:
               return (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_view, parent, false);
           case STUDIO:
               return (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.studio_view, parent, false);
           case DIRECTOR:
               return (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.director_view, parent, false);
           default:
               throw new IllegalArgumentException("Unknown Header Key Value");
       }
   }

   @NonNull
   @Override
   public GridCellViewHolder getGridViewHolder(GridHeader key, @NonNull ViewGroup parent) {
       switch (key.getKey()) {
           case ACTOR:
               return new ActorViewHolder(parent);
           case GENRE:
               return new GenreViewHolder(parent);
           case MOVIE:
               return new MovieViewHolder(parent);
           case STUDIO:
               return new StudioViewHolder(parent);
           case DIRECTOR:
               return new DirectorViewHolder(parent);
           default:
               throw new IllegalArgumentException("Unknown Header Key Value");
       }
   }
}

GridHeader 类充当普通旧 Java 对象 (POJO),保存有关网格部分标题的信息。

2. 为每个网格单元实现 ViewHolder

接下来,实现 GridCellViewHolder 接口以有效地将数据绑定到每种类型的网格单元。


public abstract class GridCellViewHolder<T> extends RecyclerView.ViewHolder {
    // Implement the bind method to efficiently bind data to the ViewHolder.
}

例如,为 actor 单元创建一个专门的 ViewHolder:


public class ActorViewHolder extends GridCellViewHolder<Actor> {
    private final Context context;
    private final TextView name;
    private final ImageView image;

    public ActorViewHolder(@NonNull View itemView) {
        super(itemView);
        this.context = itemView.getContext();
        this.name = itemView.findViewById(R.id.name);
        this.image = itemView.findViewById(R.id.image);
    }

    @Override
    public void bind(Actor actor) {
        this.name.setText(actor.getName());
        this.image.setImageDrawable(getDrawableFromName(context, actor.getImage()));
    }
}

在此处输入图像描述

You can modify an Android RecyclerView to return a grids at each row see here

Add the `gridrecyclerview` library to your project
implementation 'com.github.koros:gridrecyclerview:1.0.2'

Once the library is added, follow these steps to create a grid view:

1. Implement the GridRecyclerViewHelper

Begin by creating a helper class that implements GridRecyclerViewHelper. This class manages the creation of headers, the binding of header data, the setup of grid views, and the creation of grid view holders.


public class SampleGridRecyclerViewHelper implements GridRecyclerViewHelper<GridHeader> {
   @NonNull
   @Override
   public RecyclerView.ViewHolder getHeaderViewHolder(@NonNull ViewGroup parent) {
       ViewGroup view = (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.header_view, parent, false);
       return new HeaderViewHolder(view);
   }

   @Override
   public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, GridHeader headerItem) {
       ((HeaderViewHolder) holder).bind(headerItem);
   }

   @NonNull
   @Override
   public ViewGroup getGridView(GridHeader key, @NonNull ViewGroup parent) {
       switch (key.getKey()) {
           case ACTOR:
               return (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.actor_view, parent, false);
           case GENRE:
               return (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.genre_view, parent, false);
           case MOVIE:
               return (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_view, parent, false);
           case STUDIO:
               return (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.studio_view, parent, false);
           case DIRECTOR:
               return (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.director_view, parent, false);
           default:
               throw new IllegalArgumentException("Unknown Header Key Value");
       }
   }

   @NonNull
   @Override
   public GridCellViewHolder getGridViewHolder(GridHeader key, @NonNull ViewGroup parent) {
       switch (key.getKey()) {
           case ACTOR:
               return new ActorViewHolder(parent);
           case GENRE:
               return new GenreViewHolder(parent);
           case MOVIE:
               return new MovieViewHolder(parent);
           case STUDIO:
               return new StudioViewHolder(parent);
           case DIRECTOR:
               return new DirectorViewHolder(parent);
           default:
               throw new IllegalArgumentException("Unknown Header Key Value");
       }
   }
}

The GridHeader class serves as a Plain Old Java Object (POJO) holding information about grid section headers.

2. Implement ViewHolder for Each Grid Cell

Next, implement the GridCellViewHolder interface to efficiently bind data to each type of grid cell.


public abstract class GridCellViewHolder<T> extends RecyclerView.ViewHolder {
    // Implement the bind method to efficiently bind data to the ViewHolder.
}

For instance, create a specialized ViewHolder for actor cells:


public class ActorViewHolder extends GridCellViewHolder<Actor> {
    private final Context context;
    private final TextView name;
    private final ImageView image;

    public ActorViewHolder(@NonNull View itemView) {
        super(itemView);
        this.context = itemView.getContext();
        this.name = itemView.findViewById(R.id.name);
        this.image = itemView.findViewById(R.id.image);
    }

    @Override
    public void bind(Actor actor) {
        this.name.setText(actor.getName());
        this.image.setImageDrawable(getDrawableFromName(context, actor.getImage()));
    }
}

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文