您好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>
但我聽說的是,你不能把listview內滾動視圖! – user3781907
通常我們不把列表視圖放在滾動視圖中,因爲列表視圖有它自己的默認滾動功能,這就是我們爲什麼不放。但它不是強制性的,你不能做這件事。你可以在UI中放置儘可能多的列表視圖。但是你已經停止了列表視圖的滾動功能。 –
只需嘗試我的代碼。實現我發送給你的自定義函數setListViewHeightBasedOnChildren()。你會明確地得到你的結果。如果執行中有任何問題,請告訴我。 –