Android主屏幕小部件中的水平滚动

3
我一直在尝试创建一个支持水平滚动的Android主屏幕小部件。
我将一个GridView放在我的小部件的LinearLayout中。我的GridView有一些列数。我希望在某一时刻只有5列可见,并且其余的可以通过滚动或点击按钮来访问(它应该实现滚动动画)。我使用了HorizontalScrollView,但是在我的设备上显示“加载小部件出错”。
似乎HorizontalScrollView在主屏幕小部件中不被支持。是否有另一种方法来实现Android主屏幕小部件中GridView的水平滚动?

我在小部件的LinearLayout中放置了一个GridView。我的GridView有一些列。我希望在某个时刻屏幕上只能看到5列,其余列可以通过滚动来访问。我使用了HorizontalScrollView,但在我的设备上它显示"Problem Loading Widget"。 - user3376390
请在问题@user3376390中添加您的代码。 - Lips_coder
主屏幕小部件不支持水平滚动。如果它们支持,你将如何在多个主页之间进行滑动? - Hector
2个回答

0
你无法使应用程序小部件水平滚动,因为此操作受限于切换主屏幕。
此外,您也不能使用HorizontalScrollView。
A RemoteViews object (and, consequently, an App Widget) can support the following layout classes:

FrameLayout
LinearLayout
RelativeLayout
GridLayout
And the following widget classes:

AnalogClock
Button
Chronometer
ImageButton
ImageView
ProgressBar
TextView
ViewFlipper
ListView
GridView
StackView
AdapterViewFlipper
Descendants of these classes are not supported.

源代码


0
如果您想要创建一个包含5列的简单Gridview布局,可以按照以下步骤进行:
<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="5"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"/>
</RelativeLayout>

添加您的活动和适配器代码。如果您想要为视图添加动画效果,可以使用Android GridLayout Manager与Material Design中的Recycler View,如下所示:

public class MainActivity extends ActionBarActivity {

    private GridLayoutManager lLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle(null);

        Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(topToolBar);
        topToolBar.setLogo(R.drawable.logo);
        topToolBar.setLogoDescription(getResources().getString(R.string.logo_desc));

        List<ItemObject> rowListItem = getAllItemList();
        lLayout = new GridLayoutManager(MainActivity.this, 4);

        RecyclerView rView = (RecyclerView)findViewById(R.id.recycler_view);
        rView.setHasFixedSize(true);
        rView.setLayoutManager(lLayout);

        RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(MainActivity.this, rowListItem);
        rView.setAdapter(rcAdapter);
    }
private List<ItemObject> getAllItemList(){

        List<ItemObject> allItems = new ArrayList<ItemObject>();
        allItems.add(new ItemObject("United States", R.drawable.one));
        allItems.add(new ItemObject("Canada", R.drawable.two));
        allItems.add(new ItemObject("United Kingdom", R.drawable.three));
        allItems.add(new ItemObject("Germany", R.drawable.four));
        allItems.add(new ItemObject("Sweden", R.drawable.five));
        allItems.add(new ItemObject("United Kingdom", R.drawable.six));
        allItems.add(new ItemObject("Germany", R.drawable.seven));
        allItems.add(new ItemObject("Sweden", R.drawable.eight));
        allItems.add(new ItemObject("United States", R.drawable.one));
        allItems.add(new ItemObject("Canada", R.drawable.two));
        allItems.add(new ItemObject("United Kingdom", R.drawable.three));
        allItems.add(new ItemObject("Germany", R.drawable.four));
        allItems.add(new ItemObject("Sweden", R.drawable.five));
        allItems.add(new ItemObject("United Kingdom", R.drawable.six));
        allItems.add(new ItemObject("Germany", R.drawable.seven));
        allItems.add(new ItemObject("Sweden", R.drawable.eight));

        return allItems;
    }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接