[英]how to solve scrollview listview in linearlayout in android
在這里我在linearlayout中使用scrollview。但是我想在我的scrollview linearlayout中包含另一個listview。
Scrollview只支持一個孩子。我知道scrollview listview內部不起作用。但是還有其他選擇可以使用listview。我想滾動所有內容。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/widget54"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:layout_width="310px"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 1"
/>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 2"
/>
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 3"
/>
<EditText
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="300px"
/>
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 4"
/>
</LinearLayout>
</ScrollView>
ScrollView中的ListView非常確定不能正常工作。
我能想到的唯一方法是,您在界面中只有一個listview,並為此創建一個自定義適配器:
public class HeaderAdapter{
private Adapter mAdapter; // this adapter is the adapter that you will normally have, for your listview
getViewTypeCount(){
return mAdapter.getViewTypeCount() + 1;
}
getView(int pos, View convertView){
// Handle only the first position, othervise, let mAdapter return
if (pos!=0) return mAdapter.getView(pos-1, convertView);
//
View v = LayoutInflater.inflate(R.layout.other_linear_layout);
// Bind the v if necessary
return v;
}
}
上面僅是一個示例,您可以將其余內容(線性布局,按鈕,edittext等)視為適配器的“標題”行。
如果ScrollView中只有一個ListView,則可以在布局XML中將屬性添加到ScrollView: android:fillViewport="true"
。
這將擴展ListView的所有內容。
否則,如果ScrollView中有多個ListView或GridView,則需要創建一個customListView。
擴展ListView和@Override onMeasure:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.