CardViewAdapter具有多个嵌套的ViewHolders

时间:2016-10-28 11:24:37

标签: android

我有一个更实际的问题。 我正在创建一个具有多个卡片的应用程序 - 每个卡片都有不同的信息。我正在创建CardViewAdapter和内部我为每张卡创建了ViewHolders。一切都很好。

我的问题是 - 是否有更好的方法来创建嵌套类?因为我有5个ViewHolders,每个都有200多行代码,这使我的CardViewAdapter非常难以阅读。是否有更实际的方法可以做到这一点,或者它很好吗?

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

private Context mContext;

public CardViewAdapter(Context context) {
    mContext = context;
}

private String[] mDataSet;
private int[] mDataSetTypes;

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    Context context = viewGroup.getContext();
    View v;
    ViewHolder holder;


    if(viewType == 0){
        v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card1, viewGroup, false);
        return new Card1ViewHolder(v);
    } else if(viewType == 1) {
        v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card2, viewGroup, false);
        return new Card2ViewHolder(v);
    } else if(viewType == 2) {
        v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card3, viewGroup, false);
        return new Card3ViewHolder(v);
    } else if(viewType == 3) {
        v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card4, viewGroup, false);
        return new Card4DialerViewHolder(v);
    } else if(viewType == 4) {
        v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card5, viewGroup, false);
        return new Card5SettingsViewHolder(v);
    } else {
        return null;
    }
}

@Override
public void onBindViewHolder(CardViewAdapter.ViewHolder holder, int position) {

}

@Override
public int getItemCount() {
    return 5;
}

@Override
public int getItemViewType(int position) {
    int viewType = 0;

    if(position == 0){
        viewType = 0;
    } else if (position == 1){
        viewType = 1;
    } else if (position == 2){
        viewType = 2;
    } else if (position == 3){
        viewType = 3;
    } else if (position == 4){
        viewType = 4;
    }
    return viewType;
}

public class ViewHolder extends RecyclerView.ViewHolder {
    public ViewHolder(View v) {
        super(v);

    }
}

private class Card1ViewHolder extends ViewHolder{
    //layout 1
}

private class Card2ViewHolder extends ViewHolder{
    //layout 2
}

private class Card3ViewHolder extends ViewHolder {
    //layout 3
}

private class Card4ViewHolder extends ViewHolder {
    //layout 4
}

private class Card5ViewHolder extends ViewHolder {
    //layout 5
}

}

基本上在5个ViewHolders中,我有完全不同的布局做不同的事情。

1 个答案:

答案 0 :(得分:1)

声明基类:

public abstract class Binder {
    public abstract void bind(View view, Hash<Integer, Object> values);
}

因此,您可以为所有卡片视图覆盖它。 接下来,声明一个项目类:

public class CardItem {
    public HashMap<Integer, Object> values;
    public int type;
    public Binder binder;
    public bind(View view) {binder.bind(view, values);}
}

因此,在数据绑定中,您可以像item.bind(holder.view);一样填充它。 使用item.type以正确的布局创建ViewHolder