我有幾個內容由Expandable listview.I無法滾動整個佈局。 我一直在尋找適當的答案超過一個星期..推薦一些答案。scrollview內可擴展的列表視圖
回答
首先我想你在滾動視圖中不使用多個子佈局,因爲滾動視圖只能有一個直接子對象。
其次,你不應該使用任何滾動組件像可擴展列表,滾動視圖內 這裏爲什麼ListView inside ScrollView is not scrolling on Android。
對於誰是尋找可擴展列表視圖進入滾動型用戶,使用視圖,而不是擴展列表按照以下鏈接&代碼 -
<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類的一些事情像這個 -
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;
}
});
這種方法只適用於你有100個項目,因爲更多應用程序將凍結,直到所有項目的膨脹完成 –
你可以做到這一點很簡單。將滾動視圖的高度設置爲wrap_content。將scroll view中根視圖的高度設置爲wrap_content(如線性佈局),並將可展開列表視圖設置爲wrap_content。之後,設置OnGroupExpandListener和OnGroupCollapseListener爲您的可展開列表視圖併爲其設置高度,只要可展開列表視圖的子項。這樣的代碼: 佈局:
<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();
}
在計算onGroupExpand中的param.height時,我必須將1添加到子計數,以說明展開時,高度是標題加上子項的事實。 (這是假定頁眉佈局高度和子頁眉高度相同的尺寸。) – k2col
Works,但ExpandableListView爲空... –
該解決方案爲我工作,當我使用具有滾動導航視圖中的自定義佈局使用具有Expandable列表視圖的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();
}
}
}
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();
}
爲我工作。謝謝 –
工作像一個魅力。非常感謝@amit –
,但它只適用於當我點擊可擴展列表視圖的groupitem時。當我滾動視圖時它不工作 –
我知道這個問題是很老,但可能這將是對別人有幫助的。基本上我的答案是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();
}
然後擴展列表視圖即初始高度單擊組時設置可擴展列表視圖的高度爲Wrap內容
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();
}
});
而且佈置得ExpandableListView內的LinearLayout內滾動型。
希望這可以幫助別人。 :)
如果要滾動你的佈局,那麼你可以使用滾動條你佈置在下方輸入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();
- 1. 可擴展列表視圖
- 2. Android中的可擴展列表視圖
- 3. 可擴展的列表視圖問題
- 4. Android的可擴展列表視圖
- 5. iOS中的可擴展列表視圖
- 6. 可擴展的列表視圖android json
- 7. 改變列表視圖(可擴展列表視圖)的backgroundColor
- 8. 可擴展列表視圖滾動到列表視圖中
- 9. 刷新可擴展列表視圖
- 10. Android可擴展列表視圖
- 11. Android可擴展列表視圖 - 網頁
- 12. 可擴展列表視圖刷新
- 13. 拖放可擴展列表視圖
- 14. 可擴展列表視圖與片段
- 15. Windows Phone 8可擴展列表視圖
- 16. n級可擴展列表視圖
- 17. 可擴展列表視圖設計
- 18. 排序可擴展列表視圖
- 19. 可擴展列表視圖不顯示
- 20. Android:可擴展列表視圖實現
- 21. 要爲可擴展列表視圖
- 22. Android 2級可擴展列表視圖
- 23. Android可擴展列表視圖片段
- 24. Android可擴展列表視圖
- 25. Android可擴展列表視圖
- 26. 可擴展列表視圖android
- 27. 我可擴展的列表視圖不會擴展
- 28. 擴展列表視圖
- 29. 擴展列表視圖
- 30. 向上和向下滑動可擴展列表視圖內容
的代碼。 – Himani
https://stackoverflow.com/questions/29943600/how-can-i-put-a-expandablelistview-into-a-scrollview-without-it-collapsing/46660821#46660821 –
我已經解決了同樣的問題> https:/ /stackoverflow.com/questions/29943600/how-can-i-put-a-expandablelistview-into-a-scrollview-without-it-collapsing/46660821#46660821 –