私たちの問題は、データバインディングを使用して[recycler view]のいずれかのアイテムをクリックすることによってリサイクルビューで複数の画像を更新することです。 ImageAdapterのxml.Butでデータバインディングを使用しているいくつかのポジションでは、アンドロイドデベロッパーサイトでの検索に基づいて、初期アプローチではなくリサイクルビューを使用することを提案しました。 before clicking any imageandroidのRecyclerビューを使用して複数の画像を同時に2つのデータセットにバインドする
上記のアプローチは、以下に示されている画像・アダプタでデータバインディングを使用することです。
<?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>
item_view.xml
イメージ・アダプタ
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;
}
}
我々はアンドロイドにデータバインディングとリサイクルのビューを使用して、任意の画像を1回クリックするだけですべての画像を更新する必要があります。