我如何使用一个适配器几个片段?

时间:2016-09-09 06:29:26

标签: java android android-fragments android-recyclerview android-adapter

有几个片段。在每个片段中我都有recyclerView with adapte)。在我创建第三个适配器的时候,我理解它们在结构上是相似的(ImageView,textView,textView),只有我保持文本和图片不同的数组。好吧,实际上我提出了一个想法,最重要的是,是否为我的所有片段使用一个适配器?目前,我的适配器:

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

private String[] titles = {"Chapter One",
        "Chapter Two",
        "Chapter Three",
        "Chapter Four",
        "Chapter Five",
        "Chapter Six",
        "Chapter Seven",
        "Chapter Eight"};

private String[] details = {"Item one details",
        "Item two details", "Item three details",
        "Item four details", "Item file details",
        "Item six details", "Item seven details",
        "Item eight details"};

private int[] images = { R.drawable.android_image_1,
        R.drawable.android_image_2,
        R.drawable.android_image_3,
        R.drawable.android_image_4,
        R.drawable.android_image_5,
        R.drawable.android_image_6,
        R.drawable.android_image_7,
        R.drawable.android_image_8 };

class ViewHolder extends RecyclerView.ViewHolder{

    public int currentItem;
    public ImageView itemImage;
    public TextView itemTitle;
    public TextView itemDetail;

    public ViewHolder(View itemView) {
        super(itemView);
        itemImage = (ImageView)itemView.findViewById(R.id.item_image);
        itemTitle = (TextView)itemView.findViewById(R.id.item_title);
        itemDetail =
                (TextView)itemView.findViewById(R.id.item_detail);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                int position = getAdapterPosition();

                Snackbar.make(v, "Click detected on item " + position,
                        Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.card_layout, viewGroup, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}


@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    viewHolder.itemTitle.setText(titles[i]);
    viewHolder.itemDetail.setText(details[i]);
    viewHolder.itemImage.setImageResource(images[i]);
}

@Override
public int getItemCount() {
    return titles.length;
}

}

3 个答案:

答案 0 :(得分:1)

检查object.getType()返回的内容。这个问题与此有关。我确定它正在返回字符串。这就是问题所在。

答案 1 :(得分:1)

试试这个,因为从你的问题来看,你似乎得到了整数但是期望字符串

case TYPE_TEXTVIEW:
                ((NumericViewHolder) holder).title.setText(String.valueOf(object.getTitle()));
                break;

case TYPE_EDITVIEW:
                ((DropDownViewHolder) holder).title_options.setText(String.ValueOf(object.getTitle()));
                break;

答案 2 :(得分:1)

只需更改静态变量类型,如:

public static final String TYPE_TEXTVIEW = "0";
public static final String TYPE_EDITVIEW = "1";

希望它会帮助你。