简体   繁体   中英

Making the ListView Scrollable

I have two xml files. One of them contains the ListView and the one who populates the listView. I was able to display the items but the thing is, it can't be scrolled. For example, I have list of items to display but it will not automatically enables scroll. Which part of the code should be enhanced so that it will allow scrolling. I know that listView enables scrolling by default. Please help me with this one. Thanks in advance.

    public class ActivityViewCategory extends ListActivity {

                @Override
                public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_view_products);

                // Hashmap for ListView
                productsList = new ArrayList<HashMap<String, String>>();
                // Get listview
                ListView lv = getListView();
                            .
                            .
                            .
     protected void onPostExecute(String file_url) {
                // dismiss the dialog after getting all products
                pDialog.dismiss();
                // updating UI from Background Thread
                runOnUiThread(new Runnable() {
                    public void run() {
                        /**
                         * Updating parsed JSON data into ListView
                         ArrayList<HashMap<String, String>> com.example.restotrial.ActivityViewCategory.productsList * */

                        ListAdapter adapter = new SimpleAdapter(
                                ActivityViewCategory.this, productsList,
                                R.layout.list_item, new String[] { TAG_PID,
                                        TAG_NAME},
                                new int[] { R.id.pid, R.id.name });
                        // updating listview
                        setListAdapter(adapter);
                    }
                });
            }
        }

activity_view_products.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <!-- Main ListView 
         Always give id value as list(@android:id/list)
    -->

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="456dp" >
    </ListView>


</LinearLayout>

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
    <TextView
        android:id="@+id/pid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />


    <!-- Name Label -->
    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:textSize="17dip"
        android:textStyle="bold" />


</LinearLayout>

I think it would work better if you used the constraints of a RelativeLayout to contain your ListView .

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">
    <!-- Main ListView
         Always give id value as list(@android:id/list)
    -->

    <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content" >
    </ListView>


</RelativeLayout>

The visual top of your ListView will be at the top of the parent view, the visual bottom will be at the bottom of the parent view, and your content will be as long as your list happens to be, and scroll where it needs to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM