0

如何在滚动视图中添加多个列表视图 我有支持滚动的单个列表视图的代码 如何添加多个列表视图?下面是我支持滚动单个列表视图的代码我如何添加多个列表视图???

              public class MainActivity extends Activity {

    @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView = (ListView) findViewById(R.id.lisview);
    View headerView = LayoutInflater.from(this).inflate(R.layout.header, null);
    listView.addHeaderView(headerView);
    ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i<100; i++){
        list.add(String.valueOf(i));
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
            android.R.layout.simple_list_item_1, android.R.id.text1, list);
    listView.setAdapter(adapter);
  }


              <!--activity_main.xml--->



                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res 
       /android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:orientation="vertical">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dip">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="30sp" 
        android:layout_gravity="center"/>
</FrameLayout>

<ListView 
    android:id="@+id/lisview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:cacheColorHint="#00000000"/>
          </LinearLayout>


            <!-----header.xnml---->


                 <?xml version="1.0" encoding="utf-8"?>
   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Header View" 
    android:textSize="30sp"
    android:textColor="#ff00ff00"
    android:layout_gravity="center"/>

      </FrameLayout>
4

2 回答 2

0

这是一个安静的旧用户体验问题 - 列表视图中的列表视图。它在 Android 中是不允许的,但您可以尝试一些替代方案 -一个包含不同类型项目的列表视图

Android 列表视图和适配器支持 ViewType 元素,因此您可以轻松实现它。例如,

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView listView = (ListView) findViewById(R.id.list1);

    listView.setAdapter(new MyCustomAdapter(this));
}

private class MyCustomAdapter extends ArrayAdapter<Object> {

    public MyCustomAdapter(Context context) {
        super(context, 0);
    }

    @Override
    public int getViewTypeCount() {
        return 2;   //any number what you need.
    }

    @Override
    public int getItemViewType(int position) {
        //view type is managed as zero-based index.
        if(position > 5)
            return 0;
        else
            return 1;
    }

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

        if( convertView == null ){
            // View Recycling is managed separately based on its view type,
            // so you don't need to worry about view corruptions.

            LayoutInflater inflater = getLayoutInflater();
            int type = getItemViewType(position);
            switch (type){
                case 0:
                    convertView = inflater.inflate(R.layout.item_type_0);
                case 1:
                    convertView = inflater.inflate(R.layout.item_type_1);
            }
        }

        return convertView;
    }
}//end of inner class 
}//end of class

您也可以使用多个视图类型来实现一些特殊的分隔线或截面栏。

于 2013-08-21T06:17:57.430 回答
0

我不完全明白......用户如何选择滚动滚动视图或列表视图?将 listView 放在 ScrollView 中通常不是一个好主意。但是,只需将另一个 ListView 放在 xml 布局中的第一个下。

于 2013-08-21T05:51:29.813 回答