Android小部件禁用listitem嵌套按钮onclick

时间:2014-07-27 11:29:10

标签: android android-widget

我正在创建一个具有可点击按钮列表视图的Android应用程序(嵌套列表视图)。该应用程序的Homescreen小部件也具有几乎相同的布局。

点击后需要禁用按钮。在活动中,我可以轻松地禁用按钮:

button.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    // Disable Button
                    v.setEnabled(false);

                    // Mark item as not received
                    Toast.makeText(getApplicationContext(), "Button Disabled", Toast.LENGTH_SHORT).show();
                }
            });

对于小部件部分,我使用RemoteViewFactory来设置带有按钮的列表项。代码如下:

public class ListProvider implements RemoteViewsFactory {
.....

   public RemoteViews getViewAt(int position) {
      final RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.list_row); //The layout of single list row
     remoteView.setTextViewText(R.id.heading, "Some Heading");
     remoteView.setTextViewText(R.id.content, "Some Content");
     Intent fillInIntent = new Intent();
     fillInIntent.putExtra(WidgetProvider.EXTRA_ITEM, position);
     remoteView.setOnClickFillInIntent(R.id.buttonwidget, fillInIntent);
     .....
   }
.....
} 

现在已经设置了按钮的意图,我可以在WidgetProvider的onReceive()方法中接收它:

public class WidgetProvider extends AppWidgetProvider {
   public static final String EXTRA_ITEM = "com.example.android.stackwidget.EXTRA_ITEM";
   public static final String ACTION_START_ACTIVITY = "startActivity";

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; ++i) {
            RemoteViews remoteViews = updateWidgetListView(context,appWidgetIds[i]);
            Intent clickIntent = new Intent(context, WidgetProvider.class);
            clickIntent.setAction(ACTION_START_ACTIVITY);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, clickIntent, 0);
            remoteViews.setPendingIntentTemplate(R.id.listViewWidget, pendingIntent);

        }

        super.onUpdate(context, appWidgetManager, appWidgetIds);
   }

   @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
        RemoteViews remoteViews = updateWidgetListView(context,appWidgetId);
        ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);

        //Printing detail of clicked item from widget
        if(ACTION_START_ACTIVITY.equals(intent.getAction())) {
             //Disable the button
             remoteViews.setBoolean(R.id.buttonwidget, "setEnabled", false);
             Toast.makeText(context, "Widget Button Clicked",   Toast.LENGTH_SHORT).show();
        }
        //Update the widget again
        manager.updateAppWidget(thisWidget, remoteViews);
   }
}

除了只更新第一个listitem的按钮外,此方法也运行良好。我想禁用被点击的相应listitem的按钮。所以我可能需要一个位置元素。所以我想到了获取RemoteViewsFactory的getViewAt方法的意图,因为它保持了listitem的位置。但我无法得到意图。以下是修改后的RemoteViewsFactory代码:

public RemoteViews getViewAt(int position) {
          final RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.list_row); //The layout of single list row
         remoteView.setTextViewText(R.id.heading, "Some Heading");
         remoteView.setTextViewText(R.id.content, "Some Content");

         //Get the intent of button here
         if(WidgetProvider.ACTION_START_ACTIVITY.equals(this.remoteIntent.getAction())) {
            Toast.makeText(context, "Listitem Complete Button Clicked: "+position,   Toast.LENGTH_SHORT).show();
        }

         Intent fillInIntent = new Intent();
         fillInIntent.putExtra(WidgetProvider.EXTRA_ITEM, position);
         remoteView.setOnClickFillInIntent(R.id.buttonwidget, fillInIntent);
         .....
       }

所以我希望你们明白这一点。如何在itemitem的某个特定位置设置/更改按钮?

编辑:我应用了下面给出的解决方案,它让我工作。我现在可以随意地了解意图和相应的数据。我还有一个问题。根据状态,按钮需要更改为其他图像。简单来说,listview需要更新,我通过生成随机数并将其与服务意图一起发送。 正如here所述。

1 个答案:

答案 0 :(得分:4)

这是我实施类似的方式:

在实现RemoteViewsFactory的类中,我有一个自定义对象的ArrayList。我使用这些来膨胀并从RemoteViews方法返回getViewAt(int)。我的自定义对象的一个​​成员是boolean isEnabled。根据用户选择,我设置了这个成员变量,并在我的小部件上调用了刷新/更新。

getViewAt(int)方法中,我检查isEnabled的值并对其采取行动:

@Override
public RemoteViews getViewAt(int position) {
    final RemoteViews remoteView = new RemoteViews(
    mContext.getPackageName(), R.layout.app_widget_listview);

    MyCustomObj info = mEntries.get(position);

    if (info.isEnabled()) {
        remoteView.setBoolean(R.id.buttonwidget, "setEnabled", true);
    } else {
        remoteView.setBoolean(R.id.buttonwidget, "setEnabled", false);
    }

    Bundle extras = new Bundle();
    extras.putInt(Constants.POSITION_IN_LIST, position);
    Intent fillInIntent = new Intent();
    fillInIntent.putExtras(extras);
    remoteView.setOnClickFillInIntent(R.id.buttonwidget, fillInIntent);

    return remoteView;
}

onReceive(Context, Intent)内,我获取点击按钮的位置 - 并更新该位置isEnabled的{​​{1}}状态 - 并更新小部件。