如何在List中组合两种类型的适配器?

时间:2016-11-21 11:00:23

标签: android listview android-recyclerview adapter

我想创建一个2类型结构的List。

如何在一个列表中组合两种类型的适配器?

适配器1在struct 1上创建基础,而适配器2在struct 2上创建基础。

Three type of list

3 个答案:

答案 0 :(得分:0)

那么,您需要在列表中包含两种不同类型的项目吗?使用RecyclerView而不是ListView,因为它已经实现了ViewHolderPattern。在适配器中,有方法.onBindViwHolder(),接收int变量,重写元素类型。用它来绑定你的视图

答案 1 :(得分:0)

它也可以在listview中完成,你需要做的就是根据条件在baseadapter的getView()方法中膨胀视图

例如:

public void getView()
{
  if(some condition)
{
view=LayoutInflator.from(context).inflate(R.layout.layout1,parent,null);

//bind the related elements in the view accordingly;
}
else
{

view=LayoutInflator.from(context).inflate(R.layout.layout2,parent,null);

//bind the related elements in the view accordingly;
}
}

答案 2 :(得分:0)

您可以在RecyclerView中显示severel类型的listview行布局 - 您不需要多个适配器

您需要覆盖回收站视图的getItemCount和getItemViewType方法,并告诉它要在列表中显示的类型数量:RecyclerView item count

以下是一个例子:

适配器:

public class NotificationsAdapter extends RecyclerView.Adapter<MultipleRowViewHolder> {

private LayoutInflater inflater = null;

// notification list
private List<Notification> notificationList;

public NotificationsAdapter(Context context, List<Notification> multipleRowModelList){
    this.notificationList = multipleRowModelList;
    inflater = LayoutInflater.from(context);
}

@Override
public MultipleRowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = null;

    if (viewType == NotificationType.Lick.getValue())
        view = inflater.inflate(R.layout.lick_row_item, parent, false);
    else if (viewType == NotificationType.Comment.getValue())
        view = inflater.inflate(R.layout.comment_list_row, parent, false);
    else if (viewType == NotificationType.ViewMyProfile.getValue())
        view = inflater.inflate(R.layout.view_my_profile_list_row, parent, false);
    else // if notification is not of the first three types get the view_my_profile_list_row layout
        view = inflater.inflate(R.layout.view_my_profile_list_row, parent, false);

    return new MultipleRowViewHolder(view, viewType);
}
@Override
public void onBindViewHolder(MultipleRowViewHolder holder, int position) {

    try {
        // display enum type as title
        holder.titleTV.setText(NotificationType.toNotificationType(Integer.valueOf(notificationList.get(position).getType())).name());
        // display pretty date
        holder.actionTimeTV.setText(TimeUtils.getPrettyTimeDifference(notificationList.get(position).getActionTime()));
        holder.sourceUserNameTV.setText(notificationList.get(position).getSourceUserName());
    }catch (Exception e)
    {
        Log.e(NotificationReader.TAG,e.getMessage());
    }
}
@Override
public int getItemCount() {
    return notificationList.size();
}

@Override
public int getItemViewType(int position) {

    Notification multipleRowModel = notificationList.get(position);

    if (multipleRowModel != null)
        return Integer.valueOf(multipleRowModel.getType());

    return super.getItemViewType(position);
}

数据模型:

public class MultipleRowViewHolder extends RecyclerView.ViewHolder {

public TextView titleTV;
public TextView sourceUserNameTV;
public TextView actionTimeTV;

private int type;

public MultipleRowViewHolder(View itemView, int type) {
    super(itemView);

    titleTV = (TextView)itemView.findViewById(R.id.titleTV);
    sourceUserNameTV = (TextView)itemView.findViewById(R.id.sourceUserNameTV);
    actionTimeTV = (TextView)itemView.findViewById(R.id.actionTimeTV);

    this.type = type;

}

}