Android - NestedScrollView, который содержит ExpandableListView, не прокручивается при расширении
У меня есть ExpandableListView
внутри NestedScrollView
(да, я знаю, неплохо иметь прокрутку в другом прокручиваемом представлении, но я не знаю, что еще делать, пожалуйста, скажите мне, знает ли кто-нибудь лучший подход).
Размер содержимого в NestedScrollView
по-прежнему находится на экране, поэтому он не будет прокручиваться, но когда ExpandableListView
будет расширен, содержимое будет течь вне экрана, но NestedScrollView
по-прежнему не будет прокручиваться. Почему это так?
Здесь мой макет NestedScrollView
:
<NestedScrollView>
<LinearLayout>
<LinearLayout></LinearLayout>
... // About 3 of the LinearLayouts
<ExpandableListView/>
</LinearLayout>
</NestedScrollView>
Ответы
Ответ 1
Вы можете использовать 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" />
Счастливое кодирование.
Ответ 2
Используйте этот метод, который будет вычислять 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;
}
});
Ответ 3
Мы не можем использовать listview, gridview или расширяемое listview внутри scrollview. Если вы не будете использовать расширяемое listview внутри scrollview, вы должны указать некоторую фиксированную высоту в расширяемое listview.
Ответ 4
Добавьте android:nestedScrollingEnabled="true"
в макет ExpandalbleListView.
Ответ 5
Ответ от 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
которое позаботится о прокрутке.