2

我有一个从服务器获取数据的异步任务填充的列表视图。但是,当列表视图第一次出现时,滚动条会出现,然后立即消失。我的主要活动扩展了 MapActivity,因为我在列表视图上方显示了一张地图。有谁知道为什么滚动条消失以及如何纠正它?下面是我的主要布局。

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
 >

<TextView
    android:text="Address is: "
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/address_view" />

<com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapvw"
    android:layout_width="match_parent" 
    android:layout_height="250dp" 
    android:enabled="true"
    android:clickable="true"
    android:apiKey="xxxxxxxxxxxxxxxxxx"
    />
<ListView 
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:scrollbars="vertical"
    android:drawSelectorOnTop="false" />

</LinearLayout>

这是我的列表视图的自定义布局。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >


  <TextView
    android:id="@+id/viewtext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingLeft="6sp"
    android:textColor="#000000"
    android:textSize="15sp"
    android:paddingRight="10sp"
    />
   </LinearLayout>

下面是创建列表视图的逻辑:

@Override
    protected void onPostExecute(ArrayList<String> result) {
 ......... some code

 List<String> viewline = new ArrayList<String>();
   ..... some code
   mapView.postInvalidate();

        final ArrayAdapter<String> arrayAdpt = new ArrayAdapter<String>(getApplicationContext(), R.layout.listview, R.id.viewtext, viewline);

        ListView restaurant_list = (ListView) findViewById(R.id.list);
        restaurant_list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        restaurant_list.setAdapter(arrayAdpt);
        restaurant_list.setScrollContainer(true);
        restaurant_list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "You have selected " + String.valueOf(arg2) + " arg3=" + String.valueOf(arg3) , Toast.LENGTH_LONG).show();
            }

        }); 

    }
4

1 回答 1

5

有两种方法可以阻止滚动条从ListView. 您只需要执行其中一项。

在代码中:

restaurant_list.setScrollbarFadingEnabled(false);

在 xml 中:

android:fadeScrollbars="false"

参考

于 2013-03-12T19:52:56.947 回答