【问题标题】:Android GridView scrolling with ImageButtonsAndroid GridView 使用 ImageButtons 滚动
【发布时间】:2016-06-17 01:52:13
【问题描述】:

我正在构建一个应用程序抽屉片段,并使用 GridView 来显示每个应用程序项。目前,抽屉没有像我希望的那样垂直滚动。我更喜欢使用 ImageButtons 作为唯一可聚焦的项目,以与我的其他屏幕保持一致。本质上,所有导航都是通过在 ImageButtons(而不是与每个项目关联的 GridView 区域)之间移动来完成的。此应用程序是电视的启动器,因此 GridView 应使用远程输入滚动。这些按钮现在可以导航,但只有实际显示的按钮。

我一直在尝试让滚动功能​​正常工作。我假设它与强制焦点到 ImageButtons 有关,但我不确定如何解决这个问题。任何帮助表示赞赏!

fragment_app_drawer.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context="ninjabox.tv.com.ninjabox.AppDrawerFragment"
             android:background="@color/dark_gray">

    <GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/appsGridLayout"
        android:numColumns="5"
        android:verticalSpacing="10dp"
        android:columnWidth="50dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:focusable="false"
        android:descendantFocusability="afterDescendants"
        android:scrollIndicators="right">
    </GridView>

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragmentProgressBar"
        android:layout_gravity="center"
        android:visibility="invisible"/>

</FrameLayout>

app_drawer_item.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="25dp"
                android:clipToPadding="false"
                android:focusable="false">

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/appIcon"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        android:cropToPadding="false"
        android:stateListAnimator="@anim/button_elevation"
        android:background="@drawable/image_button_drawable"
        android:elevation="@dimen/elevation_default"
        android:src="@drawable/spotify_icon"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:padding="8dp"
        android:focusable="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/appLabel"
        android:textSize="@dimen/abc_text_size_medium_material"
        android:textColor="#ffffff"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:text="App Name"
        android:layout_below="@+id/appIcon"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

AppDrawerFragment.java ArrayAdapter

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ArrayAdapter<AppDetail> adapter = new ArrayAdapter<AppDetail>(
            getActivity(),
            R.layout.app_drawer_item,
            MainActivity.getApps()) {
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = getLayoutInflater(null).inflate(R.layout.app_drawer_item, null);
            }

            ImageButton appIconButton = (ImageButton)convertView.findViewById(R.id.appIcon);
            appIconButton.setImageDrawable(mApps.get(position).getIcon());
            if (position == 0) {
                appIconButton.requestFocus();
            }
            appIconButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getContext(), mApps.get(position).getName(), Toast.LENGTH_LONG).show();
                }
            });

            TextView appLabel = (TextView)convertView.findViewById(R.id.appLabel);
            appLabel.setText(mApps.get(position).getLabel());

            return convertView;
        }
    };

    mGridView.setAdapter(adapter);
}

【问题讨论】:

  • 我最终只是将我的 ImageButtons 交换为 ImageViews 并为 GridView 构建一个选择器

标签: android gridview scroll scrollview imagebutton


【解决方案1】:

你只需要用scrollView包围你想要滚动的内容。例如,如果您想在第一个布局中滚动 GridView,请添加:

<ScrollView 
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<GridView


    <!-- everything you already have -->

</GridView>
</ScrollView>

我还没有测试它,但我会工作。

【讨论】:

  • 我试过了。看起来我可以导航到下面未在屏幕上绘制的按钮,但行不会在 GridView 中向上移动以反映焦点按钮
  • 编辑:似乎不是 ScrollView 的结果。从我的简短研究来看,GridViews 不应该放在 ScrollViews 中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多