我有一个包含标题,图像和ExpandableListView的布局。我的问题是,虽然标题不必滚动,但我希望图像和ExpandaleListView是一个可以滚动的唯一块。
直到现在,除非我添加高度值,否则我看不到ExpandibleListView。
这是我的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/title"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/userContent"
android:layout_width="match_parent"
android:layout_height="175dp"
android:background="@color/black"
android:gravity="center_vertical">
<RelativeLayout
android:id="@+id/userDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent" >
<ImageView
android:id="@+id/ImgDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="@string/app_name"
android:scaleType="centerCrop"
android:src="@drawable/ic_user" />
</RelativeLayout>
</LinearLayout>
<ExpandableListView
android:id="@+id/lvExp"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:focusable="false"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
答案 0 :(得分:3)
解决了将图像放入名为“header”的布局中,然后将此代码放入活动中:
final ExpandableListView elv = (ExpandableListView) v.findViewById(R.id.lvExp);
View header = inflater.inflate(R.layout.header, null);
elv.addHeaderView(header);
答案 1 :(得分:0)
您的布局文件嵌套太深,您应该删除一些不必要的组,请尝试下面的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/title"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/ImgDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="@string/app_name"
android:scaleType="centerCrop"
android:src="@drawable/ic_user" />
<ExpandableListView
android:id="@+id/lvExp"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:focusable="false"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
答案 2 :(得分:0)
最短和最短的ScrollView问题中ListView的最简单解决方案。
您不必在layout.xml文件中执行任何特殊操作,也不必处理父ScrollView上的任何内容。您只需处理子ListView。您还可以使用此代码在ScrollView&amp;中使用任何类型的子视图。执行触摸操作。
只需在java类中添加以下代码行:
ListView lv = (ListView) findViewById(R.id.layout_lv);
lv.setOnTouchListener(new OnTouchListener() {
// Setting on Touch Listener for handling the touch inside ScrollView
@Override
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
如果将ListView放在ScrollView中,则所有ListView都不会伸展到其全高。以下是解决此问题的方法
/**** Method for Setting the Height of the ListView dynamically.
**** Hack to fix the issue of not showing all the items of the ListView
**** when placed inside a ScrollView ****/
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.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
要使用此方法,只需在此方法中传递ListView:
ListView list = (ListView) view.findViewById(R.id.ls);
setListViewHeightBasedOnChildren(list);
与ExpandableListView一起使用
ExpandableListView: view = listAdapter.getView(0, view, listView);
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT, View.MeasureSpec.EXACTLY);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY);
view.measure(widthMeasureSpec, heightMeasureSpec);
对于具有可变项目高度的ListView,请使用以下链接