Sticky title / element in recyclerview

I have successfully implemented sections in a recycler view, as shown in the screenshot with two different types of views. How to implement a sticky header in a recycler view. I want the “A” to stick to the top until the user scrolls up, in which case the next element that will be attached to the top will be “B”. How to do it?

Recycler view

+4
source share
1 answer

. Emil Sjölander StickyListHeaders - ( ), .

, , StickyListHeaders:

  • (, ). , item_cheese.xml item_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 , .

.

+2

All Articles