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

时间:2016-05-08 08:37:09

标签: android android-widget

我一直在尝试创建一个支持水平滚动的Android主屏幕小部件。

我在{widget}的GridView内放了一个LinearLayout。我的GridView有一些列。我希望在某个时刻屏幕上只能显示5列,并且可以在滚动或点击按钮时访问其余部分(它应该实现滚动动画)。我在我的设备上使用HorizontalScrollView并说“问题加载小工具”

主屏幕小部件似乎不支持HorizontalScrollView。还有另一种方法可以在android主屏幕小部件中实现GridView的水平滚动吗?

3 个答案:

答案 0 :(得分:0)

您无法使app小部件水平滚动,因为此操作仅限于切换主屏幕。

此外,您无法使用Horizo​​ntalScrollView

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.

Source

答案 1 :(得分: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 gridlayoutmanager和recycler视图,如下所示:

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

答案 2 :(得分:-1)

您可以使用RecyclerView

执行此操作

为此,您需要将其添加到项目build.gradle文件中:

compile 'org.solovyev.android.views:linear-layout-manager:0.5@aar'

按照ListView的方式执行所有操作,并在设置列表适配器之前添加此行:

RecyclerView recyclerview = (RecyclerView) findViewById(R.id.list_main);
recyclerview.setLayoutManager(new org.solovyev.android.views.llm.LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));

MyAdapter mAdapter = new MyAdapter(context, my_list);
recyclerview.setAdapter(mAdapter);