• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

RecyclerView Scroll Listener

umtblbl

Member
Mar 6, 2019
50
5
Hi friends I will hide the header contained in the recyclerView according to the scrolling level and make it visible. But I couldn't find a listener I could use for this.
The setOnScrollChangeListener method always returns 0.0.
The addOnScrollListener method returns acceleration-sensitive dx and dy values.
I think I need to process the RecyclerView according to the alignment of the y values. Any suggestions?
 
Please show us how you added the header contained in the recyclerView.

Code:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    LayoutInflater inflater;
    ArrayList<String> list;

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_LIST = 1;


    public RecyclerViewAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        list = new ArrayList<String>();
        list.add("İstanbul");
        list.add("Ankara");
        list.add("Erzincan");
        
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view;


        if (viewType == TYPE_HEADER) {
            view = inflater.inflate(R.layout.header, parent, false);
            return new ViewHolder(view, viewType);
        } else if (viewType == TYPE_LIST) {
            view = inflater.inflate(R.layout.item, parent, false);
            return new ViewHolder(view, viewType);
        } else {
            return null;
        }


    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if(holder.view_type == TYPE_LIST) {

            holder.txtItem.setText(list.get(--position));
        }
        else if(holder.view_type == TYPE_HEADER) {

        }

    }

    @Override
    public int getItemCount() {
        return list.size() + 1;
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        TextView txtItem;
        int view_type;

        public ViewHolder(View itemView, int viewType) {
            super(itemView);

            if (viewType == TYPE_LIST) {
                txtItem = (TextView) itemView.findViewById(R.id.textView);
                view_type = 1;
            } else if (viewType == TYPE_HEADER) {
                view_type = 0;
            }


        }

    }


    @Override
    public int getItemViewType(int position) {

        if (position == 0)
            return TYPE_HEADER;
        return TYPE_LIST;
    }
}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones