向动态GridView添加水平滚动:Android

向动态GridView添加水平滚动:Android,android,gridview,Android,Gridview,我试图显示动态创建的GridView上的一些信息,这些信息添加到linearLayout中。无论我多么希望能够水平滚动网格,这都非常有效 我已经尝试将水平滚动视图添加到网格中,但仍然不起作用 我的代码: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="matc

我试图显示动态创建的GridView上的一些信息,这些信息添加到linearLayout中。无论我多么希望能够水平滚动网格,这都非常有效

我已经尝试将水平滚动视图添加到网格中,但仍然不起作用

我的代码:

<?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;
            }
        }
    }
}

公共void buildContainer(){
字符串gridViewId=“”;
_计划=_session.getPlanCollection();
int headerCount=0;
如果(_Plans!=null){
对于(KeyValuePair对:_计划){
_planAdapter=新的planAdapter(this.getActivity(),
getValue());
_容器=(LinearLayout)this.getActivity().findViewById(
R.id.plangrid);
TextView头=新的TextView(getActivity());
header.setLayoutParams(新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_父级,
LinearLayout.LayoutParams.WRAP_内容);
header.setText(pair.getKey());
HorizontalScrollView scrollview=新的HorizontalScrollView(
getActivity());
LinearLayout.LayoutParams params=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_内容,
LinearLayout.LayoutParams.WRAP_内容);
GridView GridView=新的GridView(getActivity());
gridView.setLayoutParams(新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_内容,
LinearLayout.LayoutParams.WRAP_内容);
gridView.setVerticalSpacing(10);
gridView.setHorizontalSpacing(10);
setNumColumns(pair.getValue().size());
gridView.setGravity(重力中心);
gridView.setStretchMode(gridView.NO_STRETCH);
gridView.setColumnWidth(320);
setHorizontalScrollBarEnabled(true);
gridView.setHorizontalFadingEdgeEnabled(真);
如果(!\u标题部分已指定){
headerCount++;
setAdapter(_planAdapter);
scrollview.addView(gridView);
_container.addView(滚动视图,参数);
}
if(headerCount==\u Plans.size()){
_headerSectionsRendered=真;
}
}
}
}
谢谢你的帮助。

试试这个:

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