bernzkie
bernzkie

Reputation: 1269

Android NestedScrollView showing only one Item in 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>

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

    }

}

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!!!

Upvotes: 2

Views: 2499

Answers (1)

Mohamed
Mohamed

Reputation: 656

As mentioned here,

You can call this method after 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);
}

Upvotes: 5

Related Questions