我有一个适配器,可以保留我需要的所有数据,但是我想在两类中使用该适配器。一种在recylerview中显示数据,另一种仅在ViewFlipper中显示数据的图像。但是我不知道如何在我的适配器上制作一个多功能以在另一个类(flipper)中使用它。
到目前为止,我将显示适配器的代码
public class AdapterCafe extends RecyclerView.Adapter<AdapterCafe.MyViewHolder> {
Context context;
List<CafeItem> cafe;
public AdapterCafe(Context context, List<CafeItem> data_cafe) {
this.context = context;
this.cafe = data_cafe;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.cafe_card, parent, false);
View view2 = LayoutInflater.from(context).inflate(R.layout.filppers_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
MyViewHolder holder2 = new MyViewHolder(view2);
holder2.tvNamacafeFlip.setText(cafe.get(viewType).getNamaCafe());
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
// Set widget
holder.tvNama.setText(cafe.get(position).getNamaCafe());
holder.tvAlamat.setText(cafe.get(position).getAlamatCafe());
final String urlGambarCafe = "http://192.168.43.159/web/ngafeapp/" + cafe.get(position).getFotoCafe();
Picasso.with(context).load(urlGambarCafe).into(holder.ivGambar);
Picasso.with(context).load(urlGambarCafe).into(holder.ivgambarFlip);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Mulai activity Detail
Intent varIntent = new Intent(context, cafedetailActivity.class);
// sisipkan data ke intent
varIntent.putExtra("ID_CAFE",cafe.get(position).getIdCafe());
varIntent.putExtra("NAMA_CAFE", cafe.get(position).getNamaCafe());
varIntent.putExtra("WKT_CAFE", cafe.get(position).getJamOperasionalWeekDay());
varIntent.putExtra("WKT_CAFE2", cafe.get(position).getJamOperasionalWeekEnd());
varIntent.putExtra("ALAMAT_CAFE", cafe.get(position).getAlamatCafe());
varIntent.putExtra("FOTO_CAFE", urlGambarCafe);
varIntent.putExtra("NO_TELEPON",cafe.get(position).getNoTelepon());
varIntent.putExtra("DESKRIPSI", cafe.get(position).getDeskripsiCafe());
varIntent.putExtra("LATITUDE",cafe.get(position).getLatitude());
varIntent.putExtra("LONGITUDE",cafe.get(position).getLongitude());
context.startActivity(varIntent);
}
});
}
@Override
public int getItemCount() {
return cafe.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView ivGambar, ivgambarFlip;
TextView tvNama, tvAlamat,tvNamacafeFlip;
public MyViewHolder(View itemView) {
super(itemView);
ivGambar = (ImageView) itemView.findViewById(R.id.images_cafe);
tvNama = (TextView) itemView.findViewById(R.id.name_cafe);
tvAlamat= (TextView) itemView.findViewById(R.id.alamatcafe);
}
}
}
因此,我尝试制作其他视图,但它不起作用。您能帮我,如何解决它?
答案 0 :(得分:0)
最好将适配器定义为abstract class
,并为这两个地方创建两个单独的类。
我想您想拥有两个不同的onCreateViewHolder
函数。
public abstract class AdapterCafe extends RecyclerView.Adapter<AdapterCafe.MyViewHolder> {
// Add every thing that are not different for the two cases.
// Anything except `onCreateViewHolder`
}
class Case1Adapter extends AdapterCafe {
// Now override `onCreateViewHolder`
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.cafe_card, parent, false);
MyViewHolder holder = new MyViewHolder(view);
holder.tvNamacafeFlip.setText(cafe.get(viewType).getNamaCafe());
return holder;
}
class Case2Adapter extends AdapterCafe {
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view2 = LayoutInflater.from(context).inflate(R.layout.filppers_item, parent, false);
MyViewHolder holder2 = new MyViewHolder(view2);
holder2.tvNamacafeFlip.setText(cafe.get(viewType).getNamaCafe());
return holder;
}