Android NestedScrollView在ListView中仅显示一个项目 [英] Android NestedScrollView showing only one Item in ListView

查看:63
本文介绍了Android NestedScrollView在ListView中仅显示一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道我的版式有什么问题吗?我无法弄清楚为什么ListView只显示一项.

Anyone knows what's wrong in my layout? I'm not able to figure out if why is my ListView is showing only one item.

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>

我正在使用Fragment Activity和SimpleCusrsorAdapter将项目填充到我的ListView中.如果是这样,这是我的课程;

I am using A Fragment Activity and SimpleCusrsorAdapter to populate an items to my ListView. in case of, this is my class;

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,但这并不能满足我的预期输出要求.

I'm so confused. i dont what happen. when i try to change the height of my RelativeLayout to 500dp then the listView is showing the other items but not all. so i think i need it to be wrap_parent but this is not giving me the expected ouput.

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

Any help would be appreciated. Thank You Very Much!!!

推荐答案

此处所述,

您可以在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 NestedScrollView在ListView中仅显示一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆