我可以在回收站视图适配器中使用switch语句将其用于不同的视图和数据吗?

时间:2019-06-24 04:34:33

标签: android-recyclerview recycler-adapter

我正在尝试在android的回收器视图中显示类似物品的列表。我有很多不同的物品。我可以使用switch语句对不同的项目视图使用相同的适配器类。如果不是,那么在这种情况下最有效的方法是。对于所有不同的项目类型,使用单独的适配器比使用switch语句更有效?

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
private Context mContext;
private List<UploadData> mUploads;
private int index;


public ImageAdapter(Context mContext, List<UploadData> mUploads, int index) {
    this.mContext = mContext;
    this.mUploads = mUploads;
    this.index = index;
}

@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View v;
    switch (index) {
        case 0:
            v = LayoutInflater.from(mContext).inflate(R.layout.single_item_view_book, viewGroup, false);
            break;
        case 1:
            v = LayoutInflater.from(mContext).inflate(R.layout.labcoat_display_fragment, viewGroup, false);

        default:
            v = LayoutInflater.from(mContext).inflate(R.layout.single_item_view_book, viewGroup, false);
            break;

    }
    return new ImageViewHolder(v);
}

@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull ImageViewHolder viewHolder, int i) {
    UploadData uploadCurrent = mUploads.get(i);
    Picasso.get().load(uploadCurrent.getUrlList().get(0)).fit().centerInside().into(viewHolder.mimageView);
    viewHolder.mPrice.setText("\u20B9" + " " + uploadCurrent.getItemDetail().get(0));
    switch (index) {
        case 0:
            viewHolder.mTitleText.setText("Title: " + uploadCurrent.getItemDetail().get(2));
            viewHolder.mDescriptionText.setText("Description: " + uploadCurrent.getItemDetail().get(3));
            viewHolder.mAuthor.setText("Author: " + uploadCurrent.getItemDetail().get(1));
            viewHolder.mPublication.setText("Publication: " + uploadCurrent.getItemDetail().get(4));
            break;
        case 1:
            viewHolder.mTitleText.setText("" + uploadCurrent.getItemDetail().get(1));
            viewHolder.mSize.setText("" + uploadCurrent.getItemDetail().get(2));
            viewHolder.mDescriptionText.setText("" + uploadCurrent.getItemDetail().get(3));
            break;

    }
}

@Override
public int getItemCount() {
    return mUploads.size();
}

class ImageViewHolder extends RecyclerView.ViewHolder {
    private TextView mTitleText;
    private ImageView mimageView;
    private TextView mPrice;
    private TextView mAuthor;
    private TextView mPublication;
    private TextView mDescriptionText;
    private TextView mSize;

    public ImageViewHolder(@NonNull View itemView) {
        super(itemView);
        mPrice = itemView.findViewById(R.id.item_price);
        mTitleText = itemView.findViewById(R.id.item_title);
        mimageView = itemView.findViewById(R.id.item_view_uploaded);
        mDescriptionText = itemView.findViewById(R.id.item_description);
        switch (index) {

            case 0:
                mAuthor = itemView.findViewById(R.id.item_author);
                mPublication = itemView.findViewById(R.id.item_publication);

                break;

            case 1:
                mSize = itemView.findViewById(R.id.item_size);
                break;


        }
    }
}

}

0 个答案:

没有答案