2

I know there is a way to scroll to the bottom of a RecyclerView from Activities, but is there any way I can scroll to the bottom of a RecyclerView from the Adapter? I want to scroll to the bottom when an EditText is focused upon.

Here is some of my Adapter where I would like to scroll to the bottom of the RecyclerView.

@Override public void onBindViewHolder(final ViewHolder holder, final int i) { final int position = holder.getAdapterPosition(); updatePrefs(); holder.editTextListener.updatePosition(position); holder.studentText.setHint(mDataset.get(position)); holder.studentNumber.setText(position + 1 + "."); if (position != 0) { holder.studentText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { holder.studentText.setHint(v.getContext().getResources() .getString(R.string.prompt_student_name)); setDeleteListener(holder.studentDelete, position); holder.studentText.setOnFocusChangeListener(null); mDataset.add(v.getContext().getResources() .getString(R.string.prompt_new_student_name)); notifyItemInserted(position + 1); //SCROLL TO BOTTOM OF RECYCLERVIEW } } }); } //Check to make sure they're not deleting their only way of adding a new EditText if (!holder.studentText.getHint().equals(holder.studentText.getContext().getResources() .getString(R.string.prompt_new_student_name))) { setDeleteListener(holder.studentDelete, position); } } 
3
  • 1
    Passing the context to the adapter and then using ((Activity) context).findViewById(R.id.recyclerView) and scroll method over it didn't work? Commented Mar 23, 2016 at 0:05
  • That's brilliant. Thanks for the tip. Commented Mar 23, 2016 at 0:59
  • For the sake of completeness I added it as an answer, hoping it will also give more visibility Commented Mar 23, 2016 at 19:56

1 Answer 1

1

First you get a reference of the Context inside your layout, most simple way is to pass it through the constructor:

private Context context; public MyAdapter(ArrayList<String> data, Context context) { this.data = data; this.context= context; } 

Inside your callback, just get the reference to the RecyclerView from the Context you passed before

public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { ... RecyclerView recyclerView = ((Activity) this.context).findViewById(R.id.recyclerView); ... use RecyclerView for your purpose } } 

Actually you could have fetch the reference directly of the RecyclerView out of your adapter and then passing the RecyclerView reference instead of the Context. I don't see any difference, but usually I use the above method since I may need to update other UI elements and it's more handy to do in this way.

Sign up to request clarification or add additional context in comments.

2 Comments

Most of the time you can get a 'fresher' Context from within your method by doing: holder.itemView.getContext() So you avoid troubles when using your RecyclerView inside a Fragment.
You forgot to parse returned view from findViewById(R.id.recyclerView) to RecyclerView. It should done like this RecyclerView recyclerView = (RecyclerView) ((Activity) this.context).findViewById(R.id.recyclerView);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.