2016-11-21 42 views

回答

0

你可以在您的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

所以我必須創建一個包含兩個列表中所有元素的結構體?對 ? – z85510

+0

是的,在適配器中,您需要將兩種類型的元素存儲在您選擇的列表中,並根據對象類型在onBindViewHolder中填充其佈局屬性 –

+0

非常感謝,您救了我 – z85510

0

所以,你需要有兩種不同類型的項目在列表基地的基礎?使用RecyclerView而不是ListView,因爲它已經實現了ViewHolderPattern。在適配器中,有方法.onBindViwHolder(),接收int變量,重新設置元素類型。用它來綁定您的視圖

+0

我用RecyclerView但我不知道如何將兩種類型的結構發送到其適配器 – z85510

+0

create b ase類併爲每個結構擴展它。將對基類的引用傳遞給適配器。 –

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; 
} 
} 
相關問題