【问题标题】:How to show different layouts in recyclerView?如何在 recyclerView 中显示不同的布局?
【发布时间】:2016-10-02 00:40:28
【问题描述】:

我想显示具有 4 种不同布局类型的通知列表。将有 4 种类型的通知 chat_noti、share_noti、coorganizer_noti、invitation_noti。 这 4 种类型有 4 种不同的布局。我想在 recyclerview 中显示这个。

这是我的适配器:

编辑:

List<Notifications> notificationsList;
Context context;
private static final int INVITATION_NOTI = 0;
private static final int CHAT_NOTI = 1;
private static final int SHARE_NOTI = 2;
private static final int COORGANIZER_NOTI = 3;
private int selectedPos = 0;
public NotificationsAdapter(Context context,List<Notifications> notificationsList){
    this.notificationsList = notificationsList;
    this.context = context;
}

@Override
public int getItemViewType(int type) {

    if (type == 0)
    {
        return INVITATION_NOTI;
    }
    else if(type == 1)
    {
        return CHAT_NOTI;
    }
    else if(type == 2)
    {
        return SHARE_NOTI;
    }
    else if (type == 3)
    {
        return COORGANIZER_NOTI;
    }

    return -1;

}

@Override
public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType){
        case INVITATION_NOTI:
            return new InvitationViewHolder(LayoutInflater.from(context).inflate(R.layout.invitation_notification, parent, false));
        case CHAT_NOTI:
            return new ChatNotificationViewHolder(LayoutInflater.from(context).inflate(R.layout.chat_notification, parent, false));
        case SHARE_NOTI:
            return new ShareViewHolder(LayoutInflater.from(context).inflate(R.layout.share_notification, parent, false));
        case COORGANIZER_NOTI:
            return new CoOrganizerViewHolder(LayoutInflater.from(context).inflate(R.layout.co_organizer_notification, parent, false));
    }
    return null;
}

@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
    if(holder.getItemViewType() == INVITATION_NOTI){
        InvitationViewHolder  mholder = (InvitationViewHolder) holder;
    }
    else if(holder.getItemViewType() == CHAT_NOTI){
        ChatNotificationViewHolder  mholder = (ChatNotificationViewHolder) holder;
    }
    else if(holder.getItemViewType() == SHARE_NOTI){
        ShareViewHolder  mholder = (ShareViewHolder) holder;
    }
    else if(holder.getItemViewType() == COORGANIZER_NOTI){
        CoOrganizerViewHolder mholder = (CoOrganizerViewHolder) holder;
    }
}



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

public class InvitationViewHolder extends MainViewHolder{
    TextView text,event,question;
    Button yes,no;

    public InvitationViewHolder(View v){
        super(v);
        this.text = (TextView) v.findViewById(R.id.text);
        this.event = (TextView) v.findViewById(R.id.eventType);
        this.question = (TextView) v.findViewById(R.id.question);
        this.yes = (Button) v.findViewById(R.id.yes);
        this.no = (Button) v.findViewById(R.id.no);
    }
}

public class ChatNotificationViewHolder extends MainViewHolder{
    TextView text,sender;

    public ChatNotificationViewHolder(View v){
        super(v);
        this.text = (TextView) v.findViewById(R.id.text);
        this.sender = (TextView) v.findViewById(R.id.sender);
    }
}
public class ShareViewHolder extends MainViewHolder{
    TextView text;

    public ShareViewHolder(View v){
        super(v);
        this.text = (TextView) v.findViewById(R.id.text);
    }
}
public class CoOrganizerViewHolder extends MainViewHolder{
    TextView text;
    Button view;

    public CoOrganizerViewHolder(View v){
        super(v);
        this.text = (TextView) v.findViewById(R.id.text);
        this.view = (Button)  v.findViewById(R.id.view);
    }
}

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

LogCat:

致命异常:主要 进程:com.example.siddhi.meavita,PID:22717 java.lang.NullPointerException:尝试在空对象引用上写入字段“int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType” 在 android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5483) 在 android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4707) 在 android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) 在 android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994) 在 android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390) 在 android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)

我尝试使用此代码,它为视图类型抛出空指针异常。

应根据通知类型显示布局。我怎样才能做到这一点? 谢谢。。

我正在添加这样的项目:

  private void prepareData() {

    Notifications notifications = new Notifications("You have an Invitation.","Michell's Marraige","Attending?","",0);
    notificationsList.add(notifications);

    notifications = new Notifications("You have an Invitation.","Kate's Reception","Attending?","",0);
    notificationsList.add(notifications);

    notifications = new Notifications("Vishal Say's","","Have you attended wedding?","",1);
    notificationsList.add(notifications);


    notifications = new Notifications("Siddhi Say's","","Have you gone for shopping?","",1);
    notificationsList.add(notifications);

    notifications = new Notifications("Ashmit shared a photo with you","","","",2);
    notificationsList.add(notifications);

    notifications = new Notifications("Neeta shared a photo with you","","","",2);
    notificationsList.add(notifications);

    notifications = new Notifications("You are a co-organizer for My Birthaday","","","",3);
    notificationsList.add(notifications);

    notifications = new Notifications("You are a co-organizer for My Wedding","","","",3);
    notificationsList.add(notifications);

    mAdapter.notifyDataSetChanged();


}

最后一个属性是通知类型。

【问题讨论】:

  • 配合使用viewType可以实现
  • 你能检查一下编辑后的代码吗?它为视图类型抛出空指针异常。 @Nisarg
  • 请发布 logcat
  • 发布了 logcat。请检查。 @Nisarg
  • 你有多少列表项?

标签: java android android-recyclerview


【解决方案1】:

getItemViewType() 返回一个onCreateViewHolder 没有为其分配viewholder 的值时会导致此错误。我觉得正在发生的是 getItemViewType() 正在返回 -1 以获得一些价值。现在onCreateViewHolder() 不知道viewType == -1 时该怎么办。因此,您会收到错误消息。

【讨论】:

    【解决方案2】:

    在我的例子中,当我忘记在我的 onCreateViewHolder 方法的一部分中返回视图持有者时,我收到了这个错误,并且在方法结束时出现了“return null”语句。

    【讨论】:

      【解决方案3】:
      @Override
      public int getItemViewType(int position) {
          if (position != -1) {
              return 0;
          }
          return -1;
      }
      
      @Override
      public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          if (viewType != -1) {
              return new GroupImageViewHolder(mInflater.inflate(R.layout.group_image_item_view, parent, false));
          }
          return null;
      }
      
      
      @Override
      public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
          if (viewHolder != null) {
              // do whatever you want to do...
      

      }

      【讨论】:

      • 感谢您提供此代码 sn-p,它可能会提供一些即时帮助。一个正确的解释would greatly improve 其教育价值通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有类似但不相同的问题的读者更有用。请edit您的答案添加解释,并说明适用的限制和假设。
      【解决方案4】:

      @soku 和@Jason Tyler 是正确的。我曾经遇到过同样的问题,但原因是我在 onCreateViewHolder 方法中返回了“null”。我将其更改为我的默认 ViewHolder 的一个新实例,并且它起作用了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多