其他分享
首页 > 其他分享> > Android NestedScrollView在ListView中仅显示一个项目

Android NestedScrollView在ListView中仅显示一个项目

作者:互联网

有人知道我的版式有什么问题吗?我无法弄清楚为什么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_width="match_parent"
    android:layout_height="match_parent"
    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_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/one">

        <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            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_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/White"
                android:padding="10dp">

                <RelativeLayout
                    android:id="@+id/headerThisNow"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/border_bottom">

                    <Button
                        android:id="@+id/btnCloseWordOfTheDay"
                        style="?android:attr/buttonStyleSmall"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        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_width="wrap_content"
                        android:layout_height="wrap_content"
                        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_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/headerThisNow">

                    <TextView
                        android:id="@+id/wordNow"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        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_width="wrap_content"
                        android:layout_height="wrap_content"
                        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_width="wrap_content"
                        android:layout_height="wrap_content"
                        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_width="wrap_content"
                        android:layout_height="wrap_content"
                        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_width="match_parent"
                        android:layout_height="40dp"
                        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_width="35dp"
                            android:layout_height="35dp"
                            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_width="35dp"
                            android:layout_height="35dp"
                            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_width="100dp"
                            android:layout_height="40dp"
                            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_width="match_parent"
            android:layout_height="wrap_content"
            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>

我正在使用“片段活动”和SimpleCusrsorAdapter将项目填充到我的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,但这并不能给我预期的输出.

任何帮助,将不胜感激.非常感谢你!!!

解决方法:

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);
}

标签:android,android-studio,listview,android-listview,nestedscrollview
来源: https://codeday.me/bug/20191011/1891988.html