我在我的应用程序小部件中使用GridView。我想在没有数据时将默认文本设置为app-widget。我尝试使用getLoadingView()方法进行操作,但无法正常工作。
WidgetViewsFactory.java的getLoadingViewMethod():
@Override
public RemoteViews getLoadingView() {
Log.e("textview is displayed","true");
RemoteViews row = new RemoteViews(mContext.getPackageName(), R.layout.widget_default_display);
row.setTextViewText(R.id.txt_default_widget,"DEFAULT TEXT");
return row;
}
widget_default_display.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="180dp">
<TextView
android:id="@+id/txt_default_widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/no_notifications"
android:textColor="@color/txt_sub_title_gray"
android:textSize="@dimen/txt_title_size"/>
</LinearLayout>
请给我一些解决方案。
最佳答案
应用小部件中的AdapterView
与应用本地布局中的View
非常相似。要在AdapterView
的集合为空时显示某个View
,您需要将该View
设置为AdapterView
上的空RemoteViews
。
对于Widget,setEmptyView()
类为此具有AppWidgetProvider
方法。从包含RemoteViews
的布局中创建主GridView
对象之后,可以在View
中使用此方法。例如:
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
...
rv.setEmptyView(R.id.grid_view, R.id.txt_default_widget);
请注意,在这种情况下,空的
txt_default_widget
-TextView
GridView
必须与处于相同的主布局中,而不是项目布局中。