在recyclerview

时间:2015-08-27 18:13:40

标签: android list view header sticky

我已成功在回收站视图中实现了部分,如屏幕截图所示,其中包含两种不同类型的视图。 如何在回收站视图中实现粘性标头。 我希望'A'坚持到顶部,直到用户向上滚动,在这种情况下,下一个坚持顶部的项目将是'B'。 我该怎么做?

Recycler View

1 个答案:

答案 0 :(得分:2)

你肯定会想要使用一个库。EmilSjölander的StickyListHeaders是一个不错的选择(我在几个项目中使用它),但也有其他库出来那里。

不知道您的代码的具体细节,这里是一个粗略的草图,说明如何使用 StickyListHeaders

  • 为您的商品和标题创建一个布局(您可能已经有了)。我们假设这些被称为item_cheese.xmlitem_header.xml

  • 将您的ListView替换为布局文件中的StickyListHeadersListView

  • 为您的奶酪集合创建一个自定义适配器(您可能已经拥有它)并使其实现StickyListHeadersAdapter

界面只添加两种方法:

public interface StickyListHeadersAdapter extends ListAdapter {
    View getHeaderView(int position, View view, ViewGroup parent);
    long getHeaderId(int position);
}

适配器的完整实现如下所示。它正在使用ViewHolders用于项目和标题(以回收视图,就像您要求的那样):

public class CheeseAdapter extends BaseAdapter implements StickyListHeadersAdapter {

    private List<String> cheeses;
    private final LayoutInflater inflater;

    public CheeseAdapter(Context context, List<String> cheeses) {
        // Assuming cheeses are already alphabetically sorted at this point
        this.cheeses = cheeses;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return (cheeses != null) ? cheeses.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return cheeses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        CheeseViewHolder holder;
        if (view != null)
            holder = (CheeseViewHolder) view.getTag();
        else {
            view = inflater.inflate(R.layout.item_cheese, parent, false);
            holder = new CheeseViewHolder(view);
            view.setTag(holder);
        }
        holder.decorate(cheeses.get(position));
        return view;
    }

    @Override
    public long getHeaderId(int position) {
        // Return the first char of each item as its header ID
        return cheeses.get(position).charAt(0);
    }

    @Override
    public View getHeaderView(int position, View view, ViewGroup parent) {
        HeaderViewHolder holder;
        if (view != null)
            holder = (HeaderViewHolder) view.getTag();
        else {
            view = inflater.inflate(R.layout.item_header, parent, false);
            holder = new HeaderViewHolder(view);
            view.setTag(holder);
        }
        holder.decorate(cheeses.get(position));
        return view;
    }

    public class CheeseViewHolder {

        TextView tvCheeseName;

        public CheeseViewHolder(View view) {
            tvCheeseName = (TextView) view.findViewById(R.id.tvCheeseName);
        }

        public void decorate(String cheeseName) {
            if (cheeseName == null) return;
            tvCheeseName.setText(cheeseName);
        }
    }

    public class HeaderViewHolder {

        TextView tvHeader;

        public CheeseViewHolder(View view) {
            tvHeader = (TextView) view.findViewById(R.id.tvHeader);
        }

        public void decorate(String cheeseName) {
            if (cheeseName == null || cheeseName.isEmpty()) return;
            tvHeader.setText(cheeseName.toUpperCase().charAt(0));
        }
    }
}
  • 在“活动”或“片段”中使用自定义适配器。 ScrollView将包含带有粘性标题的字母部分,并且表现为just as advertised

这很简单。