有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使ListView可滚动

我有两个xml文件。其中一个包含ListView和填充ListView的一个。我是 能够显示项目,但事情是,它不能滚动。例如,我有要显示的项目列表,但它不会自动启用滚动。应该增强代码的哪一部分以便允许滚动。我知道listView默认启用滚动。请帮我拿这个。提前谢谢

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

活动\查看\产品。xml

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

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


</LinearLayout>

列出项目。xml

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


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


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


</LinearLayout>

共 (1) 个答案

  1. # 1 楼答案

    我认为如果您使用RelativeLayout的约束来包含您的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>
    

    您的ListView的可视顶部将位于父视图的顶部,可视底部将位于父视图的底部,您的内容将与您的列表一样长,并滚动到需要的位置