ListView中单个item的跑马灯效果;及所有item实现跑马灯

为使界面效果更完善 ,能让用户知道一条消息后面未显示的内容。需对ListView进行设置跑马灯效果。

本案例中,实现以下跑马灯效果:

1、ListView需要用手触碰点击,才实现跑马灯(且是一个item跑起来):原因是该item获取到了触碰的焦点,其他item没有被触碰到,所以只有被触碰的item才能跑起来。代码如,红色字体为重要部分

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
  
    <TextView
        android:id="@+id/widgetTips_txt"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="2dp"
        android:paddingLeft="10dp"
        android:paddingBottom="1dp"
        android:paddingRight="5dp"
        android:textSize="14sp"
        android:singleLine="true"

        android:scrollHorizontally="true"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:ellipsize="marquee"

        android:marqueeRepeatLimit="marquee_forever"       

        android:text="获取数据失败,请检查网络设置" 
        android:textColor="#ffffffff"> 
</LinearLayout>


2、ListView不需要用手触碰,设置属性后可以自动跑起来(且是一个item跑起来),代码如下,红色字体为重要部分

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
  
    <TextView
        android:id="@+id/widgetTips_txt"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="2dp"
        android:paddingLeft="10dp"
        android:paddingBottom="1dp"
        android:paddingRight="5dp"
        android:textSize="14sp"
        android:singleLine="true"

        android:scrollHorizontally="true"

        android:selectAllOnFocus="true"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:ellipsize="marquee"

        android:marqueeRepeatLimit="marquee_forever"
        android:text="获取数据失败,请检查网络设置" 
        android:textColor="#ffffffff">
        <requestFocus

                 android:focusable="true"

                 android:focusableInTouchMode="true"

                 android:duplicateParentState="true"/>   

  </TextView>
      
</LinearLayout>

解析:

<requestFocus />: 标签用于指定屏幕内的焦点View。 用法: 将标签置于Views标签内部 。

本案例中将<requestFocus />放置于<TextView></TextView>中,译为无需手动触碰点击屏幕,TextView已经处于焦点状态。


3、ListView不需要手动触碰,且所有item实现跑马灯效果,代码如下,红色字体为重要部分

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants">
  
    <TextView
        android:id="@+id/widgetTips_txt"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="2dp"
        android:paddingLeft="10dp"
        android:paddingBottom="1dp"
        android:paddingRight="5dp"
        android:textSize="14sp"
        android:singleLine="true"

        android:scrollHorizontally="true"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:ellipsize="marquee"

        android:marqueeRepeatLimit="marquee_forever"       

        android:text="获取数据失败,请检查网络设置" 
        android:textColor="#ffffffff"> 
        <requestFocus

          android:focusable="true"

         android:focusableInTouchMode="true"

         android:duplicateParentState="true"/>
    </TextView>
       
</LinearLayout>

解析:

“android:descendantFocusability”属性来强制获取焦点,该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。

该属性的值有三种:

        beforeDescendants:viewgroup会优先其子类控件而获取到焦点

        afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点

        blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

亦可参考该大神解析http://www.cnblogs.com/eyu8874521/archive/2012/10/17/2727882.html

以上就是listView中所遇到的常见跑马灯问题,希望对大家能有所帮助,如有不足之处还请贵客留言补充,谢谢!





版权声明:本文为qq_16505961原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。