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

标签 android gridview

我正在尝试显示有关动态创建的 GridView 的一些信息,这些 View 被添加到 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;
            }
        }
    }
}

感谢您的帮助。

最佳答案

试试这个:

<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" 添加到您的容器中,将导致 View 水平滚动,只有当您尝试在空白处滚动它们时,而不是 gridviews 本身。

关于android - 向动态 GridView 添加水平滚动 : Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22745922/

相关文章:

Android:GridView:滚动时如何只加载屏幕上显示的项目?

android - 我的 gridview 不工作,因为找不到 id

android - SwipeRefreshLayout 干扰 ViewPager 内的 GridView

android - 使用带有 GridLayoutManager 的 RecyclerView 的简单 Android 网格示例(如旧的 GridView)

android - 我看不到的循环

c - JNI/C 库 : passing byte ptr

java - 使用 Gson 库解析 Json

ANDROID STUDIO - 如何制作项目的备份副本

Java, 安卓 : ClassCastException after Intent

android - 避免在 fragment 中重新加载