简体   繁体   English

如何在recyclerView中显示不同的布局?

[英]How to show different layouts in recyclerView?

I want to show Notifications list with 4 different types of layout. 我想显示4种不同类型布局的通知列表。 There will be 4 types of notifications chat_noti,share_noti,coorganizer_noti,invitation_noti. 将有4种类型的通知chat_noti,share_noti,coorganizer_noti,invitation_noti。 These 4 types has 4 different layouts. 这4种类型有4种不同的布局。 I want to show this in recyclerview. 我想在recyclerview中展示这一点。

This is my adapter : 这是我的适配器:

EDIT: 编辑:

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 : LogCat:

FATAL EXCEPTION: main Process: com.example.siddhi.meavita, PID: 22717 java.lang.NullPointerException: Attempt to write to field 'int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null object reference at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5483) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4707) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353) 致命异常:主要进程: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。在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java)的android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:1994)上的RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4617): 1390)在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)

I tried using this code it's throwing null pointer exception for view type. 我尝试使用此代码,它为视图类型抛出空指针异常。

Based on notification type the layout should get displayed. 根据通知类型,应显示布局。 How can I do this? 我怎样才能做到这一点? Thank you.. 谢谢..

I am adding items like this: 我正在添加这样的项目:

  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();


}

Last attribute is notification type. 最后一个属性是通知类型。

This error is caused when getItemViewType() returns a value for which the onCreateViewHolder has not assigned a viewholder. getItemViewType()返回onCreateViewHolder尚未为其分配视图的值时,会导致此错误。 What I feel is happening is that the getItemViewType() is returning -1 for some value. 我觉得发生的是getItemViewType()为某些值返回-1。 Now the onCreateViewHolder() does not know what to do when the viewType == -1 . 现在,当viewType == -1时, onCreateViewHolder()不知道该怎么做。 Hence, you are getting the error. 因此,您收到错误。

在我的情况下,当我忘记在onCreateViewHolder方法的一部分中返回视图持有者时,我收到此错误,并且它在方法结束时落入“return null”语句。

@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...

} }

@soku and @Jason Tyler are correct. @soku和@Jason Tyler是对的。 I had the same problem one time but the reason was that I was returning 'null' in my onCreateViewHolder method. 我有一次遇到同样的问题,但原因是我在onCreateViewHolder方法中返回'null'。 I changed that to a new instance of my default ViewHolder and it worked. 我将其更改为我的默认ViewHolder的新实例,并且它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM