2014-03-30 122 views
1

我想显示一些动态创建的gridviews,它们被添加到linearLayout的一些信息。这完美的作品,我想如何能够水平滚动网格。添加水平滚动到动态网格视图:Android

我已经尝试将Horizo​​ntalScrollView添加到网格,仍然无法正常工作。

我的代码:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
    android:id="@+id/Container" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent" 
    android:orientation="vertical" /> 
</ScrollView> 


public void buildContainer() { 
    String gridViewId = ""; 

    _Plans = _session.getPlanCollection(); 
    int headerCount = 0; 

    if (_Plans != null) { 

     for (KeyValuePair pair : _Plans) { 

      _planAdapter = new PlanAdapter(this.getActivity(), 
        pair.getValue()); 

      _container = (LinearLayout) this.getActivity().findViewById(
        R.id.plangrid); 
      TextView header = new TextView(getActivity()); 

      header.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)); 

      header.setText(pair.getKey()); 


      HorizontalScrollView scrollview = new HorizontalScrollView(
        getActivity()); 
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT); 



      GridView gridView = new GridView(getActivity()); 
      gridView.setLayoutParams(new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT)); 

      gridView.setVerticalSpacing(10); 
      gridView.setHorizontalSpacing(10); 
      gridView.setNumColumns(pair.getValue().size()); 
      gridView.setGravity(Gravity.CENTER); 
      gridView.setStretchMode(GridView.NO_STRETCH); 
      gridView.setColumnWidth(320); 
      gridView.setHorizontalScrollBarEnabled(true); 
      gridView.setHorizontalFadingEdgeEnabled(true); 


      if (!_headerSectionsRendered) { 
       headerCount++; 
       gridView.setAdapter(_planAdapter); 
       scrollview.addView(gridView); 
       _container.addView(scrollview, params); 
      } 
      if (headerCount == _Plans.size()) { 
       _headerSectionsRendered = true; 
      } 
     } 
    } 
} 

感谢您的帮助。

回答

1

试试这个:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
     android:descendantFocusability="blocksDescendants" <=== this 
    android:layout_height="match_parent"> 

    <LinearLayout 
    android:id="@+id/Container" 
    android:descendantFocusability="blocksDescendants" <=== and this 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent" 
    android:orientation="vertical" /> 
</ScrollView> 

加入这一行android:descendantFocusability="blocksDescendants"你的容器,将导致它使视图水平滚动,只要你尝试滚动它们在空的空间,而不是GridView的自己。