当日期不同时,我想在ListView中使用分隔符,所以我使用下面的代码尝试这样做。
问题是,在TYPE_SEPARATOR
案例的getView()中,我不能同时展开chatheader
和list_right_chat
或list_left_chat
布局,因为它只是返回最后一个视图。如何在不直接将标题添加到传递给ListView的List中的情况下添加分隔符(标题)和普通布局?
代码:
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
将标题添加到我的两个布局中,并在需要时将其显示,但这看起来很糟糕&#39; ...
答案 0 :(得分:1)
我在两个布局中都没有看到任何不好的标题。
特别是,如果在您的情况下,我可以使用RecyclerView和Data Binding一起显示数据,只需几行代码来决定是否显示标题。
答案 1 :(得分:-1)
您正在将表行添加到表中,因此请使用TableLayout.LayoutParams而不是LinearLayout.LayoutParams。因为我们根据我们要添加的父项使用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);