3

您好iam目前正在一個android應用程序它有兩個列表視圖內的主activity.What我想要的是禁用滾動的兩個列表,並允許整個頁面滾動只,是有什麼辦法,請幫忙..... 我的代碼 package com.example.listviewdemo;DIsable滾動列表視圖和啓用整個佈局

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> 

回答

11

您可以試試這個。

FOR XML部分做到這一點:

把下一個滾動查看整個佈局數據,例如:

<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> 

NOW IN JAVA CLASS DO以後的事......

剛將適配器設置爲列表視圖後,將此自定義方法添加到您的代碼中:

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方法的身體

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(); 

} 

希望它爲你工作。

+0

但我聽說的是,你不能把listview內滾動視圖! – user3781907

+0

通常我們不把列表視圖放在滾動視圖中,因爲列表視圖有它自己的默認滾動功能,這就是我們爲什麼不放。但它不是強制性的,你不能做這件事。你可以在UI中放置儘可能多的列表視圖。但是你已經停止了列表視圖的滾動功能。 –

+0

只需嘗試我的代碼。實現我發送給你的自定義函數setListViewHeightBasedOnChildren()。你會明確地得到你的結果。如果執行中有任何問題,請告訴我。 –

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> 
1

你不應該把其他滾動容器(含滾動型的ListView)內滾動容器 即使你管理,使其工作就會產生問題。

請考慮重新設計您的佈局或例如動態添加布局以滾動視圖或擺脫滾動視圖並使用帶有頁眉和/或頁腳視圖的列表視圖

+0

http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/ –

相關問題