如何在android中创建复杂列表?

问题描述 投票:-3回答:3

我想用不同的行创建一个复杂的回收器视图。

怎么可能?

像这张照片:Three types of list

android listview android-recyclerview adapter
3个回答
1
投票

您可以在RecyclerView中显示严格的列表视图行布局类型-您不需要多个适配器

您需要重写回收器视图的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;

}

}


0
投票

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


0
投票

也可以在列表视图中完成,您只需要根据条件在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;
}
}
© www.soinside.com 2019 - 2024. All rights reserved.