Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在ListView中一次充气多个布局_Android_Android Layout_Listview - Fatal编程技术网

Android 在ListView中一次充气多个布局

Android 在ListView中一次充气多个布局,android,android-layout,listview,Android,Android Layout,Listview,当日期不同时,我希望在我的ListView中有一个分隔符,因此我使用下面的代码尝试这样做 问题是,在getView()中,对于TYPE\u SEPARATOR,我无法对chatheader和list\u right\u chat或list\u left\u chat布局进行充气,因为它只返回最后一个视图。如何在不直接将标题添加到传递给ListView的列表中的情况下添加分隔符(标题)和常规布局 代码: 公共类ChatListAdapter扩展了ArrayAdapter{ 私人停车场; 公共Cha

当日期不同时,我希望在我的ListView中有一个分隔符,因此我使用下面的代码尝试这样做

问题是,在getView()中,对于
TYPE\u SEPARATOR
,我无法对
chatheader
list\u right\u chat
list\u left\u chat
布局进行充气,因为它只返回最后一个视图。如何在不直接将标题添加到传递给ListView的列表中的情况下添加分隔符(标题)和常规布局

代码:

公共类ChatListAdapter扩展了ArrayAdapter{
私人停车场;
公共ChatListAdapter(上下文、列表项){
超级(上下文,0,项);
mInflater=LayoutInflater.from(上下文);
}
@凌驾
public int getViewTypeCount(){
返回RowType.values().length;
}
@凌驾
public int getItemViewType(int位置){
//核对日期
DateTimeZone timeZone=DateTimeZone.getDefault();
DateTime now=新的日期时间(时区);
Interval today=新的时间间隔(now.WithTimeAtStarToDay(),now.plusDays(1).WithTimeAtStarToDay());
MutableDateTime epoch=新的MutableDateTime();
epoch.setDate(getItem(position).getEpochTime());//设置为epoch时间
DateTime DateTime=新的日期时间(历元);
布尔messagehappendtoday=today.contains(dateTime);
如果(位置==0)
{
如果(消息发生在今天)
返回类型\消息\项目;
返回式分离器;
}
其他的
{
//检查当前消息是否与最后一条消息的日期相同。
//最后信息
epoch.setDate(getItem(位置-1).getEpochTime());//设置为epoch时间
DateTime dateTimeLastMessage=新日期时间(历元);
Interval oneDayLastMessage=新间隔(dateTimeLastMessage.WithTimeAtStarToDay(),dateTimeLastMessage.plusDays(1).WithTimeAtStarToDay());
//当前消息
MutableDateTime epochCurrentMessage=新的MutableDateTime();
setDate(getItem(position.getEpochTime());
DateTime dateTimeCurrentMessage=新日期时间(epochCurrentMessage);
布尔LastMessageHappendToday=oneDayLastMessage.contains(dateTimeCurrentMessage);
如果(最后一条消息发生在今天)
返回类型\消息\项目;
}
返回式分离器;
}
私有静态最终整数类型\消息\项=0;
私有静态final int TYPE_SEPARATOR=1;
公共视图getView(int位置、视图转换视图、视图组父视图){
ViewHolder=null;
int rowType=getItemViewType(位置);
视图;
if(convertView==null){
Log.i(“级联”,“4:”+getItem(position).getFromName());
holder=新的ViewHolder();
开关(行类型){
案例类型\消息\项目:
if(getItem(position).getSelf())
convertView=mInflater.充气(R.layout.list\u right\u chat,null);
其他的
convertView=mInflater.充气(R.layout.list\u left\u chat,null);
holder.View=getItem(位置).getView(输入消息项,mInflater,convertView);
打破
箱型分离器:
convertView=mInflater.充气(R.layout.chatheader,null);
holder.View=getItem(位置).getView(类型分隔符、mInflater、convertView);
if(getItem(position).getSelf())
convertView=mInflater.充气(R.layout.list\u right\u chat,null);
其他的
convertView=mInflater.充气(R.layout.list\u left\u chat,null);
holder.View=getItem(位置).getView(输入消息项,mInflater,convertView);
打破
}
convertView.setTag(支架);
}
其他的
{
holder=(ViewHolder)convertView.getTag();
}
返回视图;
}
公共静态类视图持有者{
公共视图;}
}

我总是可以使用
视图将标题添加到我的两个布局中。如果需要,可以使用
并使其可见,但这似乎有点“黑客”…

您正在将表行添加到表中,因此使用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);

我看不出在两个布局中都包含标题有什么不好的地方


特别是,如果您的情况可能的话,我会使用RecyclerView和来显示数据,并使用几行代码来决定是否显示标题。

我没有使用表,您不能在ListView中使用addView()。
            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);