Domanda

I have a listview that is populated from an asynchronous task that gets data from the server. However, when the listview first appears, the scrollbar shows up and then immediately goes away. My main activity extends MapActivity since I display a map above the listview. Does anyone know why the scrollbar disappears and how to correct it? Below is my main layout.

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

Here is my custom layout for the listview.

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

And below is the logic for creating the listview:

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

        }); 

    }
È stato utile?

Soluzione

There are two ways to stop your scroll bars from fading from your ListView. You only need to do one of these.

In code:

restaurant_list.setScrollbarFadingEnabled(false);

In the xml:

android:fadeScrollbars="false"

Reference

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top