2013-06-05 52 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