如何在小部件中使用列表视图? [关闭]

2024-04-28

我正在尝试创建一个从 rss feed 加载到小部件中的列表视图?

有谁知道如何在小部件中使用列表视图的教程?

或者也许有人可以提供1。


基本上,您需要使用两个组件:

  1. 远程视图工厂:这是特殊的适配器。
  2. 远程查看服务: 这是返回第一个。

让我们创建 RemoteViewsFactory 类:

public class MyWidgetRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {

    private Context mContext;
    private Cursor mCursor;

    public MyWidgetRemoteViewsFactory(Context applicationContext, Intent intent) {
        mContext = applicationContext;
    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDataSetChanged() {

        if (mCursor != null) {
            mCursor.close();
        }

        final long identityToken = Binder.clearCallingIdentity();
        Uri uri = Contract.PATH_TODOS_URI;
        mCursor = mContext.getContentResolver().query(uri,
                null,
                null,
                null,
                Contract._ID + " DESC");

        Binder.restoreCallingIdentity(identityToken);

    }

    @Override
    public void onDestroy() {
        if (mCursor != null) {
            mCursor.close();
        }
    }

    @Override
    public int getCount() {
        return mCursor == null ? 0 : mCursor.getCount();
    }

    @Override
    public RemoteViews getViewAt(int position) {
        if (position == AdapterView.INVALID_POSITION ||
                mCursor == null || !mCursor.moveToPosition(position)) {
            return null;
        }

        RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.collection_widget_list_item);
        rv.setTextViewText(R.id.widgetItemTaskNameLabel, mCursor.getString(1));

        return rv;
    }

    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public long getItemId(int position) {
        return mCursor.moveToPosition(position) ? mCursor.getLong(0) : position;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

}

现在是 RemoteViewsService 的时候了(最简单的):

public class MyWidgetRemoteViewsService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new MyWidgetRemoteViewsFactory(this.getApplicationContext(), intent);
    }
}

此外,您还必须在 androidManifest.xml 中注册此服务:

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

现在,在扩展 AppWidgetProvider 的 Widget 类中,您可以管理布局:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {
        RemoteViews views = new RemoteViews(
                context.getPackageName(),
                R.layout.collection_widget
        );
        Intent intent = new Intent(context, MyWidgetRemoteViewsService.class);
        views.setRemoteAdapter(R.id.widgetListView, intent);
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

现在创建一个新的资源文件res/xml并将其命名为collection_widget.xml:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="40dp"
    android:minHeight="40dp"
    android:updatePeriodMillis="864000"
    android:previewImage="@drawable/simple_widget_preview"
    android:initialLayout="@layout/collection_widget"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen">
</appwidget-provider>

我们需要再创建两个文件资源/布局定义列表的布局和每个列表项的布局。

集合_widget.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorWhite"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical">
    <FrameLayout android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView android:layout_width="match_parent"
            android:id="@+id/widgetTitleLabel"
            android:text="@string/title_collection_widget"
            android:textColor="@color/colorWhite"
            android:background="@color/colorPrimary"
            android:textSize="18dp"
            android:gravity="center"
            android:textAllCaps="true"
            android:layout_height="@dimen/widget_title_min_height"></TextView>
    </FrameLayout>
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ListView android:id="@+id/widgetListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorWhite"
            android:dividerHeight="1dp"
            android:divider="#eeeeee"
            tools:listitem="@layout/collection_widget_list_item"></ListView>
    </LinearLayout>
</LinearLayout>

collection_widget_list_item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:paddingLeft="@dimen/widget_listview_padding_x"
    android:paddingRight="@dimen/widget_listview_padding_x"
    android:paddingStart="@dimen/widget_listview_padding_x"
    android:paddingEnd="@dimen/widget_listview_padding_x"
    android:minHeight="@dimen/widget_listview_item_height"
    android:weightSum="2"
    android:id="@+id/widgetItemContainer"
    android:layout_height="wrap_content">

    <TextView android:id="@+id/widgetItemTaskNameLabel"
        android:layout_width="wrap_content"
        android:gravity="start"
        android:layout_weight="1"
        android:textColor="@color/text"
        android:layout_gravity="center_vertical"
        android:layout_height="wrap_content"></TextView>

</LinearLayout>

我使用这篇文章作为例子,对我来说就像一个魅力!

https://www.sitepoint.com/killer-way-to-show-a-list-of-items-in-android-collection-widget/ https://www.sitepoint.com/killer-way-to-show-a-list-of-items-in-android-collection-widget/

如今,这种情况并不罕见。希望能帮助某人。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在小部件中使用列表视图? [关闭] 的相关文章

  • Android 错误 - close() 从未在数据库上显式调用

    我应该在代码的哪里调用 close LogCat 返回此错误 close 从未在数据库上显式调用 android database sqlite DatabaseObjectNotClosedException 应用程序未关闭此处打开的游标
  • Fused Location Provider 是不错的选择吗?

    我正在开发一个应用程序 我想在其中使用融合位置提供程序 但我有一些疑问 还有几个问题 当 GPS 关闭并且我将优先级设置为 HIGH 时 是否意味着 GPS 会自动打开 我可以根据需要将 UpdateLocation 设置为具有高优先级的
  • Moshi 无法解析 nullable

    你好 希望你能帮助我 使用 kotlin Retrofit2 moshi 我从 https api spacexdata com v3 launches 获取数据并解析它 一切都很顺利 我得到的属性如下 flight number miss
  • Mediaplayer 播放几次后停止播放

    我有一个按钮 按下它会播放一个随机声音剪辑 然后播放另一个声音剪辑 然后通过一个媒体播放器播放另一个声音剪辑 但是多次按下该按钮 15 20 次 后 所有音频都会停止 我在播放最后一个音频剪辑后释放媒体播放器 所以我不认为这是原因 有什么指
  • 如何在android中使用retrofit访问404错误?

    我正在使用改造 2 访问 REST API 以使用原始正文插入 JSON 数据 我从服务器获得成功响应 但在响应时收到 404 错误 我想访问404错误请帮我解决这个问题 ApiUtil getServiceClass sendFinalC
  • 每当调用 startactivityforresult 时 Android 就会终止我的应用程序

    好吧 在我的应用程序中 我使用 Android 的默认相机和图库 startActivityforResult 为 Intent i new Intent android intent action PICK MediaStore Imag
  • 带有 ListTiles 和按钮行的 Flutter 下拉菜单

    我正在尝试构建一个自定义下拉菜单 如下所示 我已经成功地实现了ListTiles and Row of Buttons没有下拉菜单 但我不确定如何将所有内容嵌套在下拉菜单类中 这是我到目前为止所得到的 class HomePage exte
  • MediaStyle 通知未响应 RemoteControl 事件。

    我们正在将正在进行的播放通知迁移到 Lollipop 中引入的 MediaStyle 通知 RemoteControlClient 似乎已被弃用 并且 MediaStyle 通知不处理媒体按钮事件 例如通过耳机远程暂停 播放 有人得到这个工
  • 使用 Spotify SDK 安装 Android 应用程序时出错,[INSTALL_FAILED_NO_MATCHING_ABIS]

    我正在尝试遵循Spotify Beta SDK 使用教程 https developer spotify com technologies spotify android sdk tutorial 每当我尝试将应用程序安装到 Nexus 6
  • 如何在应用程序中创建会话对象

    在我的应用程序中 我想创建一个用于登录和注销的会话 我不知道如何使用会话 任何人都可以通过提供一些示例来帮助我 我认为会话对象应该是在应用程序开始运行时声明和初始化的静态对象 我遇到了这个问题 并决定将我的会话对象放入 utils 类中 该
  • Android 生命周期哪个事件在生命周期中只触发一次?

    我读过一些博客并访问了一些网站 我想知道哪个事件在生命周期中只触发了一次 阅读博客后我意识到onCreate 方法在生命周期内仅触发一次 我不知道我是对还是错 现在我的问题是 我想触发任何仅在我更改横向或纵向方向时触发一次的事件 而不是在启
  • Firebase API 初始化失败,java.lang.reflect.InitationTargetException

    我在我的应用程序中使用 firebase 身份验证 数据库和存储服务 之前运行良好 我已经添加了 firebase 云消息传递设置 如文档中所述 但应用程序在运行时崩溃了 我调查了这个问题大约 4 个小时并尝试了不同的解决方案 就像保持所有
  • 长按 HOME 按钮菜单隐藏 Android 应用程序

    我想从 且仅从 完成后长时间按住 HOME 按钮时出现的菜单中隐藏我的 Android 应用程序 有没有办法做到这一点 以编程方式调用 finish 并不能解决问题 有很多关于从启动器和任务管理器隐藏应用程序的线程 但这不是我想要的 我只是
  • 如何从MediaCodec获取解码格式?

    我正在与MediaCodec 我用它来解码 mp4 video MediaCodec 将视频解码为YUV格式 但我需要得到RGBA 一切都很好 但我发现有几种可能的格式 例如YUV420 YUV422等等 因此 据我所知 要进行转换 我需要
  • 短信管理器在少于 160 个字符时发送多部分消息

    我编写了一个使用短信管理器的应用程序 我用的方法sendTextMessage 但这行不通 现在我正在使用sendMutlipartTextMessage 这是工作 但当它大约 60 个字符时 它会发送多部分消息 这个是正常的 我读过的所有
  • 找不到元素“android.support.constraint.ConstraintLayout”的声明

    我创建了一个名为的 xml 文件activity main sw50dp 但是当我尝试验证它时 它给了我错误 错误 4 42 cvc elt 1 a 找不到元素 android support constraint ConstraintLa
  • Android 上的 Facebook 深度链接

    我正在尝试在我的应用程序上实现 Facebook 的深度链接功能 并遇到了以下情况 我有一个名为 MainActivity 的活动 其声明如下
  • 如何调试仅在发布模式下崩溃的 Android 应用程序

    在调试模式下一切正常 但在发布模式下崩溃 调试模式下有哪些所需权限在发布模式下未打开 EDIT 当我将 链接 设置为 无 时 我会通过第一个屏幕进入 登录 屏幕 但是 当我添加发布权限时Internet 第一次尝试读取远程实体框架核心表时它
  • 加载 highchart 时 Android 错误膨胀类

    我正在尝试加载highcharts via Dialog 下面是我的代码 Gradle implementation com highsoft highcharts highcharts 9 0 1 XML
  • 在edittext android中插入imageview

    我想将 imageview 放在 edittext 中 可能吗 我检查了 evernote 应用程序 它能够将照片放在编辑文本部分 我想让我的应用程序完全相同 我如何才能将从图库中选择的图像视图放入编辑文本中 我首先尝试将 imagevie

随机推荐