2014-03-30 125 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的自己。