如何使用RemoteView设置行项目的可见性?

时间:2015-03-29 18:36:03

标签: android-widget

我想设置单击该行时列表视图行中图标的可见性。

在onUpdate中,我在listview上设置了pendingIntentTemplate,以便在单击时调用onReceive。

在我的RemoteViewsService类中,我设置了行的fillIntent来区分行,将位置放在intent bundle中:

@Override public RemoteViews getViewAt(int position) {
Item currentItem = items.get(position);
String label = currentItem.getLabel();

Intent fillIntent = new Intent();
fillIntent.putExtra(WidgetController.WIDGET_LABEL, label);
fillIntent.putExtra(WidgetController.WIDGET_ROW_NUM, position);
views.setOnClickFillInIntent(R.id.label, fillIntent);

return views;

我如何在AppWidgetProvider类中使用该信息?

public final class WidgetController extends AppWidgetProvider {

@Override public void onReceive(@NonNull final Context context, @NonNull Intent intent) {
// once it detects a listview element click, I do the following
final RemoteViews views = new RemoteViews(context.getPackageName(),  
                                          R.layout.listview_item);

views.setViewVisibility(R.id.icon, View.GONE);

listview_item.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center">

<ImageView
        android:id="@+id/icon"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="10dp"
        android:src="@drawable/dog"
        android:scaleType="fitXY" />

<ProgressBar
        android:id="@+id/icon_loading"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:visibility="gone"/>

<TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:layout_marginLeft="10dp"
        android:text="Pets"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您可以尝试SharedPreferences获取是否按下该行的信息。

public static boolean isPressed(Context context) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            boolean lock = prefs.getBoolean("lock", true);
            return lock;
        }

public static void setPressed(Context context, boolean pressed) {

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("lock", pressed);
            editor.commit();
        }

然后在AppWidgetProvider课程中,您可以查看isPressed。

if(isPressed)
  views.setViewVisibility(R.id.icon, View.GONE);
else
  views.setViewVisibility(R.id.icon, View.VISIBLE);