当前位置:首页 > 嵌入式 > 嵌入式软件
[导读] 最近项目开发,碰到一个ListView的需求。向上滑动,隐藏Header。向下滑动,迅速显示Header。在GitHub中,找到了QuickReturnHeader项目,虽然最终因为我们的特殊需求,没有

 最近项目开发,碰到一个ListView的需求。

向上滑动,隐藏Header。向下滑动,迅速显示Header。

在GitHub中,找到了QuickReturnHeader项目,虽然最终因为我们的特殊需求,没有采用这个开源项目,但觉得这个项目不错,就对其进行了分析。

项目的使用方式很简单,下载master工程下来,将library目录倒入到eclipse中(我用的eclipse),其他项目使用的使用,作为library引用即可。

使用方式如下:

这是本项目中的给出的例子:

在要使用的的Activity中onCreate方法中

?

1
2
3
QuickReturnHeaderHelper helper = new QuickReturnHeaderHelper(this, R.layout.activity_listview, R.layout.header);
View view = helper.createView();
setContentView(view);

R.layout.activity_listview 就是我们的ListView布局,代码如下,

项目中使用了系统默认的ListView的id:@android:id/list。并且在QuickReturnHeader源码中,固定的使用了这个id。这应该是项目需要改进的地方。

?

1
2
<listview android:id="@android:id/list" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">
</listview>

R.layout.header 就是我们的header布局文件,下面是例子中的布局文件

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<framelayout android:background="@color/abs__background_holo_light"
android:layout_height="@dimen/abs__action_bar_default_height" android:layout_width="match_parent"
 
    <linearlayout android:id="@+id/frame" android:layout_gravity="center"
android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="horizontal">
 
        <imagebutton android:layout_height="wrap_content"
android:layout_weight="1" android:layout_width="0dp"
android:src="@drawable/ic_navigation_accept" style="@style/actionBarButton">
 
        <imagebutton android:layout_height="wrap_content"
android:layout_weight="1" android:layout_width="0dp" android:src="@drawable/ic_navigation_cancel" style="@style/actionBarButton">
 
        <imagebutton android:layout_height="wrap_content"
android:layout_weight="1" android:layout_width="0dp"
android:src="@drawable/ic_navigation_refresh" style="@style/actionBarButton">
 
        <imagebutton android:layout_height="wrap_content"
android:layout_weight="1" android:layout_width="0dp"
android:src="@drawable/ic_action_help" style="@style/actionBarButton">
    </imagebutton></imagebutton></imagebutton></imagebutton></linearlayout>
 
    <view android:background="#3333"
android:layout_gravity="bottom" android:layout_height="1dp"
android:layout_width="match_parent">
 
</view></framelayout>

以上就是,这个开源项目的使用,很简单。下面就实现的的原理,进行分析。

简单总结就是一句话,QuickRetrunHeaderHelper,将我们的ListView布局和Header布局,放在了一个FrameLayout中。同时为ListView提供一个Header布局同样高度的空白的ListViewHeader。

用一张图来说明

下面是开源库中的关键代码:

构造函数:

?

1
2
3
4
5
public QuickReturnHeaderHelper(Context context, int contentResId, int headerResId) {
        this.context = context;
        this.contentResId = contentResId;
        this.headerResId = headerResId;
    }

createView方法:

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public View createView() {
        inflater = LayoutInflater.from(context);
        content = inflater.inflate(contentResId, null);
 
        realHeader = inflater.inflate(headerResId, null);
        realHeaderLayoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        realHeaderLayoutParams.gravity = Gravity.TOP;
 
        // Use measured height here as an estimate of the header height, later on after the layout is complete
        // we‘ll use the actual height
        int widthMeasureSpec = MeasureSpec.makeMeasureSpec(LayoutParams.MATCH_PARENT, MeasureSpec.EXACTLY);
        int heightMeasureSpec = MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.EXACTLY);
        realHeader.measure(widthMeasureSpec, heightMeasureSpec);
        headerHeight = realHeader.getMeasuredHeight();
 
        listView = (ListView) content.findViewById(android.R.id.list);
        if (listView != null) {
            createListView();
        } else {
            createScrollView();
        }
        return root;
    }

createListView方法(此开源库同样适用于ScrollView,通过createScrollView创建,代码类似):

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
private void createListView() {
        root = (FrameLayout) inflater.inflate(R.layout.qrh__listview_container, null);
        root.addView(content);
 
        listView.getViewTreeObserver().addOnGlobalLayoutListener(this);
        ListViewScrollObserver observer = new ListViewScrollObserver(listView);
        //        listView.setOnScrollListener(this);
        observer.setOnScrollUpAndDownListener(new OnListViewScrollListener() {
            @Override
            public void onScrollUpDownChanged(int delta, int scrollPosition, boolean exact) {
                onNewScroll(delta);
                snap(headerTop == scrollPosition);
            }
 
            @Override
            public void onScrollIdle() {
                QuickReturnHeaderHelper.this.onScrollIdle();
            }
        });
 
        root.addView(realHeader, realHeaderLayoutParams);
 
        dummyHeader = new View(context);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, headerHeight);
        dummyHeader.setLayoutParams(params);
        listView.addHeaderView(dummyHeader);
    }

添加好布局以后,监听ListView的onScroll,向上滑动就逐渐隐藏我们自定义的Header,向下滑动就将Header显示出来。

以上就是全部分析了,大家看了源码,估计很快就能明白。

我认为项目可能还有很多改进的地方,也不一定适合大家的需求,(好多时候我们自定义的ListView,已经使用了ListView的HeadView,这样再增加空白HeadView就不合适了)

但项目提供了一种可以借鉴的思路,相信再很多时候还是很有用的。

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

其他电脑(比如安卓手机/平板电脑)的屏幕坏了,你可能想在安排维修之前紧急访问一些东西。你可以使用android的USB OTG功能(是的,几乎每个android都支持这个功能,你可以将鼠标和键盘连接到它)。

关键字: USB 鼠标 Android 树莓派

Google 宣布与中国 AR 科技公司 XREAL 达成深度战略合作,联合推出全球首款专为 Android XR 平台打造的旗舰级 AR 眼镜 Project Aura。

关键字: Google XREAL Android XR眼镜 AR

继停止维护AOSP开源项目后,谷歌母公司Alphabet近日被曝在其安卓系统(Android)、Pixel手机以及Chrome浏览器等部门裁员数百人。这一举措引发了业界的广泛关注,也引发了对谷歌未来业务布局的诸多猜测。

关键字: 谷歌 AOSP Android 裁员

在本教程中,我们将使用Capacitor 6、Angular和TypeScript构建一个Android应用程序,该应用程序通过串行端口连接到BleuIO USB加密狗。该应用程序允许用户直接从Android设备发送和接...

关键字: Android USB 电容器 BLE设备

早前媒体报道谷歌将停止维护Android开源项目(AOSP),将Android开发全面转向内部闭源分支,目前这一消息已经得到谷歌官方确认。

关键字: 谷歌 Android 开源

本项目演示了如何通过OTG (on - go) USB在Android设备上使用BleuIO USB加密狗作为串行端口。使用电容器6和@adeunis/电容器-串行插件,我们建立串行连接,发送AT命令,并实时读取响应。该...

关键字: 电容器 Android 传感器 微控制器 嵌入式系统

在Linux操作系统中,Android Debug Bridge(ADB)是一个功能强大的命令行工具,它允许开发者在计算机和Android设备之间建立通信,从而进行调试、管理、安装应用等操作。本文将详细介绍在Linux系...

关键字: Linux系统 Android Debug ADB

随着Android操作系统的进步,智能手机的使用日益增加。随后,有报道称,恶意个人和黑客利用 Android 提供的漏洞来访问用户珍视的数据。例如,此类威胁包括 2021 年针对 Android 设备发布的 Flubot...

关键字: Android 恶意软件

在本教程中,我们将构建超出电子领域的东西。作为一名电子工程师,我们大多数人都想为我们的物联网应用程序构建一些用户界面,在大多数情况下,Android应用程序将是用户与我们的物联网设备交互的正确选择。所以,如果你想为你的物...

关键字: 物联网 Android
关闭