1

I have a listview in Fragment, which includes some list elements.

Here is the xml-layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".uiFragments.WikiFragment">

    <LinearLayout
        android:id="@+id/tablayout"
        android:layout_below="@+id/wiki_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <ListView
        android:id="@+id/list"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginTop="5dp"/>

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_below="@+id/tv_header"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"/>

    </LinearLayout>

</FrameLayout>

here is a part of the code from this fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_wiki, container, false);

        //ListView
        listView = (ListView) view.findViewById(R.id.list);

        // Defined Array values to show in ListView
        String[] values = new String[] { "Android List View",
                "Adapter implementation",
                "Simple List View In Android",
                "Create List View Android",
        };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, android.R.id.text1, values);

        // Assign adapter to ListView
        listView.setAdapter(adapter);

        // ListView Item Click Listener
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                // ListView Clicked item index
                int itemPosition     = position;

                // ListView Clicked item value
                String  itemValue    = (String) listView.getItemAtPosition(position);

                // Show Alert
                Toast.makeText(getContext().getApplicationContext(),
                        "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
                        .show();

            }

return view;
        });

What I would like to have is like in the picture:

enter image description here At the moment, all my list-items will be displayed. But I want to display for example only 2 list-items and the rest shall be scrollable choosen, like in the image above.

This is what I get now.

enter image description here

If someone could me help out, I would be very happy. Thanks

  • Have you tried to set a fixed height to your ListView? – Locdoc01 Apr 17 '18 at 21:41
  • you mean like android:layout-height = "40dp"? Yes, I tried out something, but could not see some differences..maybe I am doing it wrong – New GenerationFashion Apr 17 '18 at 21:55
  • Mmh, that's odd. Actually it should do the trick. I tried your layout. When I change only layout_height of the listview to e.g. 100dp, it shows only two items from four with the rest scrollable, just like you want. Did you post your complete layout? Cause you have android:layout_below="@+id/tv_header" in your PdfView, but I don't see the View tv_header. By the way, layout_below is not a valid layout parameter in linearlayour ;) – Locdoc01 Apr 17 '18 at 22:50
  • @Locdoc01 thanks! I tried out, and it show me much less items, what I want! Thank you :) – New GenerationFashion Apr 18 '18 at 07:37
  • Possible duplicate of [Dynamically add elements to a listView Android](https://stackoverflow.com/questions/4540754/dynamically-add-elements-to-a-listview-android) –  Apr 20 '18 at 05:20

0 Answers0