本文介绍了Android RemoteViews ListView滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 ListView 滚动到 AppWidget 中的特定位置.

Im trying to scroll a ListView to a particular position in an AppWidget.

但是它什么也没做,我也尝试了 setPosition 方法,但是没有用.

However it does not do anything, i also tried the setPosition method but not working.

也没有错误或堆栈跟踪.

Also no errors or stack trace.

代码:

                    if (list.size() == 0) {
                        loadLayout(R.layout.rooster_widget_header);
                        views.addView(R.id.header_container,
                                views);
                    } else {
                        views.setRemoteAdapter(R.id.lvWidget, svcIntent);
                        views.setScrollPosition(R.id.lvWidget, 3);
                    }

推荐答案

问题: ListView 在显示之前没有任何子级.因此,在设置adpater之后立即调用 setScrollPosition 无效.以下是 AbsListView 进行此检查:

Issue: ListView does not have any children until it is displayed. Hence calling setScrollPosition right after setting adpater has no effect. Following is the code in AbsListView which does this check:

final int childCount = getChildCount();
if (childCount == 0) {
    // Can't scroll without children.
    return;
}

解决方案:理想情况下,我应该使用 ViewTreeObserver.OnGlobalLayoutListener 设置 ListView 滚动位置,但是在远程情况下是不可能的意见.设置滚动位置,并在运行时延迟延迟调用 partiallyUpdateAppWidget .我已经修改了Android天气小部件代码,并在 git hub 中共享.

Solution: Ideally I would have used ViewTreeObserver.OnGlobalLayoutListener for setting the ListView scroll position, but it is not possible in case of remote views. Set the scroll position and invoke partiallyUpdateAppWidget in a runnable with some delay. I've modified the Android weather widget code and shared in git hub.

public class MyWidgetProvider extends AppWidgetProvider {

    private static HandlerThread sWorkerThread;
    private static Handler sWorkerQueue;

    public MyWidgetProvider() {
        // Start the worker thread
        sWorkerThread = new HandlerThread("MyWidgetProvider-worker");
        sWorkerThread.start();
        sWorkerQueue = new Handler(sWorkerThread.getLooper());
    }

    public void onUpdate(Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int i = 0; i < appWidgetIds.length; ++i) {
            ...
            final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            views.setRemoteAdapter(R.id.lvWidget, svcIntent);

            sWorkerQueue.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    views.setScrollPosition(R.id.list, 3);
                    appWidgetManager.partiallyUpdateAppWidget(appWidgetIds[i], views);
                }

            }, 1000);

            appWidgetManager.updateAppWidget(appWidgetIds[i], views);
            ...
        }
    }
}

这是屏幕记录.滚动到第5个位置.

Here is the screen record. It scrolls to 5th position.

这篇关于Android RemoteViews ListView滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:29