可以滚动列表视图并启用整个布局

时间:2014-12-25 10:07:34

标签: android android-layout listview android-listview vertical-scrolling

嗨iam目前正在开发一个Android应用程序,它在主要活动中有两个列表视图。我想要的是禁用滚动两个列表并允许整个页面只滚动,有什么办法可以帮助。 .... 我的代码        package com.example.listviewdemo;

 import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import android.widget.Toast;

public class MainActivity extends Activity {

ListView list,list2;
String[] name={"Happy","always","try","hard","you will","get it!","Believe","in","God","everything","will","work well!","Believe","in","God","everything","will","work well!"};
String[] name2={"Believe","in","God","everything","will","work well!","Believe","in","God","everything","will","work well!"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list = (ListView) findViewById(R.id.listview);
    list2 = (ListView) findViewById(R.id.listview2);
    list.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,name));
  list2.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,name2));



    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), name [position],Toast.LENGTH_SHORT).show();
        }
    });

list2.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), name2 [position],Toast.LENGTH_SHORT).show();
        }
    });   
}   
  }

我的xml代码是:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
     android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:orientation="vertical"
     tools:context="${relativePackage}.${activityClass}" >
    <TextView
    android:id="@+id/text_id1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
  android:text="@string/str1" />
   <ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
     android:layout_height="wrap_content"
    />
    <TextView
      android:id="@+id/text_id2"
     android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/str2" 

     />

      <ListView
    android:id="@+id/listview2"
    android:layout_width="match_parent"
     android:layout_height="wrap_content"
       />
      </LinearLayout>

3 个答案:

答案 0 :(得分:13)

你可以试试这个。

FOR xml PART DO THIS:

将整个布局数据放在一个滚动视图下,例如:

    <ScrollView
        android:id="@+id/scrollViewId"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >             

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

                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" /> // SAY YOUR FIRST LIST VIEW:

                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" /> // SAY YOUR SECONDLIST VIEW:

                 // Add your other views as per requirement....

        </LinearLayout>

    </ScrollView>

现在JAVA CLASS做了以下事情......

在将适配器设置为列表视图后,只需将此自定义方法添加到代码中:

setListViewHeightBasedOnChildren(listview)

例如:

      list = (ListView) findViewById(R.id.listview);
      list.setAdapter(new ArrayAdapter<String> 
                      (MainActivity.this,android.R.layout.simple_list_item_1,name));
      setListViewHeightBasedOnChildren(list);

对第二个列表视图也这样做。

这是setListViewHeightBasedOnChildren METHOD

的主体
   public static void setListViewHeightBasedOnChildren(ListView listView) 
{
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight=0;
    View view = null;

    for (int i = 0; i < listAdapter.getCount(); i++) 
    {
        view = listAdapter.getView(i, view, listView);

        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,  
                                      LayoutParams.MATCH_PARENT));

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();

    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));

    listView.setLayoutParams(params);
    listView.requestLayout();

}

希望它适合你。

答案 1 :(得分:2)

你不应该将滚动容器放在其他滚动容器中(包含listView的ScrollView) 即使你设法使它工作也会产生问题。

请考虑重新设计您的布局,例如动态添加布局以滚动视图或删除滚动视图并使用带有页眉和/或页脚视图的列表视图

答案 2 :(得分:0)

scroolview只允许一个孩子。所以为了解决你的问题,你需要在xml里面创建scrollview,在linearLayout里面和在LinearLayout里面放置你所有的内容。

并且要禁用列表滚动,您可以在MainActivty中使用:list .setVerticalScrollBarEnabled(false);

<ScrollView
       android:id="@+id/scrollView1"
       android:layout_width="match_parent"
       android:layout_height="match_parent" >
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical" >
           <TextView
               android:id="@+id/text_id1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="@string/str1" 
               />
           <ListView
               android:id="@+id/listview"
               android:layout_width="match_parent"
               android:layout_height="wrap_content" >
           </ListView>
           <TextView
               android:id="@+id/text_id2"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="@string/str2" />
           <ListView
               android:id="@+id/listview2"
               android:layout_width="match_parent"
               android:layout_height="wrap_content" >
           </ListView>
       </LinearLayout>
   </ScrollView>