首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android小部件禁用listitem嵌套按钮

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

Stack Overflow用户
提问于 2014-07-27 11:29:10
回答 1查看 1.6K关注 0票数 2

我正在制作一个android应用程序,它有一个带有可点击按钮的listview (嵌套的listview)。该应用程序的主屏幕小部件也具有几乎相同的布局。

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

代码语言:javascript
运行
复制
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设置带有按钮的listitems。守则如下:

代码语言:javascript
运行
复制
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()方法中接收它,如下所示:

代码语言:javascript
运行
复制
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代码

代码语言:javascript
运行
复制
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);
         .....
       }

所以我希望你们明白重点。如何在项目的特定位置设置/更改按钮?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-31 21:22:45

这就是我实现类似东西的方式:

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

getViewAt(int)方法中,我检查isEnabled的值并对其进行操作:

代码语言:javascript
运行
复制
@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)中,我获取单击按钮的位置-并在该位置更新MyCustomObjisEnabled状态-并更新小部件。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24980466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档