Android NestedScrollView 在 ListView 中仅显示一项
Posted
技术标签:
【中文标题】Android NestedScrollView 在 ListView 中仅显示一项【英文标题】:Android NestedScrollView showing only one Item in ListView 【发布时间】:2016-03-12 19:00:17 【问题描述】:有人知道我的布局有什么问题吗?我无法弄清楚为什么我的 ListView 只显示一项。
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:background="@color/grey_200"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.rostata.alejo.vbmobileapp.ActivityResult"
tools:showIn="@layout/activity_home">
<RelativeLayout
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/one">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_
android:layout_
android:layout_gravity="center"
android:layout_margin="0dp"
android:background="@color/White"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_marginTop="20dp"
app:cardCornerRadius="4dp"
app:cardElevation="2dp">
<RelativeLayout
android:id="@+id/relativeGridInside"
android:layout_
android:layout_
android:background="@color/White"
android:padding="10dp">
<RelativeLayout
android:id="@+id/headerThisNow"
android:layout_
android:layout_
android:background="@drawable/border_bottom">
<Button
android:id="@+id/btnCloseWordOfTheDay"
style="?android:attr/buttonStyleSmall"
android:layout_
android:layout_
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/ic_window_close"
android:text="" />
<TextView
android:id="@+id/textView9"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:fontFamily="sans-serif-light-bold"
android:text="Word of the Day"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/grey_700"
android:textSize="25sp" />
</RelativeLayout>
<RelativeLayout
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/headerThisNow">
<TextView
android:id="@+id/wordNow"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="5dp"
android:fontFamily="sans-serif-light-bold"
android:text="Paghurop-hurop"
android:textColor="@color/grey_700"
android:textSize="20sp" />
<TextView
android:id="@+id/thisPronounceNow"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/wordNow"
android:fontFamily="sans-serip-light"
android:text="-Pag-hu-rop-hu-rop-"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/grey_400"
android:textStyle="italic" />
<TextView
android:id="@+id/maningTitle"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/thisPronounceNow"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:text="Meaning"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/thisMeaningNow"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/maningTitle"
android:ellipsize="end"
android:maxLines="2"
android:text="If ever na mabasa nindu ini, sample lang pu ni, maung comment please"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RelativeLayout
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/thisMeaningNow"
android:layout_marginTop="10dp">
<Button
android:id="@+id/btnVoice"
style="?android:attr/buttonStyleSmall"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/btnMore"
android:background="@drawable/ic_voice"
android:padding="0dp"
android:text="" />
<ToggleButton
android:id="@+id/checkBox"
android:layout_
android:layout_
android:layout_alignTop="@+id/btnVoice"
android:layout_toEndOf="@+id/btnVoice"
android:layout_toRightOf="@+id/btnVoice"
android:layout_marginLeft="10dp"
android:background="@drawable/toogle_favorite_check"
android:text=""
android:textOff=""
android:textOn="" />
<Button
android:id="@+id/btnMore"
style="?android:attr/buttonStyleSmall"
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="@drawable/btn_more"
android:text="More"
android:textColor="@color/White" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
<ListView
android:layout_
android:layout_
android:layout_below="@+id/card_view"
android:id="@+id/words_list_view"
android:divider="@color/transparent"
android:scrollbars="none"
android:layout_weight="1"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
我正在使用 A Fragment Activity 和 SimpleCursorAdapter 将项目填充到我的 ListView。万一,这是我的课;
public class ActivityHome extends Fragment
CardView cardViewWordOfTheDay;
private WordsDbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
private String wordFromSpinner = "ALL";
ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
final View view = inflater.inflate(R.layout.layout, container, false);
Button btnCloseWordOfTheDay = (Button) view.findViewById(R.id.btnCloseWordOfTheDay);
listView = (ListView) view.findViewById(R.id.words_list_view);
btnCloseWordOfTheDay.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
cardViewWordOfTheDay = (CardView) view.findViewById(R.id.card_view);
cardViewWordOfTheDay.setVisibility(View.GONE);
);
dbHelper = new WordsDbAdapter(getActivity().getApplicationContext());
dbHelper.open();
displayListView(wordFromSpinner);
return view;
private void displayListView(String wordFromSpinnerData)
Cursor cursor = null;
try
cursor = dbHelper.fetchAllWords(wordFromSpinnerData);
catch (Exception e)
Toast.makeText(getActivity().getApplicationContext(), "EER " + e, Toast.LENGTH_LONG).show();
String[] columns = new String[]
WordsDbAdapter.KEY_ONE, WordsDbAdapter.KEY_TWO,
WordsDbAdapter.KEY_THREE, WordsDbAdapter.KEY_FOUR, WordsDbAdapter.KEY_FIVE
;
int[] to = new int[]
R.id.thisWord, R.id.thisSub, R.id.thisWordFrom, R.id.thisWordLocation, R.id.thisLetter
;
dataAdapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.words_list_item, cursor, columns, to, 0);
listView.setAdapter(dataAdapter);
我很困惑。我不知道发生了什么。当我尝试将我的 RelativeLayout 的高度更改为500dp
时,listView 会显示其他项目,但不是全部。所以我想我需要它是 wrap_parent
但这并没有给我预期的输出。
任何帮助将不胜感激。非常感谢!!!
【问题讨论】:
【参考方案1】:如here所述,
你可以在listView.setAdapter(dataAdapter);
之后调用这个方法
public static void setListViewHeightBasedOnChildren(ListView 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, 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);
【讨论】:
imgur.com/a/yfYfk 没有图片的截图是数据的样子,有图片的是我得到的。我尝试删除所有填充,在列表视图中添加填充,滚动视图等以上是关于Android NestedScrollView 在 ListView 中仅显示一项的主要内容,如果未能解决你的问题,请参考以下文章
Android之解决NestedScrollView嵌套RecyclerView部分手机返回到这个页面Recyclerview顶部,而不是页面NestedScrollView顶部
如何在Android中设置NestedScrollView的最大高度?
Android - 包含 ExpandableListView 的 NestedScrollView 在展开时不会滚动
Android:TextInputLayout 不适合页面并阻止页面在 NestedScrollView 内滚动