Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.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_Android Layout_Android Listview - Fatal编程技术网

Android 一个布局中有多个列表视图

Android 一个布局中有多个列表视图,android,android-layout,android-listview,Android,Android Layout,Android Listview,我想在一个活动中填充两个ListView。为此,我在一个布局中放置了两个ListView。我的问题是两个listview都已填充,但只有一个listview可见。现在,当我使一个布局不可见时,它将显示另一个listview 这里我发布了我的xml文件。我的布局有问题吗?请提出解决方案 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fil

我想在一个活动中填充两个ListView。为此,我在一个布局中放置了两个ListView。我的问题是两个listview都已填充,但只有一个listview可见。现在,当我使一个布局不可见时,它将显示另一个listview

这里我发布了我的xml文件。我的布局有问题吗?请提出解决方案

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingTop="10dp"
    android:id="@+id/playlist_lyt"
    >

    <Button 
        android:id="@+id/btnAlbumwise"
        android:text="View Albumwise"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        />

    <RelativeLayout 
        android:id="@+id/layout_songlist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:layout_below="@+id/layout_albumlist"  
        >

        <EditText android:id="@+id/inputSearch"
            android:layout_width="fill_parent"
            android:layout_height="33dp"
            android:hint="Search.."
            android:inputType="textVisiblePassword"
            android:cursorVisible="false"
            android:background="@drawable/rounded_edittext" 
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"            
        />

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:divider="#000000"
            android:dividerHeight="4dp"
            android:listSelector="@drawable/list_selector"
            android:layout_below="@+id/inputSearch" 
            android:paddingTop="8dp"/>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layout_albumlist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:layout_below="@+id/btnAlbumwise"        
        >

         <ListView  
            android:id="@+id/liist"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:divider="#242424"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/list_selector"
        />
    </RelativeLayout>
</RelativeLayout>

填充listview“liist”的代码。playlativity.java

      Cursor cursor1 = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null,null, null, null);
      if (cursor1 == null) 
      {
          //Query Failed , Handle error.
      }
      else if (!cursor1.moveToFirst()) 
      {
         //No media on the device.
      }
      else
      {   
        //  int albumName = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
          int id = cursor1.getColumnIndex(MediaStore.Audio.Albums._ID);
          int albumartist = cursor1.getColumnIndex(MediaStore.Audio.Albums.ARTIST);

          for(int i=0;i<cursor1.getCount();i++)
          {
                String Name = cursor1.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM));
                Integer albumid = cursor1.getInt(id);                       
                String artist = cursor1.getString(albumartist);

                Bitmap bitmap = null;
                bitmap = getArtwork(context, albumid);
                    lstImage.add(bitmap);
                    lstName.add(Name);
                    lstArtist.add(artist);

                cursor1.moveToNext();
           }
       }
       cursor1.close();                    

       rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < lstName.size(); i++) {
            RowItem item = new RowItem(lstImage.get(i), lstName.get(i) , lstArtist.get(i));
            rowItems.add(item);
        }          
        CustomListViewAdapter adapter = new CustomListViewAdapter(PlayListActivity.this,
                R.layout.list_item, rowItems);
        lv1.setAdapter(adapter);
        lv1.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                RowItem item = rowItems.get(position);
                String title = item.getTitle();
                Intent in = new Intent(PlayListActivity.this,AlbumsongListActivity.class);

                // Sending songIndex to PlayerActivity
                in.putExtra("albumTitle", title);
                in.putExtra("list", 0);
                in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(in);

            }
        });
Cursor cursor1=managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,null,null,null);
if(cursor1==null)
{
//查询失败,处理错误。
}
如果(!cursor1.moveToFirst()),则为else
{
//设备上没有媒体。
}
其他的
{   
//int albumName=cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
int id=cursor1.getColumnIndex(MediaStore.Audio.Albums.\u id);
int albumartist=cursor1.getColumnIndex(MediaStore.Audio.Albums.Artister);
对于(int i=0;i父级,视图,
内部位置,长id)
{
//获取列表项索引
int songIndex=(int)id;
//开始新的意图
Intent in=newintent(getApplicationContext(),MusicPlayertemp.class);
in.putExtra(“歌曲索引”,songIndex);
in.putExtra(“albumtitle”,albumName);
in.putExtra(“listFlag”,3);
星触觉(in);
}
});
}
公共ArrayList getAlbumSongList(上下文c)
{
字符串whereClause=MediaStore.Audio.Media.ALBUM+“=?”;
字符串s[]=新字符串[]{albumName};
Cursor Cursor=c.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,where子句,s,MediaStore.Audio.Media.TITLE);
if(游标==null)
{
//查询失败,处理错误。
} 
如果(!cursor.moveToFirst()),则为else
{
//设备上没有媒体。
} 
其他的
{
int titleColumn=cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn=cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.DATA);
int artistcolumn=cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
albumid=cursor.getInt(cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ALBUM_ID));
int id=cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.\u id);
对于(int i=0;i

在albumsonglistActivity中,如果我单击它未被调用的项,

您已将
android:layout\u height=“fill\u parent”
赋予两个相对布局。这就是为什么不能同时显示两个列表视图


使用线性布局,通过使用权重,您可以分割显示两个列表视图的屏幕

尝试如下更新代码

<RelativeLayout 
    android:id="@+id/layout_songlist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="10dp"
    android:layout_below="@+id/layout_albumlist"  
    >

    <EditText android:id="@+id/inputSearch"
        android:layout_width="fill_parent"
        android:layout_height="33dp"
        android:hint="Search.."
        android:inputType="textVisiblePassword"
        android:cursorVisible="false"
        android:background="@drawable/rounded_edittext" 
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"            
    />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#000000"
        android:dividerHeight="4dp"
        android:listSelector="@drawable/list_selector"
        android:layout_below="@+id/inputSearch" 
        android:paddingTop="8dp"/>

</RelativeLayout>

<RelativeLayout
    android:id="@+id/layout_albumlist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="10dp"
    android:layout_below="@+id/btnAlbumwise"        
    >

     <ListView  
        android:id="@+id/liist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="#242424"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
    />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/playlist_lyt"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingTop="10dp" >

    <Button
        android:id="@+id/btnAlbumwise"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View Albumwise" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:weightSum="1" >

        <RelativeLayout
            android:id="@+id/layout_albumlist"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5" >

            <ListView
                android:id="@+id/liist"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:divider="#242424"
                android:dividerHeight="1dp"
                android:listSelector="@drawable/list_selector" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layout_songlist"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5" >

            <EditText
                android:id="@+id/inputSearch"
                android:layout_width="fill_parent"
                android:layout_height="33dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/rounded_edittext"
                android:cursorVisible="false"
                android:hint="Search.."
                android:inputType="textVisiblePassword" />

            <ListView
                android:id="@android:id/list"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/inputSearch"
                android:divider="#000000"
                android:dividerHeight="4dp"
                android:listSelector="@drawable/list_selector"
                android:paddingTop="8dp" />
        </RelativeLayout>

    <RelativeLayout>
</RelativeLayout>

这是一个问题,因为您正在第一个布局下方设置另一个布局,并且第一个布局需要高度来填充父布局

您应该使用线性布局,将两个listview放在该布局中,并使用Layout_weight属性使两个listview具有相同的高度

例如,您可以遵循下面的层次结构。它不是原始代码,只是一个可以用来开发布局的结构

<LinearLayout orientation="verical">
   <ListView layout_weight=1></ListView>   
   <ListView layout_weight=1></ListView>
</LinearLayout>

添加


对于包含
列表视图的
相对布局

,第二个布局为填充父布局,第一个布局位于其下方。所以这是不可能看到的

我猜你想要这样的东西

<RelativeLayout 
    android:id="@+id/layout_songlist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="10dp"
    android:layout_below="@+id/layout_albumlist"  
    >

    <EditText android:id="@+id/inputSearch"
        android:layout_width="fill_parent"
        android:layout_height="33dp"
        android:hint="Search.."
        android:inputType="textVisiblePassword"
        android:cursorVisible="false"
        android:background="@drawable/rounded_edittext" 
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"            
    />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#000000"
        android:dividerHeight="4dp"
        android:listSelector="@drawable/list_selector"
        android:layout_below="@+id/inputSearch" 
        android:paddingTop="8dp"/>

</RelativeLayout>

<RelativeLayout
    android:id="@+id/layout_albumlist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="10dp"
    android:layout_below="@+id/btnAlbumwise"        
    >

     <ListView  
        android:id="@+id/liist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="#242424"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
    />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/playlist_lyt"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingTop="10dp" >

    <Button
        android:id="@+id/btnAlbumwise"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View Albumwise" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:weightSum="1" >

        <RelativeLayout
            android:id="@+id/layout_albumlist"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5" >

            <ListView
                android:id="@+id/liist"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:divider="#242424"
                android:dividerHeight="1dp"
                android:listSelector="@drawable/list_selector" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layout_songlist"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5" >

            <EditText
                android:id="@+id/inputSearch"
                android:layout_width="fill_parent"
                android:layout_height="33dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/rounded_edittext"
                android:cursorVisible="false"
                android:hint="Search.."
                android:inputType="textVisiblePassword" />

            <ListView
                android:id="@android:id/list"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/inputSearch"
                android:divider="#000000"
                android:dividerHeight="4dp"
                android:listSelector="@drawable/list_selector"
                android:paddingTop="8dp" />
        </RelativeLayout>

    <RelativeLayout>
</RelativeLayout>

最好的解决方案是创建一个自定义滚动视图并使用它来 将
替换为

将以下内容用于VerticalScrollView.java

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class VerticalScrollView extends ScrollView{

    public VerticalScrollView(Context context) {
        super(context);
    }

    public VerticalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerticalScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false" );
                super.onTouchEvent(ev);
                break;

            case MotionEvent.ACTION_MOVE:
                return false; // redirect MotionEvents to ourself

            case MotionEvent.ACTION_CANCEL:
                Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false" );
                super.onTouchEvent(ev);
                break;

            case MotionEvent.ACTION_UP:
                Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false" );
                return false;

            default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action ); break;
        }

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        Log.i("VerticalScrollview", "onTouchEvent. action: " +ev.getAction());
        return true;
    }
}

在第一个listview中,只有3个项目存在,然后它也不显示另一个listview。要查看另一个listview,我必须隐藏一个布局。之后,如果我单击第二个listview项目,它将启动另一个活动,该活动也包含listview,但此listview项目没有响应。这意味着当我单击任何项目时..它不会触发onItemClick()事件..您是否使用setOnItemClickListener将侦听器设置为listview?是的..我已设置了侦听器。您是否通过在onItemClickListener方法中放置日志消息进行了检查?我已在onItemClickListener的每一行设置了断点,但调试器没有设置。所以,把日志消息放在里面是没有用的。好吧。但是现在,如果我点击第二个listview项目,它会启动另一个活动,这个活动有另一个具有listview的布局,它会被自动填充。当我点击这个新的ListView项目时,它不会触发McClick事件。logcat显示“无法保存…”的id。那么问题是什么呢?