我正在制作一个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的某个特定位置设置/更改按钮?
EDIT: 我应用了下面给出的解决方案,它让我工作 . 我现在可以随意地了解意图和相应的数据 . 我还有一个问题 . 根据状态,按钮需要更改为其他图像 . 简单来说,listview需要更新,我通过生成随机数并将其与服务意图一起发送 . 正如here所解释的那样 .