将水平滚动添加到动态gridview:Android

时间:2014-03-30 15:34:11

标签: android gridview

我正在尝试在动态创建的网格视图上显示一些信息,这些信息会添加到linearLayout中。这非常有效,我希望能够水平滚动网格。

我尝试将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 个答案:

答案 0 :(得分: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"添加到您的容器中会导致视图水平滚动,只有当您尝试在空白处滚动它们时,而不是网格视图本身。