ListView嵌套Viewpager实现左滑打电话,右滑发短信(附源码)

本文介绍如何在ListView中使用Viewpager实现左滑打电话、右滑发短信的功能。通过ListView每个Item包含一个ViewPager,同时处理好滑动事件避免混乱。详细介绍了布局文件、关键Java文件的实现,并提到了Touch事件的分发和消费机制的重要性。提供源码下载供进一步学习。

最近自己一直在写一个开源的通讯录项目,在实现的过程中参照了不少优秀通讯录的用户体验效果,比如现在很流行的左滑打电话,右滑发短信功能,先来看看实现后的效果图(在下载Android手机通讯录和Listview实现A-Z排序这篇样例代码上增加的)

关于ListView的Item左右滑动实现的方式网上也有很多种,我这里用的是ListView嵌套Viewpager来实现的.下面说说思路和需要注意的地方

1,ListView中每一个Item的联系人都是一个ViewPager(如效果图)

2,这两个组件都是可以滑动的,为了避免滑动出现混乱或者不好的用户体验,当父组件ListVew在滑动时子组件Viewpager不能滑动,同样在子ViewPager滑动时父ListView不能滑动


先看看布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_dark"
    android:gravity="center"
    android:orientation="vertical" >
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="通话"
        android:textSize="25dp"
        android:textColor="#FFFFFF"/>

</LinearLayout>
fragment1.xml是蓝色背景的"通话"界面布局,Viewpager中的第一个页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_dark"
    android:gravity="center"
    android:orientation="vertical" >
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="短信"
        android:textSize="25dp"
        android:textColor="#FFFFFF"/>

</LinearLayout>
fragment3.xml是绿色 背景的"短信"界面布局,Viewpager中的第三个页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView 
            android:id="@+id/contacts_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#336598"
            android:textStyle="bold"/>
        <TextView 
            android:id="@+id/contacts_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#336598"/>
    </LinearLayout>
	    
	
</LinearLayout>

list_item.xml是显示"联系人姓名和电话"界面布局,Viewpager中的第二个页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical" >
    
    	<TextView 
	    android:id="@+id/pb_item_LetterTag"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:background="#E0E0E0"
	    android:visibility="gone"
	    android:paddingLeft="5dp"/>
    	
    	<!-- 自定义的Viewpager -->
        <huahua.mycontacts.ItemsViewPager
        android:id="@+id/tabcontent_vp"
        android:background="@andro
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值