Android列表小部件和项目单击无法正常工作

时间:2018-01-24 18:06:29

标签: android listview kotlin android-widget android-pendingintent

我知道这个问题已被多次询问过,但我几乎尝试了所有解决方案,对我来说没有任何作用。所以我想我也不明白。

关键是,我有一个主屏幕小部件,一个列表,并且像往常一样,我希望每个项目在点击时打开活动。

我看到很多关于吐司和广播的答案,但我希望它能开启一项活动。

问题是,点击不起作用。

以下是我所做的:

widget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minHeight="50dp"
    android:minWidth="300dp"
    android:previewImage="@mipmap/ic_launcher"
    android:resizeMode="horizontal|vertical" />

Provider

class StickyWidgetProvider : AppWidgetProvider() {

    override fun onReceive(context: Context, intent: Intent) {
        super.onReceive(context, intent)
        if (ACTION_APPWIDGET_UPDATE == intent.action) {
            updateWidget(context)
        }
    }

    override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
        for (i in 0 until appWidgetIds.size) {
            val appWidgetId = appWidgetIds[i]

            val rv = RemoteViews(context.packageName, R.layout.widget_container)
            rv.setRemoteAdapter(R.id.widget_list, Intent(context, MyWidgetService::class.java))

            val activityIntent = Intent(context, MainActivity::class.java)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
            val pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT)
            rv.setPendingIntentTemplate(R.id.widget_list, pendingIntent)

            appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_list)
            appWidgetManager.updateAppWidget(appWidgetId, rv)
        }
    }

    private fun updateWidget(context: Context) {
        val appWidgetManager = AppWidgetManager.getInstance(context)
        appWidgetManager.notifyAppWidgetViewDataChanged(
                appWidgetManager.getAppWidgetIds(
                        ComponentName(context, StickyWidgetProvider::class.java)
                ),
                R.id.widget_list)
    }
}

RemoteViewsFactory

class StickyWidgetRemoteViewsFactory(private val context: Context) : RemoteViewsService.RemoteViewsFactory {

    private var itemList: List<MyItems>? = null

    private fun updateWidgetListView() {
        itemList = filled by somrthing
    }

    override fun onCreate() {
        updateWidgetListView()
    }

    override fun onDataSetChanged() {
        updateWidgetListView()
    }

    override fun onDestroy() {
        itemList = null
    }

    override fun getCount(): Int {
        return if (itemList != null)
            itemList!!.size
        else
            0
    }

    override fun getViewAt(position: Int): RemoteViews {
        val remoteView = RemoteViews(context.packageName, R.layout.widget_row)
        val item = itemList!![position]

        // fill the view //

        val bundle = Bundle()
        bundle.putParcelable(MainActivity.EXTRA_ITEM, item)
        val fillInIntent = Intent().putExtras(bundle)
        remoteView.setOnClickFillInIntent(R.id.widget_item, fillInIntent)

        return remoteView
    }

    ...
}

我无法弄清楚我在这里做错了什么。在指南https://developer.android.com/guide之后,我在setPendingIntentTemplate中有Provider,由工厂中的setOnClickFillInIntent填充,但项目点击不执行任何操作。

有关信息,小部件为LinearLayout,其TextViews android:textIsSelectable="false"android:clickable="false"android:focusable="false"

我不知道我在这里缺少什么,所以任何帮助都很好!

由于

1 个答案:

答案 0 :(得分:0)

将android:clickable =“false”更改为var result = his.observedByProperties.find(t=>t.name ==='admin'); if(result){ console.log('true'); }