私は、recyclerviewが単一のアイテムだけを表示しているという奇妙なエラーに直面しています。 私は以下の私のrecyclerviewアダプターのコードです..... recylerviewでデータバインディングを使用していますなぜrecyclerviewはゼロ位置値のみを表示しますか?
ここpublic class ImageSetAdapter extends RecyclerView.Adapter<ImageSetAdapter.PictureItemHolder> {
List<HouseModelSet> data;
public ImageSetAdapter(List<HouseModelSet> data) {
this.data = data;
}
@Override
public PictureItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.item_home_picture,parent,false);
/*ItemHomePictureBinding itemHomePictureBinding = DataBindingUtil.inflate(inflater, R.layout.item_home_picture, parent, false);*/
ItemHomePictureBinding binding = ItemHomePictureBinding.inflate(inflater,parent,false);
return new PictureItemHolder(binding.getRoot());
}
@Override
public void onBindViewHolder(PictureItemHolder holder, int position) {
HouseModel hm1 = data.get(position).getHouseModels().get(0);
HouseModel hm2 = data.get(position).getHouseModels().get(1);
HouseModel hm3 = data.get(position).getHouseModels().get(2);
HouseModel hm4 = data.get(position).getHouseModels().get(3);
HouseModel hm5 = data.get(position).getHouseModels().get(4);
holder.binding.house1.setHouse(hm1);
holder.binding.house2.setHouse(hm2);
holder.binding.house3.setHouse(hm3);
holder.binding.house4.setHouse(hm4);
holder.binding.house5.setHouse(hm5);
}
@BindingAdapter("android:src")
public static void loadImage(ImageView imageView, int v){
Glide.with(imageView.getContext()).load(v).into(imageView);
}
@Override
public int getItemCount() {
return data.size();
}
public class PictureItemHolder extends RecyclerView.ViewHolder{
ItemHomePictureBinding binding;
public PictureItemHolder(View itemView) {
super(itemView);
binding = DataBindingUtil.bind(itemView);
}
}
}
はAndroidがここ
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
ImageSetAdapter mImageSetAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
//--------Set and Get Tool Bar-------//
setSupportActionBar(binding.appbar);
getSupportActionBar().setTitle("");
mImageSetAdapter = new ImageSetAdapter(getHousesSet());
binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
binding.recyclerView.setAdapter(mImageSetAdapter);
}
private List<HouseModelSet> getHousesSet() {
List<HouseModelSet> list = new ArrayList<>();
List<HouseModel> houses = new ArrayList<>();
for (int i = 0; i < 4; i++) {
HouseModel houseModel1 = new HouseModel("City " + i, 1.34f + i , R.drawable.home8);
HouseModel houseModel2 = new HouseModel("Sialkot " + i, 2.43f + i,R.drawable.home8);
HouseModel houseModel3 = new HouseModel("Sialkot " + i, 3.43f + i,R.drawable.home8);
HouseModel houseModel4 = new HouseModel("Sialkot " + i, 4.43f + i,R.drawable.home8);
HouseModel houseModel5 = new HouseModel("Sialkot " + i, 5.43f + i,R.drawable.home8);
houses.add(houseModel1);
houses.add(houseModel2);
houses.add(houseModel3);
houses.add(houseModel4);
houses.add(houseModel5);
}
list.add(new HouseModelSet(houses));
return list;
}
MainActivity
ためのコードであることは私のxmlレイアウトである<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="170dp"
android:layout_weight="2"
android:background="@drawable/home8"
tools:showIn="@layout/item_home_picture">
<ImageView
android:id="@+id/img1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@{house.image}" />
<TextView
android:id="@+id/price"
style="@style/item_image_text_price"
android:layout_above="@+id/city"
android:text="@{String.valueOf(house.price)}"
android:textSize="20sp"/>
<TextView
android:id="@+id/city"
android:layout_alignParentBottom="true"
style="@style/item_image_text_city"
android:text="@{house.city}"
android:textSize="15sp" />
</RelativeLayout>
ここに私のコンテナレイアウトは
<LinearLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:id="@+id/cataorytxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="New Apartments"
android:textSize="20sp"
android:textStyle="bold" />
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Sialkot"
android:textSize="14sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="2"
android:layout_marginRight="2dp">
<include android:id="@+id/house1"
layout="@layout/house_container_item" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="2"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp">
<include android:id="@+id/house2"
layout="@layout/house_container_item" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:weightSum="4">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginRight="2dp">
<include android:id="@+id/house3"
layout="@layout/house_container_item" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginLeft="2dp">
<include android:id="@+id/house4"
layout="@layout/house_container_item" />
</LinearLayout>
</LinearLayout>
<include android:id="@+id/house5"
layout="@layout/house_container_item" />
</LinearLayout>
</LinearLayout>
getHousesSetは1のアイテムのサイズを返します – Ramesh
ありがとうRameshあなたはただ私の問題を解決します...... – Ahsan
もう1つ質問Ramesh ....私はちょうどlist.add(新しいHouseModelSet(住宅))を置く;私の4回を与えるが、ITはi = 0の値を示す毎回..........?あなたが私の言葉を理解していない場合は、私に教えてください – Ahsan