Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.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 一个视图中的两个列表视图_Android_Listview_Android Activity - Fatal编程技术网

Android 一个视图中的两个列表视图

Android 一个视图中的两个列表视图,android,listview,android-activity,Android,Listview,Android Activity,两个列表视图显示布局XML文件中的不同数据,其中包含以下代码: <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ListView android:id="@+id/detaillistview"

两个列表视图显示布局XML文件中的不同数据,其中包含以下代码:

 <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/detaillistview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:headerDividersEnabled="false" >
        </ListView>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/divider" />

        <ListView
            android:id="@+id/reviewlistview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:headerDividersEnabled="false" >
        </ListView>
    </LinearLayout>

但是当顶部的列表视图充满屏幕时,我将永远看不到底部的列表视图。
有办法解决这个问题吗?提前谢谢你

线性布局保持在
滚动视图中。就这样

我使用的格式如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/lblHeading"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:textColor="#ffffffff"
    android:textSize="20sp"
    android:textStyle="bold" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/lblBody"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:textColor="#ffffffff"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/txtText"
            android:layout_width="fill_parent"
            android:layout_height="170dp"
            android:layout_weight="1"
            android:autoText="true"
            android:gravity="left"
            android:scrollbars="vertical"
            android:textSize="16sp" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/btnButton"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:gravity="center"
            android:textSize="18dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btnReturn"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="18dp"
            android:textStyle="bold" />
    </LinearLayout>
</ScrollView>
</LinearLayout>

如果您想在屏幕上显示它们,您应该使用权重,但您需要滚动,您应该使用ScrollView包装线性布局,请尝试下面的代码

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/detaillistview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="false"
            android:headerDividersEnabled="false" >

        </ListView>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/divider" />

        <ListView
            android:id="@+id/reviewlistview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="false"
            android:headerDividersEnabled="false" >

        </ListView>
    </LinearLayout>

在一个布局中显示两个Listview有两种方法。它们如下

1) 首先,如果您想在不向外滚动视图的情况下显示,则需要提及listview的高度

2) 在这种情况下,您需要获取一个ScrollView,因为您需要在其中获取一个布局,您需要声明两个ListView

<ScrollView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/scrollView1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#ffffff"
   android:fillViewport="true" >

<RelativeLayout
   android:id="@+id/relativelayout1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" >

<ListView
   android:id="@+id/listView1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />
<ImageView
   android:id="@+id/imageView"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:src="@drawable/divider" />
<ListView
   android:id="@+id/listView2"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />
</RelativeLayout>
</ScrollView>

公共类分隔列表适配器扩展BaseAdapter
{
公共最终映射节=新建LinkedHashMap();
公共最终阵列适配器标头;
公共最终静态整型\节\头=0;
公共分隔的ListAdapter(上下文)
{
headers=newarrayadapter(上下文,R.layout.list_头);
}
public void addSection(字符串段、适配器)
{
此.headers.add(节);
此.sections.put(节,适配器);
}
公共对象getItem(int位置)
{
for(对象节:this.sections.keySet())
{
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
如果(位置==0)
返回段;
如果(位置<尺寸)
返回适配器.getItem(位置-1);
//否则跳到下一节
位置-=大小;
}
返回null;
}
public int getCount()
{
//所有章节合计,每个章节标题加上一个章节
int-total=0;
对于(适配器:this.sections.values())
总计+=适配器.getCount()+1;
返回总数;
}
public int getViewTypeCount()
{
//假设头数为一,然后合计所有部分
整数合计=1;
对于(适配器:this.sections.values())
总计+=适配器。getViewTypeCount();
返回总数;
}
public int getItemViewType(int位置)
{
int型=1;
for(对象节:this.sections.keySet())
{
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
如果(位置==0)
返回类型\段\头;
如果(位置<尺寸)
返回类型+适配器.getItemViewType(位置-1);
//否则跳到下一节
位置-=大小;
类型+=适配器。getViewTypeCount();
}
返回-1;
}
公共布尔值areAllItemsSelectable()
{
返回false;
}
公共布尔值isEnabled(整型位置)
{
返回(getItemViewType(位置)!=类型\节\标题);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
int sectionnum=0;
for(对象节:this.sections.keySet())
{
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
如果(位置==0)
返回headers.getView(sectionnum、convertView、parent);
如果(位置<尺寸)
返回adapter.getView(位置-1,convertView,父级);
//否则跳到下一节
位置-=大小;
sectionnum++;
}
返回null;
}
@凌驾
公共长getItemId(int位置)
{
返回位置;
}
}


我用它在一个listview中放置两种不同类型的数据,使用节

你想用两个listView实现什么?首先,你必须说明你希望发生什么,以及你在尝试这样做时做了什么。由于布局是垂直的,第二个listView和imageView在超出屏幕范围时不可见。我想在一个视图中显示两组数据。但是“垂直滚动滚动视图不应包含另一个垂直滚动小部件(ListView)”“您是否尝试过?或假设?它对我有效。在我的情况下,我有一个用于具有EditText的LinearLayout的滚动视图(具有垂直scrol的小部件)工作正常…@Jxdwinter找到解决方案了吗?谢谢你和我使用节将两种不同类型的数据放在一个listview中。
public class SeparatedListAdapter extends BaseAdapter
{

public final Map<String, Adapter> sections = new LinkedHashMap<String, Adapter>();
public final ArrayAdapter<String> headers;
public final static int TYPE_SECTION_HEADER = 0;

public SeparatedListAdapter(Context context)
{
    headers = new ArrayAdapter<String>(context, R.layout.list_header);
}

public void addSection(String section, Adapter adapter)
{
    this.headers.add(section);
    this.sections.put(section, adapter);
}

public Object getItem(int position)
{
    for (Object section : this.sections.keySet())
    {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        // check if position inside this section
        if (position == 0)
            return section;
        if (position < size)
            return adapter.getItem(position - 1);

        // otherwise jump into next section
        position -= size;
    }
    return null;
}

public int getCount()
{
    // total together all sections, plus one for each section header
    int total = 0;
    for (Adapter adapter : this.sections.values())
        total += adapter.getCount() + 1;
    return total;
}

public int getViewTypeCount()
{
    // assume that headers count as one, then total all sections
    int total = 1;
    for (Adapter adapter : this.sections.values())
        total += adapter.getViewTypeCount();
    return total;
}

public int getItemViewType(int position)
{
    int type = 1;
    for (Object section : this.sections.keySet())
    {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        // check if position inside this section
        if (position == 0)
            return TYPE_SECTION_HEADER;
        if (position < size)
            return type + adapter.getItemViewType(position - 1);

        // otherwise jump into next section
        position -= size;
        type += adapter.getViewTypeCount();
    }
    return -1;
}

public boolean areAllItemsSelectable()
{
    return false;
}

public boolean isEnabled(int position)
{
    return (getItemViewType(position) != TYPE_SECTION_HEADER);
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{

    int sectionnum = 0;
    for (Object section : this.sections.keySet())
    {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        // check if position inside this section
        if (position == 0)
            return headers.getView(sectionnum, convertView, parent);
        if (position < size)
            return adapter.getView(position - 1, convertView, parent);

        // otherwise jump into next section
        position -= size;
        sectionnum++;
    }
    return null;
}

@Override
public long getItemId(int position)
{
    return position;
}