我在ExpandableListView
内有一个NestedScrollView
(是的,我知道,在另一个滚动视图中有一个滚动视图并不好,但我不知道还能做什么,请告诉我我,如果有人知道更好的方法)。
NestedScrollView
中的内容大小仍在屏幕内,因此不会滚动,但展开ExpandableListView
时,内容会泄漏到屏幕外但NestedScrollView
仍然不会滚动..为什么会这样?
这是我的NestedScrollView
布局:
<NestedScrollView>
<LinearLayout>
<LinearLayout></LinearLayout>
... // About 3 of the LinearLayouts
<ExpandableListView/>
</LinearLayout>
</NestedScrollView>
答案 0 :(得分:51)
您可以使用NonScrollExpandableListView
通过覆盖以下方法来获得任何Lisview
或GridView
或ExpandableListView
的非滚动属性。
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
因此,对于使用NonScrollExpandableListView
,您需要创建一个自定义类。
public class NonScrollExpandableListView extends ExpandableListView {
public NonScrollExpandableListView(Context context) {
super(context);
}
public NonScrollExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollExpandableListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
并使用它。
<com.example.extraclasses.NonScrollExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
快乐的编码。
答案 1 :(得分:3)
使用此方法,这将在运行时计算ExpendableListSize。
private void setListViewHeight(ExpandableListView listView,
int group) {
ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
int totalHeight = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
View.MeasureSpec.EXACTLY);
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View groupItem = listAdapter.getGroupView(i, false, null, listView);
groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += groupItem.getMeasuredHeight();
if (((listView.isGroupExpanded(i)) && (i != group))
|| ((!listView.isGroupExpanded(i)) && (i == group))) {
for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
View listItem = listAdapter.getChildView(i, j, false, null,
listView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
int height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
if (height < 10)
height = 200;
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();
}
在下面的setOnGroupClickListener.like中调用此方法
mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
setListViewHeight(parent, groupPosition);
return false;
}
});
答案 2 :(得分:1)
我们不能在scrollview中使用listview,gridview或expandable listview。如果您不想在scrollview中使用可扩展列表视图,那么您必须为可扩展列表视图提供一些固定高度。
答案 3 :(得分:1)
V-rund Puro-hit的答案对我有用。但是需要一些修改才能与Kotlin支持API> 19一起使用。因此,为了节省时间,这里是:
创建一个新的类文件NonScrollExpandableListView
:
import android.content.Context
import android.util.AttributeSet
import android.widget.ExpandableListAdapter
import android.widget.ExpandableListView
class NonScrollExpandableListView : ExpandableListView {
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {}
override fun setAdapter(adapter: ExpandableListAdapter?) {
super.setAdapter(adapter)
}
override fun setOnChildClickListener(onChildClickListener: OnChildClickListener) {
super.setOnChildClickListener(onChildClickListener)
}
override fun expandGroup(groupPos: Int) : Boolean {
return super.expandGroup(groupPos)
}
override fun expandGroup(groupPos: Int, animate: Boolean) : Boolean {
return super.expandGroup(groupPos, animate)
}
override fun isGroupExpanded(groupPosition: Int): Boolean {
return super.isGroupExpanded(groupPosition)
}
public override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE shr 2, MeasureSpec.AT_MOST)
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom)
val params = layoutParams
params.height = measuredHeight
}
}
...我像这样使用它:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<com.example.you.kotlinlistview.NonScrollExpandableListView
android:id="@+id/expandableCategories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/add_new_feed"
android:drawableStart="@drawable/ic_add"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Add new feed"
/>
<Button
android:id="@+id/settings_button"
android:drawableStart="@drawable/ic_settings"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Settings"
/>
<Button
android:id="@+id/logout_button"
android:drawableStart="@drawable/ic_exit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Log Out"
/>
</LinearLayout>
</ScrollView>
因此,我可以轻松地将按钮和其他元素添加到NavigationView
的侧面抽屉中,并且它们在一个常见的ScrollView
中都能很好地工作。这里的主要用法是当您需要在一个常见的ListView
内合并多个ExpandableListView
和ScrollView
时,这会解决滚动问题。
答案 4 :(得分:0)
将android:nestedScrollingEnabled="true"
添加到您的ExpandalbleListView布局。