2013-06-05 55 views
0

我有兩個xml文件。其中之一包含ListView和填充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); 
        } 
       }); 
      } 
     } 

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

看看這個答案:http://stackoverflow.com/questions/4310738/making-android-listview-layout-scrollable – verybadalloc

+0

你能編輯你的問題,包括你試過的不同答案嗎?它會幫助我縮小搜索範圍,解決您的問題:) – verybadalloc

回答

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的視覺頂部在父視圖的頂部,視覺底部將在父視圖的底部,併爲您的列表恰好是你的內容將是長,並滾動它需要的地方。

+1

listView現在滾動,感謝HalR。感謝您的直接和明確的答案。乾杯!〜 – rahstame