您好,我在片段视图中遇到了一个问题,即使形状发生了变化,每个片段都具有相同的RecyclerView图片,如下所示,每个片段中的结果相同:
因此,该图像仍与导入片段中的图像相同,并且无法删除事务,但是如果给定背景(如图片3),则该图像可能会丢失但无法使用,只能使用前一个图像进行堆叠。 / p>
这是我犯下的全部错误:
MainActivity.xml
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawer;
private List<Tas> lstTas;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstTas = new ArrayList<>();
lstTas.add(new Tas("Tas Serbaguna Sedang", "Kategori : Tas Lokal", "Deskripsi Tas : Tas serbaguna yang bisa menyimpan berbagai alat make up maupun perlengkapan lainnya", "Rp 70.000", R.drawable.greybag));
lstTas.add(new Tas("Tas Serbaguna Mini", "Kategori : Tas Lokal", "Deskripsi Tas : Tas serbaguna yang bisa menyimpan berbagai alat make up maupun perlengkapan lainnya dengan ukuran mini", "Rp 50.000", R.drawable.bagmini));
lstTas.add(new Tas("Tas Serbaguna Besar", "Kategori : Tas Lokal", "Deskripsi Tas : Tas serbaguna yang bisa menyimpan berbagai alat make up maupun perlengkapan lainnya dengan ukuran besar", "Rp 150.000", R.drawable.bagblack));
lstTas.add(new Tas("Pouch Wanita Sedang", "Kategori : Pouch", "Deskripsi Tas : Dompet Pouch Trendy yang muat banyak barang dan trendy di bawa kemana saja", "Rp 20.000", R.drawable.pouch));
lstTas.add(new Tas("Tas Pink Mini", "Kategori : Tas Import", "Deskripsi Tas : Tas Import mini bag yang bisa menyimpan peralatan mandi ataupun kebutuhan sehari - hari wanita", "Rp.120.000", R.drawable.pinkmini));
lstTas.add(new Tas("Tote Bag Campus", "Kategori : Tas Import", "Deskripsi Tas : Tote Bag cocok untuk kalian yang masih kuliah untuk menyimpan perlengkapan kuliah", "Rp.90.000", R.drawable.totebag));
lstTas.add(new Tas("Fold Tote Bag", "Kategori : Tas Import", "Deskripsi Tas : Tote Bag lipat yang bisa di lipat cocok untuk para remaja yang suka simple membawa barang saat berpergian", "Rp.100.000", R.drawable.totebagorange));
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
Intent intent = getIntent();
TextView name = navigationView.getHeaderView(0).findViewById(R.id.nama);
TextView email = navigationView.getHeaderView(0).findViewById(R.id.mail);
String extraName = intent.getStringExtra("name");
String extraEmail = intent.getStringExtra("email");
name.setText(extraName);
email.setText(extraEmail);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new BerandaFragment()).commit();
navigationView.setCheckedItem(R.id.nav_beranda);
}
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_beranda:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new BerandaFragment()).commit();
break;
case R.id.nav_taslokal:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new TasLokalFragment()).commit();
break;
case R.id.nav_tasimport:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new TasImportFragment()).commit();
break;
case R.id.nav_status:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new StatusFragment()).commit();
break;
case R.id.nav_belanja:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new KeranjangFragment()).commit();
break;
case R.id.nav_komplain:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new KomplainFragment()).commit();
break;
case R.id.nav_refund:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new RefundFragment()).commit();
break;
case R.id.nav_pengaturan:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new PengaturanFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
RecyclerView mrv = findViewById(R.id.recyclerviewtas);
RecyclerViewAdapter mAdapter=new RecyclerViewAdapter(this, lstTas);
mrv.setLayoutManager(new GridLayoutManager(this, 2));
mrv.setAdapter(mAdapter);
mrv.setHasFixedSize(true);
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
Adapter.java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Context mContext;
private List<Tas> mData;
public RecyclerViewAdapter(Context mContext, List<Tas> mData) {
this.mContext = mContext;
this.mData = mData;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.cardview_tas, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_title.setText(mData.get(position).getTitle());
holder.tas_thumbnail.setImageResource(mData.get(position).getImg());
holder.card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, TasActivity.class);
intent.putExtra("Title", mData.get(position).getTitle());
intent.putExtra("Category", mData.get(position).getCategory());
intent.putExtra("Description", mData.get(position).getDescription());
intent.putExtra("Harga", mData.get(position).getHarga());
intent.putExtra("Img", mData.get(position).getImg());
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_title;
ImageView tas_thumbnail;
CardView card;
public MyViewHolder(View itemView) {
super(itemView);
tv_title = itemView.findViewById(R.id.texttas);
tas_thumbnail = itemView.findViewById(R.id.imagetas);
card = itemView.findViewById(R.id.cardviewid);
}
}
}
我的问题是我仍然使用此代码,还是更改了适配器或其他一些代码以使其变得容易?原因是我在另一个页面上混合了来自另一页面的代码,所以不会发生。
答案 0 :(得分:0)
我只是更改了列表,并为其创建了一个不同的适配器,但是在导入片段和第一张图片时,我只是使用了相同的列表,但是像这样的列表的不同集合
lstTas = new ArrayList<>();
lstTas.add(new Tas("Tas Serbaguna Sedang", "Kategori : Tas Lokal", "Deskripsi Tas : Tas serbaguna yang bisa menyimpan berbagai alat make up maupun perlengkapan lainnya", "Rp 70.000", R.drawable.greybag));
lstTas.add(new Tas("Pouch Wanita Sedang", "Kategori : Pouch", "Deskripsi Tas : Dompet Pouch Trendy yang muat banyak barang dan trendy di bawa kemana saja", "Rp 20.000", R.drawable.pouch));
lstTas.add(new Tas("Tas Pink Mini", "Kategori : Tas Import", "Deskripsi Tas : Tas Import mini bag yang bisa menyimpan peralatan mandi ataupun kebutuhan sehari - hari wanita", "Rp.120.000", R.drawable.pinkmini));
lstTas.add(new Tas("Tote Bag Campus", "Kategori : Tas Import", "Deskripsi Tas : Tote Bag cocok untuk kalian yang masih kuliah untuk menyimpan perlengkapan kuliah", "Rp.90.000", R.drawable.totebag));
lstTas.add(new Tas("Fold Tote Bag", "Kategori : Tas Import", "Deskripsi Tas : Tote Bag lipat yang bisa di lipat cocok untuk para remaja yang suka simple membawa barang saat berpergian", "Rp.100.000", R.drawable.totebagorange));
仅位于每个页面上数组列表上的不同集合上