如何在scrollview中添加多个listview

时间:2013-08-21 05:48:43

标签: android

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

              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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这是安静的旧UX问题 - 列表视图中的列表视图。在Android中不允许这样做,但您可以尝试其他选项 - 一个包含不同类型项目的列表视图

Android列表视图&amp;适配器支持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

您也可以使用多种视图类型来实现一些特殊的分隔符或节条。