Android Listview横向滑动

我有一个Listview,因为横屏和竖屏原因,内容较多,竖屏会出现有些数据无法显示

     于是让Listview在横向上可以滚动。

     解决的办法是,用HorizontalScrollView包装Listview,这样,当竖屏时,就会出现横向滚动条。  

  1. <HorizontalScrollView  
  2.    Android:layout_width="fill_parent"  
  3.    android:layout_height="fill_parent"  
  4.    android:fillViewport="true">  
  5.   <ListView android:layout_width="fill_parent"    
  6.      android:layout_height="fill_parent"  
  7.      android:layout_weight="1"  
  8.      android:background="@drawable/email_box"  
  9.      android:id="@+id/advice_main_list_id"  
  10.      android:padding="5dip"/>  
  11. </HorizontalScrollView>  

     但此时又出现了另一个问题,加上HorizontalScrollView后,虽然我已经设了Listview的宽度是fill_parent。但当内容较少时,Listview还是根据内容自适应宽度,不能满屏。

    此时,需要设置一个属性就能解决问题了。设置HorizontalScrollView的android:fillViewport="true"。也就是设置是否将HorizontalScrollView的内容宽度拉伸以适应视口(viewport)。




   <ListView  android:id="@+id/listView04"  
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:layout_weight="1"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="82dp"/>
        适配器的布局代码:
<?xml version="1.0" encoding="utf-8"?>

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/horizontalScrollView"  
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:scrollbars="none"
     android:descendantFocusability="blocksDescendants"
    >
    
    <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="match_parent" 
            android:orientation="horizontal"
            >
                <RelativeLayout  
                    android:id="@+id/contentLayout" 
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                    
                    <!-- 1. android:descendantFocusability="blocksDescendants" 阻止子项获取焦点 -->
                    <!-- 2. 在button控件上 增加  android:focusable="false"  阻止控件 获取焦点-->
                    <ImageView android:id="@+id/itemImage"
                        android:layout_width="wrap_content"
                                   android:layout_height="wrap_content"
                                   android:layout_alignParentRight="true"
                                   android:src="@drawable/ic_launcher"/>
                    
                    <CheckBox android:id="@+id/itemCheckbox"
                        android:layout_width="wrap_content"
                                   android:layout_height="wrap_content"
                                   android:layout_alignParentBottom="true"
                                   android:layout_alignParentRight="true"
                                   android:layout_marginRight="10dp" 
                                   android:visibility="gone"/>
                    
                    <TextView android:id="@+id/itemName"
                        android:layout_width="fill_parent"
                                   android:layout_height="wrap_content" 
                                   android:textSize="20sp"/>
                    
                        <Button android:id="@+id/itemButton"
                        android:layout_width="wrap_content"
                                   android:layout_height="wrap_content"
                                   android:textSize="16sp" 
                                   android:layout_below="@+id/itemName"
                                   android:text="获取值"
                                   android:focusable="false"/>
                </RelativeLayout>
                <LinearLayout  android:id="@+id/hiddenLayout" 
                            android:layout_width="80dp"
                            android:layout_height="match_parent" 
                            android:orientation="horizontal" 
                            android:gravity="center">
                        <Button android:id="@+id/deleteButton"
                        android:layout_width="wrap_content"
                                   android:layout_height="wrap_content"
                                   android:textSize="16sp" 
                                   android:text="删除"
                                   android:focusable="false"/>
                </LinearLayout>
                
        </LinearLayout>
    
        
</HorizontalScrollView>    这样可以实现水平滑动item

如果要做 水平滑动listview  可以自定义horizontalScroll  可以参考http://blog.csdn.net/yanzi1225627/article/details/21294553 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值