我们的问题是通过使用数据绑定单击[回收者视图]的任何单个项目来更新回收器视图中的多个图像,但最初我们只能使用ImageAdapter的xml中的数据绑定在某些位置更新图像。但是根据我们在android开发者网站上搜索,建议使用Recycler视图而不是我们最初的方法。before clicking any image
上述方法是在图像适配器中使用数据绑定,如下所示。
图像适配器
package tene.com.darxstudios;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
public class ImageAdapter extends BaseAdapter {
private static final String TAG = ImageAdapter.class.getName() ;
Context mContext;
List<Integer> cropsList1 = new ArrayList<Integer>();
List<Integer> cropsList2 = new ArrayList<Integer>();
User user0 = new User(),user1 = new User(),user2 = new User(),user3 = new User(),user4 = new User();
public ImageAdapter(Context mContext,List<Integer> cropsList1,List<Integer> cropsList2){
this.mContext = mContext;
this.cropsList1 = cropsList1;
this.cropsList2 = cropsList2;
}
@Override
public int getCount() {
return cropsList1.size();
}
@Override
public Object getItem(int position) {
return cropsList1.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// View view = inflater.inflate(R.layout.item_view, null);
final User user = new User();
user.setFirstName(cropsList1.get(position));
tene.com.darxstudios.ItemViewBinding binding = DataBindingUtil.inflate(inflater,R.layout.item_view, parent, false);
binding.setUser(user);
View view = binding.getRoot();
ImageView imageView = (ImageView) view.findViewById(R.id.image);
// if(position == 0){
// user0 = user;
// }else if(position == 1){
// user1 = user;
// }else if(position == 2){
// user2 = user;
// }else if(position == 3){
// user3 = user;
// }else if(position == 4){
// user4 = user;
// }
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
user.setFirstName(cropsList2.get(position));
// user0.setFirstName(cropsList2.get(0));
// user1.setFirstName(cropsList2.get(1));
// user2.setFirstName(cropsList2.get(2));
// user3.setFirstName(cropsList2.get(3));
// user4.setFirstName(cropsList2.get(4));
}
});
return view;
}
}
item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:bind="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<data class=".ItemViewBinding">
<variable name="user" type="tene.com.darxstudios.User"/>
</data>
<LinearLayout
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/image"
firstName='@{user.firstName}'/>
</LinearLayout>
</layout>
我们需要使用带有数据绑定功能的循环器视图,只需单击任何图像即可更新所有图像。
答案 0 :(得分:0)
在onBindViewHolder中的onClickListener中放置用于更改图像的必要逻辑。请参阅recyclerview教程以了解如何实现它。以下代码仅处理onClickListener。
public class NumReelAdapter extends RecyclerView.Adapter<NumReelAdapter.recyclerViewHolder> {
List<NumReel> data = Collections.emptyList();
Context ctx;
int selectedPosition = 0;
onBindViewHolder
public void onBindViewHolder(final recyclerViewHolder holder, final int position) {
final NumReel current = data.get(position);
if(selectedPosition == position){
// here highlighting the background. And set the inside color to green
holder.itemView.setBackground(whatever_image_id);
} else {
//reset the color to white
holder.itemView.setBackground(whatever_it_had_before);
}
holder.vItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//refresh the adapter
notifyItemChanged(selectedPosition);
//change clicked position
selectedPosition = position;
//reset the adapter with new position highlighted
notifyItemChanged(selectedPosition);
if (numClickListener != null) {
String vTag = v.getTag().toString();
//numClickListener.repsClick(v, position); alternate method where data can't be sent
numClickListener.numClick(vTag, current.num);
}
}
});
在这里,您可以检查所点击图像的任何特定属性,然后进行后续更改。因此,在您的情况下,您必须检查图像的名称。因此,如果点击鹰嘴豆,则背景变成稻田。