RecyclerView - view at a specific position

I have activity with RecyclerView and ImageView . I use RecyclerView to display a list of images horizontally. When I click on an image in RecyclerView , a larger snapshot of the image should be displayed in the ImageView in action. So far, everything is working fine.

Now there are two more ImageButtons in the activity: imageButton_left and imageButton_right . When I click imageButton_left , the image in ImageView should turn left, and also, the sketch in RecyclerView should reflect this change. Similar to imageButton_right .

I can rotate ImageView . But how can I rotate a thumbnail in RecyclerView ? How can I get ViewHolder ImageView ?

the code:

XML action:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="vertical"> <ImageView android:id="@+id/original_image" android:layout_width="200dp" android:layout_height="200dp" android:scaleType="fitXY" android:src="@drawable/image_not_available_2" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_horizontal" android:orientation="horizontal"> <ImageButton android:id="@+id/imageButton_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:background="@drawable/rotate_left_icon" /> <ImageButton android:id="@+id/imageButton_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rotate_right_icon" /> </LinearLayout> </LinearLayout> </LinearLayout> 

My activity code:

 public class SecondActivity extends AppCompatActivity implements IRecyclerViewClickListener { RecyclerView mRecyclerView; LinearLayoutManager mLayoutManager; RecyclerViewAdapter mRecyclerViewAdapter; List<String> urls = new ArrayList<String>(); ImageView mOriginalImageView; ImageButton mLeftRotate, mRightRotate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); urls.clear(); mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview); mLayoutManager = new LinearLayoutManager(this, android.support.v7.widget.LinearLayoutManager.HORIZONTAL, false); mLayoutManager.setOrientation(android.support.v7.widget.LinearLayoutManager.HORIZONTAL); mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerViewAdapter = new RecyclerViewAdapter(this, urls); mRecyclerView.setAdapter(mRecyclerViewAdapter); mOriginalImageView = (ImageView) findViewById(R.id.original_image); mLeftRotate = (ImageButton) findViewById(R.id.imageButton_left); mLeftRotate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mOriginalImageView.setRotation(mOriginalImageView.getRotation() - 90); } }); mRightRotate = (ImageButton) findViewById(R.id.imageButton_right); mRightRotate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mOriginalImageView.setRotation(mOriginalImageView.getRotation() + 90); } }); Intent intent = getIntent(); if (intent != null) { String portfolio = intent.getStringExtra("portfolio"); try { JSONArray jsonArray = new JSONArray(portfolio); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String url = jsonObject.getString("url"); urls.add(url); } Log.d(Const.DEBUG, "URLs: " + urls.toString()); mRecyclerViewAdapter.notifyDataSetChanged(); } catch (Exception e) { e.printStackTrace(); } } } @Override public void onItemClick(int position) { Picasso.with(this).load(urls.get(position)).into(mOriginalImageView); } } 

My custom adapter for RecyclerView:

 public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { Context context; List<String> mUrls = new ArrayList<String>(); IRecyclerViewClickListener mIRecyclerViewClickListener; public int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public RecyclerViewAdapter(Context context, List<String> urls) { this.context = context; this.mUrls.clear(); this.mUrls = urls; Log.d(Const.DEBUG, "Urls Size: " + urls.size()); Log.d(Const.DEBUG, urls.toString()); if (context instanceof IRecyclerViewClickListener) mIRecyclerViewClickListener = (IRecyclerViewClickListener) context; else Log.d(Const.DEBUG, "Implement IRecyclerViewClickListener in Activity"); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_horizontal_recyclerview, parent, false); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { Picasso.with(context).load(mUrls.get(position)).into(holder.mImageView); } @Override public int getItemCount() { return mUrls.size(); } public void rotateThumbnail() { } public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public ImageView mImageView; public View v; public ViewHolder(View v) { super(v); v.setTag(getAdapterPosition()); v.setOnClickListener(this); this.mImageView = (ImageView) v.findViewById(R.id.image); } @Override public void onClick(View v) { this.v = v; mIRecyclerViewClickListener.onItemClick(getAdapterPosition()); } } } 
+116
java android position widget android-recyclerview
Nov 18 '15 at 15:43
source share
10 answers

Suppose you use LinearLayoutManager to display a list. It has a nice findViewByPosition method, which

Finds a view that represents the given position of the adapter.

All you need is the adapter position of the element you are interested in.

edit : as Paul Whitachek noted in the comments, findViewByPosition is a LayoutManager method, so it will work with all LayoutManager layouts (i.e. StaggeredGridLayoutManager , etc.)

+127
Nov 19 '15 at 8:44
source share

This is what you are looking for .

I also had this problem. And, like you, the answer is very difficult to find. But there is an easy way to get ViewHolder from a specific position (something that you are likely to do a lot in the adapter).

myRecyclerView.findViewHolderForAdapterPosition(pos);

NOTE. If the view has been redesigned, it will return null . Thanks to Michael for quickly catching my important omission.

+206
Jul 28 '16 at 15:26
source share

If you want a View , be sure to access the itemView property for the ViewHolder as follows: myRecyclerView.findViewHolderForAdapterPosition(pos).itemView;

+20
Dec 30 '16 at 18:24
source share

You can use as

recyclerViewInstance.findViewHolderForAdapterPosition(adapterPosition) and recyclerViewInstance.findViewHolderForLayoutPosition(layoutPosition) . Ensure that the RecyclerView uses two types of positions

Adapter Position: The position of the element in the adapter. This is the position from the point of view of the Adapter. Layout Position: The position of the element in the last layout calculation. This is the position in terms of the LayoutManager. You should use getAdapterPosition() for findViewHolderForAdapterPosition(adapterPosition) and getLayoutPosition() for findViewHolderForLayoutPosition(layoutPosition) .

Take a member variable to store the previously selected item position in the recyclerview adapter and another member variable to check whether the user clicks for the first time or not.

Sample code and screenshots are included for more information at the bottom.

 public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerList = null; private RecyclerAdapter adapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerList = (RecyclerView) findViewById(R.id.recyclerList); } @Override protected void onStart() { RecyclerView.LayoutManager layoutManager = null; String[] daysArray = new String[15]; String[] datesArray = new String[15]; super.onStart(); for (int i = 0; i < daysArray.length; i++){ daysArray[i] = "Sunday"; datesArray[i] = "12 Feb 2017"; } adapter = new RecyclerAdapter(mRecyclerList, daysArray, datesArray); layoutManager = new LinearLayoutManager(MainActivity.this); mRecyclerList.setAdapter(adapter); mRecyclerList.setLayoutManager(layoutManager); } } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyCardViewHolder>{ private final String TAG = "RecyclerAdapter"; private Context mContext = null; private TextView mDaysTxt = null, mDateTxt = null; private LinearLayout mDateContainerLayout = null; private String[] daysArray = null, datesArray = null; private RecyclerView mRecyclerList = null; private int previousPosition = 0; private boolean flagFirstItemSelected = false; public RecyclerAdapter(RecyclerView mRecyclerList, String[] daysArray, String[] datesArray){ this.mRecyclerList = mRecyclerList; this.daysArray = daysArray; this.datesArray = datesArray; } @Override public MyCardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater layoutInflater = null; View view = null; MyCardViewHolder cardViewHolder = null; mContext = parent.getContext(); layoutInflater = LayoutInflater.from(mContext); view = layoutInflater.inflate(R.layout.date_card_row, parent, false); cardViewHolder = new MyCardViewHolder(view); return cardViewHolder; } @Override public void onBindViewHolder(MyCardViewHolder holder, final int position) { mDaysTxt = holder.mDaysTxt; mDateTxt = holder.mDateTxt; mDateContainerLayout = holder.mDateContainerLayout; mDaysTxt.setText(daysArray[position]); mDateTxt.setText(datesArray[position]); if (!flagFirstItemSelected){ mDateContainerLayout.setBackgroundColor(Color.GREEN); flagFirstItemSelected = true; }else { mDateContainerLayout.setBackground(null); } } @Override public int getItemCount() { return daysArray.length; } class MyCardViewHolder extends RecyclerView.ViewHolder{ TextView mDaysTxt = null, mDateTxt = null; LinearLayout mDateContainerLayout = null; LinearLayout linearLayout = null; View view = null; MyCardViewHolder myCardViewHolder = null; public MyCardViewHolder(View itemView) { super(itemView); mDaysTxt = (TextView) itemView.findViewById(R.id.daysTxt); mDateTxt = (TextView) itemView.findViewById(R.id.dateTxt); mDateContainerLayout = (LinearLayout) itemView.findViewById(R.id.dateContainerLayout); mDateContainerLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LinearLayout linearLayout = null; View view = null; if (getAdapterPosition() == previousPosition){ view = mRecyclerList.findViewHolderForAdapterPosition(previousPosition).itemView; linearLayout = (LinearLayout) view.findViewById(R.id.dateContainerLayout); linearLayout.setBackgroundColor(Color.GREEN); previousPosition = getAdapterPosition(); }else { view = mRecyclerList.findViewHolderForAdapterPosition(previousPosition).itemView; linearLayout = (LinearLayout) view.findViewById(R.id.dateContainerLayout); linearLayout.setBackground(null); view = mRecyclerList.findViewHolderForAdapterPosition(getAdapterPosition()).itemView; linearLayout = (LinearLayout) view.findViewById(R.id.dateContainerLayout); linearLayout.setBackgroundColor(Color.GREEN); previousPosition = getAdapterPosition(); } } }); } } 

} first element selected second element selected and previously selected item becomes unselected fifth element selected and previously selected item becomes unselected

+11
Feb 18 '17 at 1:21
source share

You can create an ArrayList for the ViewHolder:

  ArrayList<MyViewHolder> myViewHolders = new ArrayList<>(); ArrayList<MyViewHolder> myViewHolders2 = new ArrayList<>(); 

and all ViewHolder (s) repositories in the list, for example:

  @Override public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) { final String str = arrayList.get(position); myViewHolders.add(position,holder); } 

and add / remove another ViewHolder in ArrayList as per your requirement.

+5
Mar 29 '18 at 10:42
source share

You can get an idea of ​​a specific position in a reviewer using the following

 int position = 2; RecyclerView.ViewHolder viewHolder = recyclerview.findViewHolderForItemId(position); View view = viewHolder.itemView; ImageView imageView = (ImageView)view.findViewById(R.id.imageView); 
+4
Oct 23 '18 at 22:59
source share

Keep in mind that you need to wait until the adapter is added to the list, then you can try to get an idea by position

 final int i = 0; recyclerView.setAdapter(adapter); recyclerView.post(new Runnable() { @Override public void run() { View view = recyclerView.getLayoutManager().findViewByPosition(i); } }); 
+3
Feb 18 '19 at 16:45
source share

You can simply use the findViewHolderForAdapterPosition "method to represent the recirculator, and you will get a viewHolder from this, and then insert this view holder into your adapter viewfinder to have direct access to the views of your widgets

Below is sample code for Kotlin

  val viewHolder = recyclerView.findViewHolderForAdapterPosition(position) val textview=(viewHolder as YourViewHolder).yourTextView 
+3
Jul 30 '19 at 11:11
source share

You can also do this, it will help you when you want to change the view after clicking on the element of the position of re-viewing

 @Override public void onClick(View view, int position) { View v = rv_notifications.getChildViewHolder(view).itemView; TextView content = v.findViewById(R.id.tv_content); content.setText("Helloo"); } 
+1
Apr 11 '18 at 13:08
source share

To get a specific view from the list of processor views, OR show an error while editing the text of the processor view.

 private void popupErrorMessageAtPosition(int itemPosition) { RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(itemPosition); View view = viewHolder.itemView; EditText etDesc = (EditText) view.findViewById(R.id.et_description); etDesc.setError("Error message here !"); } 
0
Aug 19 '19 at 11:45
source share



All Articles