gpt4 book ai didi

java - 无法在其父 NestedScrollView 中滚动 ListView - Android

转载 作者:行者123 更新时间:2023-12-01 07:02:25 24 4
gpt4 key购买 nike

我在 NestedScrollView 中使用 ListView,但在 ListView 中看不到超过 1 个项目。我可以滚动到 TextView (textContact) 的末尾,但无法在 ListView (listContact) 内滚动.

这是 .xml 文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
... >
<android.support.design.widget.CoordinatorLayout
... >
<LinearLayout
...
android:orientation="vertical">

<android.support.v7.widget.Toolbar
... />
<android.support.v4.widget.NestedScrollView
... >
<LinearLayout
...
android:orientation="vertical">

<TextView
android:id="@+id/textContact"
... />
<LinearLayout
...
android:orientation="vertical">
<ListView
android:id="@+id/listContact"
... />
</LinearLayout>
</LinearLayout>

</android.support.v4.widget.NestedScrollView>

</LinearLayout>

</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
... />

</android.support.v4.widget.DrawerLayout>

这里使用的小部件/控件是否存在任何设计问题、任何兼容性问题或其他问题?

最佳答案

您需要在NestedScrollView中启用以下两个属性,

<android.support.v4.widget.NestedScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

然后使用辅助类来扩展 ListView 宽度并修复滚动功能,

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Helper {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
}

要在设置 ListView 适配器时使用辅助类,

listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));
Helper.getListViewSize(listView);

PS:确保您已为您的项目添加了支持设计库。

compile 'com.android.support:design:23.4.0'

关于java - 无法在其父 NestedScrollView 中滚动 ListView - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39104713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com