我的活动包含RecyclerView
和ImageView
。我正在使用RecyclerView
水平显示图像列表。当我点击RecyclerView
中的图片时,活动中的ImageView
应该会显示更大的图片。到目前为止一切正常。
现在活动中还有两个ImageButtons
:imageButton_left
和imageButton_right
。当我点击imageButton_left
时,ImageView
中的图片应向左转,同时RecyclerView
中的缩略图也应反映此更改。与imageButton_right
类似。
我可以旋转ImageView
。但是,如何在RecyclerView
中旋转缩略图?如何获得ViewHolder
的{{1}}?
代码:
活动XML:
ImageView
我的活动代码:
<?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>
我的RecyclerView自定义适配器:
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);
}
}
答案 0 :(得分:190)
这是您正在寻找的。
我也有这个问题。和你一样,答案很难找到。但是有一种简单的方法可以让ViewHolder从特定的位置获得(你可能会在适配器中做很多事情)。
myRecyclerView.findViewHolderForAdapterPosition(pos);
注意:如果视图已被回收,则会返回null
。感谢迈克尔迅速抓住我的重要遗漏。
答案 1 :(得分:109)
我想您使用LinearLayoutManager
来显示列表。它有一个名为findViewByPosition
的好方法
查找表示给定适配器位置的视图。
您需要的只是您感兴趣的项目的适配器位置。
编辑:正如评论中 Paul Woitaschek 所述,findViewByPosition
是LayoutManager
的一种方法,因此它适用于所有LayoutManagers(即StaggeredGridLayoutManager
等。)
答案 2 :(得分:18)
如果您需要View
,请务必访问ViewHolder的itemView
属性,如下所示:myRecyclerView.findViewHolderForAdapterPosition(pos).itemView;
答案 3 :(得分:10)
您可以同时使用
recyclerViewInstance.findViewHolderForAdapterPosition(adapterPosition)
和
recyclerViewInstance.findViewHolderForLayoutPosition(layoutPosition)
。
确保RecyclerView视图使用两种类型的位置
适配器位置:项目在适配器中的位置。这是从Adapter的角度来看的位置。
布局位置:项目在最新布局计算中的位置。这是从LayoutManager的角度来看的位置。
您应getAdapterPosition()
使用findViewHolderForAdapterPosition(adapterPosition)
,getLayoutPosition()
使用findViewHolderForLayoutPosition(layoutPosition)
。
获取一个成员变量来保存recyclelerview适配器和其他成员变量中先前选择的项目位置,以检查用户是否第一次点击。
附上示例代码和屏幕截图,以获取更多信息。
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();
}
}
});
}
}
答案 4 :(得分:5)
您可以制作ViewHolder的ArrayList:
ArrayList<MyViewHolder> myViewHolders = new ArrayList<>();
ArrayList<MyViewHolder> myViewHolders2 = new ArrayList<>();
并且,列表中的所有商店ViewHolder都包含:
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
final String str = arrayList.get(position);
myViewHolders.add(position,holder);
}
并根据您的要求在ArrayList中添加/删除其他ViewHolder。
答案 5 :(得分:5)
您可以简单地使用回收器视图的“ findViewHolderForAdapterPosition ”方法,您将从中获得一个viewHolder对象,然后将该视图持有者转换为适配器视图持有者,以便可以直接访问您的视图持有者的视图
以下是科特琳的示例代码
val viewHolder = recyclerView.findViewHolderForAdapterPosition(position)
val textview=(viewHolder as YourViewHolder).yourTextView
答案 6 :(得分:4)
要从recyclerView的特定位置获取视图,您需要在recyclerView对象上调用findViewHolderForAdapterPosition(position),该对象将返回RecyclerView.ViewHolder
从返回的带有itemView的RecyclerView.ViewHolder对象中,您可以访问该特定位置的所有视图
RecyclerView.ViewHolder rv_view = recyclerView.findViewHolderForAdapterPosition(position);
ImageView iv_wish = rv_view.itemView.findViewById(R.id.iv_item);
答案 7 :(得分:3)
您可以使用以下
在recyclerview上获取特定位置的视图int position = 2;
RecyclerView.ViewHolder viewHolder = recyclerview.findViewHolderForItemId(position);
View view = viewHolder.itemView;
ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
答案 8 :(得分:1)
您也可以这样做,当您想要在点击Recyclerview位置项后修改视图时,这将有所帮助
let fileURL = URL(fileURLWithPath:"/")
do {
let values = try fileURL.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey])
if let capacity = values.volumeAvailableCapacityForImportantUsage {
print("Available capacity for important usage: \(capacity)")
} else {
print("Capacity is unavailable")
}
} catch {
print("Error retrieving capacity: \(error.localizedDescription)")
}
答案 9 :(得分:1)
请记住,您必须等待适配器添加到列表中,然后才能尝试按位置查看
final int i = 0;
recyclerView.setAdapter(adapter);
recyclerView.post(new Runnable() {
@Override
public void run() {
View view = recyclerView.getLayoutManager().findViewByPosition(i);
}
});
答案 10 :(得分:1)
要从回收者视图列表中获取特定视图,或在回收者视图的edittext中显示错误。
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 !");
}
答案 11 :(得分:0)
如果你们每次尝试获取具有任何int位置的视图时都具有null,请尝试像这样向适配器添加新的构造函数参数:
class RecyclerViewTableroAdapter(
private val fichas: Array<MFicha?>,
private val activity: View.OnClickListener,
private val indicesGanadores:MutableList<Int>
) : RecyclerView.Adapter<RecyclerViewTableroAdapter.ViewHolder>() {
//CODE
}
如果我赢了比赛,我添加了indexGanadores来为我的Cardview背景着色。
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
//CODE
if(indicesGanadores.contains(position)){
holder.cardViewFicha.setCardBackgroundColor((activity as MainActivity).resources.getColor(R.color.DarkGreen))
}
//MORE CODE
}
如果我不必为背景着色,我只是发送一个空的可变列表,如下所示:
binding.recyclerViewMain.adapter = RecyclerViewTableroAdapter(fichasTablero, this@MainActivity, mutableListOf<Int>())
编码愉快!...
答案 12 :(得分:0)
您可以在下面使用
mRecyclerview.getChildAt(pos);
答案 13 :(得分:0)
RecyclerView - 获取特定位置的视图。 //int是=位置; 这行代码对我有用。
@Override
public void onBindViewHolder(@NonNull @org.jetbrains.annotations.NotNull QuizAdapter.MyViewHolder holder, int position) {
final QuizModel tmodel = modellist.get(position);
holder.tv_question.setText(tmodel.getQuestion());
holder.tv_option_One.setText(tmodel.getOptionA());
holder.tv_option_two.setText(tmodel.getOptionB());
holder.tv_option_three.setText(tmodel.getOptionC());
holder.tv_option_four.setText(tmodel.getOptionD());
holder.tv_option_five.setText(tmodel.getOptionE());
holder.ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int is = position;
Toast.makeText(v.getContext(), "count" + is, Toast.LENGTH_LONG).show();
Intent i = new Intent(context, DetailsQuizActivity.class);
i.putExtra("question", tmodel.getQuestion());
i.putExtra("option_a", tmodel.getOptionA());
i.putExtra("option_b", tmodel.getOptionB());
i.putExtra("option_c", tmodel.getOptionC());
i.putExtra("option_d", tmodel.getOptionD());
i.putExtra("option_e", tmodel.getOptionE());
i.putExtra("answer", tmodel.getOptionAnswer());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
});
答案 14 :(得分:-1)
recyclerView.post(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
View viewTemp = recyclerView.getLayoutManager().findViewByPosition(i);
CardView cardView = (CardView) viewTemp.findViewById(R.id.cardView);
//TO DO
}
}
});