其他分享
首页 > 其他分享> > Android小部件中的GridView

Android小部件中的GridView

作者:互联网

我一直在尝试使用GridView为我的应用程序实现widget.我是Android开发的新手.我在互联网上搜索了很多,但找不到任何有用的东西.我有点卡在这里,因为这不会在我的GridView上显示任何内容.任何帮助,将不胜感激.

提前致谢 !

这是我的代码

public class MainActivity extends AppWidgetProvider{

public static final String TOAST_ACTION = "com.example.widgetdevelopment.TOAST_ACTION";
public static final String EXTRA_ITEM = "com.example.widgetdevelopment.EXTRA_ITEM";



@Override
public void onReceive(Context context, Intent intent) {

    AppWidgetManager mgr = AppWidgetManager.getInstance(context);
    if (intent.getAction().equals(TOAST_ACTION)) {
        int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0);
        Toast.makeText(context, "Touched view " + viewIndex,      Toast.LENGTH_SHORT).show();
    }
    super.onReceive(context, intent);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds)  {
    for (int i = 0; i < appWidgetIds.length; ++i) {
        Intent intent = new Intent(context, GridWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);

        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.activity_main);
        rv.setRemoteAdapter(appWidgetIds[i], R.id.gridView1, intent);

        rv.setEmptyView(R.id.gridView1, R.id.empty_view);


        Intent toastIntent = new Intent(context, MainActivity.class);

        toastIntent.setAction(MainActivity.TOAST_ACTION);
        toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent toastPendingIntent;
     toastPendingIntent=PendingIntent.getBroadcast(context,0,toastIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        rv.setPendingIntentTemplate(R.id.gridView1, toastPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds[i], rv);   
    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);

}   

}

这是我的RemoteViewServices类

public class GridWidgetService extends RemoteViewsService{

@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    // TODO Auto-generated method stub
    return new GridRemoteViewsFactory(this.getApplicationContext(), intent);
}

}



public class GridRemoteViewsFactory implements RemoteViewsFactory {

private static final int mCount = 10;
private List<WidgetItem> mWidgetItems = new ArrayList<WidgetItem>();
private Context mContext;
private int mAppWidgetId;

public GridRemoteViewsFactory(Context context, Intent intent) {
    mContext = context;
    mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
}

@Override
public void onCreate() {
    for (int i = 0; i < mCount; i++) {
        mWidgetItems.add(new WidgetItem(i + "!"));
    }
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

@Override
public void onDataSetChanged() {
    // TODO Auto-generated method stub

}

@Override
public void onDestroy() {
    //  mWidgetItems.clear();

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mCount;
}

@Override
public RemoteViews getViewAt(int position) {


    RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
    rv.setTextViewText(R.id.widget_item, mWidgetItems.get(position).getSomething());
    Bundle extras = new Bundle();
    extras.putInt(MainActivity.EXTRA_ITEM, position);
    Intent fillInIntent = new Intent();
    fillInIntent.putExtras(extras);
    rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);

    return rv;
}

@Override
public RemoteViews getLoadingView() {
    // TODO Auto-generated method stub
    return null;
}



@Override
public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return 2;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return true;
}

}

这是我的Object类.

public class WidgetItem {
String something;


public WidgetItem(String something) {
super();
this.something = something;
}

public String getSomething() {
return something;
}

public void setSomething(String something) {
this.something = something;
}

}

Widget_item XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.widgetdevelopment.MainActivity" >

 <TextView
    android:id="@+id/widget_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

 </RelativeLayout>

Main_Activity Layout(此布局是包含GridView的Widget的布局)

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="top"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".MainActivity" >

 <TextView
  android:id="@+id/textView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_alignParentTop="true"
  android:text="@string/website"
  android:textAppearance="?android:attr/textAppearanceMedium" />

 <Button
   android:id="@+id/button1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@+id/textView1"
   android:layout_below="@+id/textView1"
   android:text="@string/app_name" />

 <GridView
   android:id="@+id/gridView1"
   android:layout_width="150dp"
   android:layout_height="100dp"
   android:layout_alignLeft="@+id/button1"
   android:layout_alignParentRight="true"
   android:layout_below="@+id/button1"
   android:numColumns="3" >

 </GridView>
 <TextView
    android:id="@+id/empty_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"

    android:textColor="#ffffff"
    android:textStyle="bold"
    android:text="empty_view_text"
    android:textSize="20sp" />

  </RelativeLayout>

解决方法:

您是否在AndroidManifest.xml中注册了服务?像这样 :

<service android:name="GridWidgetService"
   android:permission="android.permission.BIND_REMOTEVIEWS" />

标签:android-gridview,android,android-widget
来源: https://codeday.me/bug/20190830/1769814.html