Pro Android学习笔记(一三九):List Widgets(1):概述

来源:互联网 发布:单片机产生正弦波程序 编辑:程序博客网 时间:2024/04/29 10:23

文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处http://blog.csdn.net/flowingflying/以及作者@恺风Wei。

通过Remote view来对widget的views进行设置,对于List和grid,如何操控里面的子view?本学习笔记开始,将探讨这个问题。

Remote Views

我们知道不能直接获取RemoteViews的对象,Remote Views对象构造函数含有layout文件参数。在Android4.4,支持remote views的有FrameLayout、LinearLayout、RelativeLayout、AnalogClock、Button、Chronometer、 ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、 GridView、StackView、AdapterViewFlipper,其中包括集合ListView和GridView。要知道某个View是否看作为RemoteView,可以在Eclipse中查看解释,如下图所示:

小例子

小例子如图所示。Widget是一个list view,上下各有一个text view,利用FrameLayout,如果ListView有内容,显示ListView,否则显示Text View,写明无内容。当点击list中的某个item,触发弹出Toast。

Android通过Service来处理List Widget中List item,这个Service是RemoteViewsService类,如何构建remote 子view,是通过RemoteViewsFactory类。

RemoteViewsFactory作用类似于在ListView中的自定义Adapter,对ListView中的子View进行创建和处理。RemoteViewsFactory是Adapter,对Remote view的每个item的数据进行设置,可视为RemoteViewsAdapter。

RemoteViewsService是Service,由AppWidgetProvider开启,用来绘制list中的子view。Android对进行很好的封装,继承类中只需要返回RemoteViewsFactory对象。

为widget准备含ListView的remote layout

下面是widget的remote layout文件。在FrameLayout中含有连个view,一个是ListView,一个TextView,根据item的数目,有则显示ListView,无则显示TextView。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout …… >
    <TextView ……  android:layout_height="30dp"  android:layout_weight="0"/>

   
    <FrameLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="center">
        <ListView android:id="@+id/listwidget_list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"  />
        <TextView android:id="@+id/listwidget_empty_tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:text="Empty Records View"
            android:textSize="20sp"/>        
     </FrameLayout>
   
    <TextView …… android:layout_height="30dp"  android:layout_weight="0"/>        
</LinearLayout>

 

 小例子代码在:Pro Android学习:list widget小例子

相关链接:我的Android开发相关文章

0 0
原创粉丝点击