两个ListViews&滚动型

时间:2011-06-26 04:39:36

标签: android android-layout

我离这儿很近,我尝试过很多东西,但是无法让它发挥作用。我这里有两个列表视图。我想要的是每个listview显示其整体内容。我不希望列表视图可滚动。我希望Scrollview能够保持列表视图的可滚动性。最接近我能够得到它(这是错误的)是每个listview是可滚动的。这意味着每个列表视图仅显示1 1/2个单元格。我认为这将是一个简单的任务,但Android有一些奇怪的事情正在发生。

同样,每个列表视图都会显示其整个内容,即使该内容高度超过了屏幕。然后我要做的就是向下滚动以查看显示其全部内容的第二个列表视图。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:orientation="vertical"
android:fillViewport="true">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:orientation="vertical"
android:fillViewport="true">
<LinearLayout 
android:layout_weight="10"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CCCCCC"
android:text="@string/alerts_top_title"
android:gravity="center"
android:textColor="#000000"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
</TextView>
<ListView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/cameraListAlerts" >
</ListView>
</LinearLayout>
<LinearLayout 
android:layout_weight="10"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CCCCCC"
android:textColor="#000000"
android:text="@string/alerts_bottom_title">
</TextView>
<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="8dp">
</LinearLayout>
<Button
android:id="@+id/addRecipient"
android:layout_height="50dp"
android:layout_width="fill_parent"
android:layout_centerHorizontal="true"
android:text="@string/addRecipient"/>

<ListView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/recipientListAlerts" >
</ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>

3 个答案:

答案 0 :(得分:9)

如果有的话,你将花费更多的时间和精力试图让它发挥作用。存在ListView来处理需要一次绘制太多资源的长列表。当您滚动它们时,它们会回收行视图以允许快速加载。你试图抛弃这种功能,从而使你对ListView的使用毫无意义。

以编程方式填充LinearLayout根本不是什么大问题。你有行的XML文件吗?当你在数组中运行一个非常简单的for循环或通过光标进行while循环,绑定数据并将其添加到LinearLayout时,为每个新行充气。

完成。没有滚动问题......它全部由ScrollView处理,就像它应该的那样。

答案 1 :(得分:0)

试试这个,

private ListView first, second;
private String check="";
String[] COUNTRIES = new String[] { "Afghanistan", "Albania",
        "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla",
        "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia",
        "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain",
        "Montserrat", "Morocco",
        "Mozambique", "Myanmar", "Namibia", "Nauru", "NepalA",
        "Montserrat", "MoroccoF",
        "Mozambique", "Myanmar", "Namibia", "Nauru", "NepalB",
        "Montserrat", "MoroccoD",
        "Mozambique", "Myanmar", "Namibia", "Nauru", "NepalC",

};

String[] COUNTRIES1 = new String[] { "Martinique",
        "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia",
        "Moldova", "Monaco", "Mongolia", "Montserrat", "Morocco",
        "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", };
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    first = (ListView) findViewById(R.id.firstlist);
    second = (ListView) findViewById(R.id.secondlist);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, COUNTRIES);
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, COUNTRIES1);

    first.setAdapter(adapter);
    second.setAdapter(adapter1);

     second.setOnTouchListener(new OnTouchListener() {
     @Override
     public boolean onTouch(View arg0, MotionEvent arg1) {
         check="second";
     return false;
     }
      });

     first.setOnTouchListener(new OnTouchListener() {
         @Override
         public boolean onTouch(View arg0, MotionEvent arg1) {
             check="first";
         return false;
         }
          });
     first.setOnScrollListener(newScrollEvent);
     second.setOnScrollListener(newScrollEvent);
}

private OnScrollListener newScrollEvent = new OnScrollListener() {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        // TODO Auto-generated method stub
        if(check.equalsIgnoreCase("first")){
         System.out.println("first");

         if (first.getChildAt(0) != null) {
         Rect r1 = new Rect();
         first.getChildVisibleRect(first.getChildAt(0), r1, null);
         second.setSelectionFromTop(first.getFirstVisiblePosition(),
         r1.top);
         }
        }
        else if(check.equalsIgnoreCase("second")){
        if (second.getChildAt(0) != null) {
         Rect r1 = new Rect();
         second.getChildVisibleRect(second.getChildAt(0), r1, null);
         first.setSelectionFromTop(second.getFirstVisiblePosition(),
         r1.top);
         }
         }
    }
};

答案 2 :(得分:0)

像这样在运行时创建scrollview。

 //find parent xml layout
         LinearLayout lL1 =(LinearLayout)rootView.findViewById(R.id.pro_scroll);

        //Create ScrollView programatically 
            ScrollView _scrollView  = new ScrollView(appContext);
           _scrollView.setBackgroundColor(Color.WHITE);
           _scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
    LayoutParams.MATCH_PARENT));

        ViewGroup layout=new LinearLayout(appContext);
                           layout.setLayoutParams(new   ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

//add scrollview to layout 
                           layout.addView(_scrollView);
//add layout to xml parent layout
                           lL1.addView(layout);