我的内容很少,后面是Expandablelistview。我无法滚动整个布局。 我一直在寻找一个多星期的适当答案。提出一些答案。
答案 0 :(得分:14)
这个解决方案对我有用,当我在导航视图中使用自定义布局时,滚动视图与LinearLayout具有可扩展列表视图。
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header">
<ScrollView
android:fillViewport="true"
android:layout_marginTop="130dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/homeLayout"
android:clickable="true"
android:gravity="center_vertical"
android:background="@drawable/layout_click_effect"
android:layout_width="match_parent"
android:layout_height="45dp">
<ImageView
android:id="@+id/homeIv"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:src="@mipmap/ic_home_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home" />
</LinearLayout>
<View
android:background="@android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<ExpandableListView
android:id="@+id/topCatgExpLv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:groupIndicator="@null"
android:dividerHeight="1dp" />
</ScrollView>
</android.support.design.widget.NavigationView>
在你的onCreate
mListView = (ExpandableListView) findViewById(R.id.activity_expandable_list_view);
MyExpandableListAdapter adapter = new MyExpandableListAdapter(this,
mGroups);
mListView.setAdapter(adapter);
mListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
setListViewHeight(parent, groupPosition);
return false;
}
});
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();
}
//Add Divider Height
totalHeight += listView.getDividerHeight() * (listAdapter.getChildrenCount(i) - 1);
}
}
//Add Divider Height
totalHeight += listView.getDividerHeight() * (listAdapter.getGroupCount() - 1);
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();
}
答案 1 :(得分:6)
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollViewDrawer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/mainGray"
>
<ExpandableListView
android:id="@+id/expandableListCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>
这是你的片段:(或在你的活动中)
public class DrawerFragment extends Fragment implements, OnGroupExpandListener, OnGroupCollapseListener{
ScrollView scrollView;
ExpandableListView expListView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view rootView = inflater.inflate(R.layout.fragment_layout, container, false);
scrollView = (ScrollView) rootView.findViewById(R.id.scrollViewDrawer);
expListView = (ExpandableListView) rootView.findViewById(R.id.expandableListCategory);
....
}
@Override
public void onGroupExpand(int groupPosition) {
LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expListView.getLayoutParams();
param.height = (childCount * expListView.getHeight());
expListView.setLayoutParams(param);
expListView.refreshDrawableState();
scrollView.refreshDrawableState();
}
@Override
public void onGroupCollapse(int groupPosition) {
LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expListView.getLayoutParams();
param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
expListView.setLayoutParams(param);
expListView.refreshDrawableState();
scrollView.refreshDrawableState();
}
答案 2 :(得分:3)
首先,我假设你没有在滚动视图中使用多个子布局,因为滚动视图只能有一个直接子项。
其次,您不应在滚动视图中使用任何滚动组件,如可扩展列表 这是ListView inside ScrollView is not scrolling on Android。
的原因答案 3 :(得分:2)
我知道问题已经很久了,但可能对某人有帮助。基本上我的答案是Amit Tumkur和user2141833的答案的组合。经过大量的反复试验后,以下代码对我有用:
首先计算可扩展列表视图的初始高度,即整个事物崩溃时
for (Integer i = 0; i < mAdapter.getGroupCount(); i++) {
View groupItem = mAdapter.getGroupView(i, false, null, mExpandableListView);
groupItem.measure(mExpandableListView.getWidth(), View.MeasureSpec.UNSPECIFIED);
mInitialHeight += groupItem.getMeasuredHeight();
}
然后,当单击该组时,将“可扩展列表”视图的高度设置为“换行内容”
mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
//Other Expansion/Collapsing Logic
setListHeightToWrap();
return true;
}
});
setListHeightToWrap是一种不同的方法:
private void setListHeightToWrap() {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mExpandableListView.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
mExpandableListView.setLayoutParams(params);
mExpandableListView.refreshDrawableState();
mScrollView.refreshDrawableState();
}
然后在OnGroupExpandListener中将可扩展列表视图的高度设置为:
mExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mStitchingWorksListView.getLayoutParams();
//The Logic here will change as per your requirements and the height of each of the children in the group
if (mAdapter.getRealChildrenCount(groupPosition) > 6) {
params.height = 9 * mInitialHeight;
} else {
params.height = 6 * mInitialHeight;
}
//For Last Group in the list and the number of children were less as compared to other groups
if (groupPosition == mAdapter.getGroupCount() - 1) {
params.height = 3 * mInitialHeight;
}
mExpandableListView.setLayoutParams(params);
mExpandableListView.refreshDrawableState();
mExpandableListView.refreshDrawableState();
}
});
此外,布局是ScrollView内的LinearLayout内的ExpandableListView。
希望这有助于某人。 :)
答案 4 :(得分:1)
对于正在寻找可扩展列表视图到scrollview的用户,请使用视图而不是可扩展列表,请点击以下链接&amp;代码 -
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/linear_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
在Java Class中有类似的东西 -
for (int i = 0; i < pProductArrayList.size(); i++) {
LayoutInflater inflater = null;
inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView = inflater.inflate(R.layout.row_first, null);
final TextView mProductName = (TextView) mLinearView.findViewById(R.id.textViewName);
final RelativeLayout mLinearFirstArrow=(RelativeLayout)mLinearView.findViewById(R.id.linearFirst);
final ImageView mImageArrowFirst=(ImageView)mLinearView.findViewById(R.id.imageFirstArrow);
final LinearLayout mLinearScrollSecond=(LinearLayout)mLinearView.findViewById(R.id.linear_scroll);
if(isFirstViewClick==false){
mLinearScrollSecond.setVisibility(View.GONE);
mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
}
else{
mLinearScrollSecond.setVisibility(View.VISIBLE);
mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
}
mLinearFirstArrow.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(isFirstViewClick==false){
isFirstViewClick=true;
mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
mLinearScrollSecond.setVisibility(View.VISIBLE);
}else{
isFirstViewClick=false;
mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
mLinearScrollSecond.setVisibility(View.GONE);
}
return false;
}
});
答案 5 :(得分:0)
输入adapteSet
下面的代码enter code hereListAdapter listAdapter = listView.getAdapter();
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
答案 6 :(得分:0)
请试试,尝试一次。
1)在设置适配器后立即添加此行setExpandableListViewHeight(expandableListViewCategories)。
expandableListCategoriesAdapter = new ExpandableListCategoriesAdapter(getActivity(), listDataHeader, listDataChild);
expandableListViewCategories.setAdapter(expandableListCategoriesAdapter);
expandableListCategoriesAdapter.notifyDataSetChanged();
setExpandableListViewHeight(expandableListView);
2)将下面的内容复制并将其传递给setExpandableListViewHeight()方法。
private void setExpandableListViewHeight(ExpandableListView listView) {
try {
ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
int totalHeight = 0;
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View listItem = listAdapter.getGroupView(i, false, null, listView);
listItem.measure(0, 0);
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();
scrollBody.post(new Runnable() {
public void run() {
scrollBody.fullScroll(ScrollView.FOCUS_UP);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
注意:这会自动关闭您的ExpandableListView,您可以全视图滚动。
答案 7 :(得分:0)
尝试了太多的解决方案后,我将两种解决方案组合在一起就可以正常工作。菜单高度很重要。
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
LinearLayout.LayoutParams param = (LinearLayout.LayoutParams)
expandableListView.getLayoutParams();
param.height = (2 * menuHeight); //adjust 2 according to you
expandableListView.setLayoutParams(param);
expandableListView.refreshDrawableState();
findViewById(R.id.sv_menu).refreshDrawableState();
}
});
static int menuHeight =0; // declare globally
public static void setListViewHeightBasedOnChildren(ExpandableListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
View.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, ViewGroup.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
menuHeight = totalHeight;}
答案 8 :(得分:0)
从 API Level 21 (Lollipop) 开始,Android SDK 正式支持嵌套滚动容器。 View 和 ViewGroup 类中有很多方法提供了这个功能。要在 Lollipop 上实现嵌套滚动,您必须通过在其 XML 声明中添加 android:nestedScrollingEnabled="true" 或通过显式调用 setNestedScrollingEnabled(true) 来为子滚动视图启用它。