2016-07-29 108 views
0

當我使用這個RecyclerView有不同的佈局

@Override 
    public int getItemViewType(int position) { 

    return position; 
    } 

我RecyclerView工作沒有問題 現在我想就可以使用多佈局我用這個

@Override 
public int getItemViewType(int position) { 
    int type = -1; 
    if(mMessages.get(position).getDir().equals("left"))   type = 1; 
    else if(mMessages.get(position).getDir().equals("right")) type = 0; 
    else if(mMessages.get(position).getDir().equals("typing")) type = 2; 
    return type; 
    //return position; 

} 

的問題,當我使用它和滾動到頂部和回到底部或滾動 其RecyclerView其重新訂購項目喜歡做它的第一個項目最後一個或中間或每一個不是我的正確位置

只是如果我回getItemViewType第一代碼的工作,而 問題,但我不能使用多佈局

我完整的代碼

public class MessageAdapter1 extends RecyclerView.Adapter<MessageAdapter1.ViewHolder> { 

private List<MessageList> mMessages; 
private int[] mUsernameColors; 
private Context context; 


public MessageAdapter1(Context context, List<MessageList> messages) { 
    mMessages = messages; 
    this.context = context; 

} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    int layout = -1; 



    switch (viewType) { 
    case 0: 
     layout = R.layout.right_message; 
     break; 
    case 1: 
     layout = R.layout.left_message; 
     break; 
    case 2: 
     layout = R.layout.message_left; 
     break; 
    } 



    View v = LayoutInflater.from(parent.getContext()).inflate(layout , parent, false); 

    return new ViewHolder(v); 
} 

@Override 
public void onBindViewHolder(ViewHolder viewHolder, int position) { 
    MessageList message = mMessages.get(position); 


    //viewHolder.setMessage(message.getMessage()); 
    //viewHolder.setUsername(message.getUsername()); 
    viewHolder.setGroupMessage(message); 
} 

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



@Override 
public int getItemViewType(int position) { 
    int type = -1; 
    if(mMessages.get(position).getDir().equals("left"))   type = 1; 
    else if(mMessages.get(position).getDir().equals("right")) type = 0; 
    else if(mMessages.get(position).getDir().equals("typing")) type = 2; 
    return type; 
    //return position; 
} 

public class ViewHolder extends RecyclerView.ViewHolder { 

    private LinearLayout groupMessage; 
    //private ImageView typing; 
    public ViewHolder(View itemView) { 
     super(itemView); 

     groupMessage = (LinearLayout)itemView.findViewById(R.id.messages); 

    } 

    public void setGroupMessage(MessageList m) { 


     if (null == groupMessage) return; 

     int i = 0; 

     if(m.getMessageStatus() == false){ 
      m.setMessageStatus(true); 
      for (String message : m.getMessageList()) { 

       //TextView text = new TextView(activity); 
       TextView text = new MyTextView(context); 
       LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
       p.setMargins(0, 0, 0, 2); 

       if (m.getDir().equals("left")) { 

        text.setTextColor(Color.BLACK); 
        p.gravity = Gravity.LEFT; 
        if(m.getMessageList().size() == 1){ 
         text.setBackgroundResource(R.drawable.message_left_default); 
        } 
        else if (i == 0) { 
         text.setBackgroundResource(R.drawable.message_left_first); 
        } else if (i + 1 == m.getMessageList().size()) { 
         text.setBackgroundResource(R.drawable.message_left_last); 
        } else { 
         text.setBackgroundResource(R.drawable.message_left); 
        } 


       } else{ 

        p.gravity = Gravity.RIGHT; 
        text.setTextColor(Color.WHITE); 

        if(m.getMessageList().size() == 1){ 
         text.setBackgroundResource(R.drawable.message_right_default); 
        } 
        else if (i == 0) { 
         text.setBackgroundResource(R.drawable.message_right_first); 
        } else if (i + 1 == m.getMessageList().size()) { 
         text.setBackgroundResource(R.drawable.message_right_last); 
        } else { 
         text.setBackgroundResource(R.drawable.message_right); 
        } 

       } 


       text.setLayoutParams(p); 
       text.setText(message); 
       text.setPadding(8, 8, 8, 8); 

       text.setTextSize(18f); 



       //text.setTextAppearance(context, android.R.style.TextAppearance_Large); 
       groupMessage.addView(text); 
       i++; 
      } 

     } 




    } 



    } 
} 

回答

1

我解決它通過刪除

int type = -1; 
if(mMessages.get(position).getDir().equals("left"))   type = 1; 
else if(mMessages.get(position).getDir().equals("right")) type = 0; 
else if(mMessages.get(position).getDir().equals("typing")) type = 2; 
return type; 

from

getItemViewType 

和onCreateViewHolder返回position

,並把第一個代碼

int type = -1; 
if(mMessages.get(position).getDir().equals("left"))   type = 1; 
else if(mMessages.get(position).getDir().equals("right")) type = 0; 
else if(mMessages.get(position).getDir().equals("typing")) type = 2; 
return type; 

,並使用viewType由於當前位置