2017-01-06 5 views
0

Я хочу делитель в моем ListView, когда дни разные, поэтому я использую приведенный ниже код, чтобы попытаться это сделать.Надуть несколько макетов в один раз в ListView

Проблема с этим, в GetView() для случая TYPE_SEPARATOR, я не могу надуть как макет chatheader и list_right_chat или list_left_chat, потому что он возвращает только последний вид. Как я могу добавить разделитель (заголовок) и нормальный макет, не добавляя непосредственно заголовок в список, который передается в ListView?

Код:

public class ChatListAdapter extends ArrayAdapter<Message> { 
private LayoutInflater mInflater; 

public ChatListAdapter(Context context, List<Message> items) { 
    super(context, 0, items); 
    mInflater = LayoutInflater.from(context); 
} 

@Override 
public int getViewTypeCount() { 
    return RowType.values().length; 
} 

@Override 
public int getItemViewType(int position) { 
    //Check Dates 
    DateTimeZone timeZone = DateTimeZone.getDefault(); 
    DateTime now = new DateTime(timeZone); 
    Interval today = new Interval(now.withTimeAtStartOfDay(), now.plusDays(1).withTimeAtStartOfDay()); 
    MutableDateTime epoch = new MutableDateTime(); 
    epoch.setDate(getItem(position).getEpochTime()); //Set to Epoch time 
    DateTime dateTime = new DateTime(epoch); 
    boolean messageHappenedToday = today.contains(dateTime); 

    if(position == 0) 
    { 
     if(messageHappenedToday) 
      return TYPE_MESSAGE_ITEM; 

     return TYPE_SEPARATOR; 
    } 
    else 
    { 
     //Check if current message is the same date as the last message. 
     //Last message 
     epoch.setDate(getItem(position - 1).getEpochTime()); //Set to Epoch time 
     DateTime dateTimeLastMessage = new DateTime(epoch); 
     Interval oneDayLastMessage = new Interval(dateTimeLastMessage.withTimeAtStartOfDay(), dateTimeLastMessage.plusDays(1).withTimeAtStartOfDay()); 

     //Current message 
     MutableDateTime epochCurrentMessage = new MutableDateTime(); 
     epochCurrentMessage.setDate(getItem(position).getEpochTime()); 
     DateTime dateTimeCurrentMessage = new DateTime(epochCurrentMessage); 
     boolean lastMessageHappenedToday = oneDayLastMessage.contains(dateTimeCurrentMessage); 

     if(lastMessageHappenedToday) 
      return TYPE_MESSAGE_ITEM; 
    } 
    return TYPE_SEPARATOR; 
} 

private static final int TYPE_MESSAGE_ITEM = 0; 
private static final int TYPE_SEPARATOR = 1; 

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder = null; 
    int rowType = getItemViewType(position); 
    View View; 
    if (convertView == null) { 
     Log.i("Cascade", "4: " + getItem(position).getFromName()); 

     holder = new ViewHolder(); 
     switch (rowType) { 
      case TYPE_MESSAGE_ITEM: 
       if(getItem(position).getSelf()) 
        convertView = mInflater.inflate(R.layout.list_right_chat, null); 
       else 
        convertView = mInflater.inflate(R.layout.list_left_chat, null); 
       holder.View = getItem(position).getView(TYPE_MESSAGE_ITEM, mInflater, convertView); 
       break; 
      case TYPE_SEPARATOR: 
       convertView = mInflater.inflate(R.layout.chatheader, null); 
       holder.View = getItem(position).getView(TYPE_SEPARATOR, mInflater, convertView); 


       if(getItem(position).getSelf()) 
        convertView = mInflater.inflate(R.layout.list_right_chat, null); 
       else 
        convertView = mInflater.inflate(R.layout.list_left_chat, null); 
       holder.View = getItem(position).getView(TYPE_MESSAGE_ITEM, mInflater, convertView); 
       break; 
     } 
     convertView.setTag(holder); 
    } 
    else 
    { 
     holder = (ViewHolder) convertView.getTag(); 
    } 
    return convertView; 
} 

public static class ViewHolder { 
    public View View; } 
} 

Я всегда мог добавить заголовок к обоим из моих макетов с View.GONE и сделать его видимым, когда это необходимо, но, кажется, своего рода «Hacky» ...

ответ

1

Я не вижу ничего плохого, включая заголовок в обоих ваших макетах.

В частности, если это возможно в вашем случае, я бы использовал RecyclerView вместе с Data Binding для отображения данных и всего несколько строк кода, чтобы решить, отображать ли заголовок или нет.

-1

Вы добавление строки таблицы в таблицу, так что используйте TableLayout.LayoutParams вместо LinearLayout.LayoutParams.As мы используем LayoutParam в соответствии с родителем, в котором мы будем добавлять

  TableLayout table = (TableLayout) findViewById(R.id.TableLayout1); 

     TableRow tr = new TableRow(this); 
     LinearLayout.LayoutParams trparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     tr.setLayoutParams(trparams); 

     cg[i] = new EditText(this); 
     weight[i] = new EditText(this); 
     cg[i].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); 
     weight[i].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); 

     //Here's where it goes wrong. By adding fieldparams, the text field doesn't even get generated. 
     LinearLayout.LayoutParams fieldparams = new LinearLayout.LayoutParams(10, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f); 
     tr.addView(cg[i], fieldparams); 
     tr.addView(weight[i], fieldparams); 

     table.addView(tr); 
+0

Я не использую таблицу, и вы не можете использовать addView() в ListView. –

Смежные вопросы