温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ListView+Adapter适配器的使用与Listview常见的UI显示问题

发布时间:2020-07-27 13:31:20 来源:网络 阅读:249 作者:胡雨生 栏目:移动开发

一.ListView的事件:

1.setOnItemClickListener()点击事件

ListView+Adapter适配器的使用与Listview常见的UI显示问题

     listview.setOnItemClickListener(new OnItemClickListener() {
            @Override            public void onItemClick(AdapterView<?> parent, View view,                    int position, long id) {
                 Toast.makeText(ArrayAdapterActivity.this, "你点击了第"+(position+1)+"项", Toast.LENGTH_SHORT).show();
            }
        });

ListView+Adapter适配器的使用与Listview常见的UI显示问题

 

2.setOnItemLongClickListener()长按事件,设置“长按”listview某一项的监听事件。

ListView+Adapter适配器的使用与Listview常见的UI显示问题

    listview.setOnItemLongClickListener( OnItemLongClickListener() {  
            @Override  
              onItemLongClick(AdapterView<?> parent, View view,  
                     position,  id) {
          Toast.makeText(ArrayAdapterActivity., "你了第"+(position+1)+"项", Toast.LENGTH_SHORT).show();
           ; 
       } 
    });

ListView+Adapter适配器的使用与Listview常见的UI显示问题

 二.ListView由于item项中包含某些可以抢焦点的控件导致无法获取焦点问题

(注意:ListView的项根元素layout的宽要设置成android:layout_width="fill_parent",否则可能会因为项内容太短,导致点击时获取不到焦点)

  1>.如果你自定义ListView的项中包含能获得焦点的子控件(RadioGroup、Button、CheckBox、DatePicker、ImageButton、ScrollView、SeekBar、EditText、ToggleButton、RatingBar等)的话,默认焦点会被交给这些子控件,而ListView的项能被选中的基础是它能获取焦点,所以项中的这些子控件的焦点必须为false,这样ListView的项才能获取onItemLongClick事件与onItemClick事件

解决办法(以Button为例):

在布局文件中,在项的layout布局文件根元素中加入android:descendantFocusability="blocksDescendants"如:

ListView+Adapter适配器的使用与Listview常见的UI显示问题

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants" >

    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/header"
        android:layout_toRightOf="@id/header" />

    <TextView
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/name"
        android:layout_below="@id/name" />

    <Button
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/deleteselect" />
</RelativeLayout>

ListView+Adapter适配器的使用与Listview常见的UI显示问题

如果layout是程序动态生成的,则调用

layout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

  2>.如果你自定义ListView的项中包含能获得焦点的子控件GridView的话,默认焦点会被交给这些子控件,而ListView的项能被选中的基础是它能获取焦点,所以项中的这些子控件的焦点必须为false,这样ListView的项才能获取onItemLongClick事件与onItemClick事件

解决办法(以Button为例): 

      1.在布局文件中,在项的layout布局文件根元素中加入

android:descendantFocusability="blocksDescendants"

      2.程序中给GridView进行如下设置

gridView.setClickable(false);
gridView.setPressed(false);
gridView.setEnabled(false);

 三.ListView的UI显示中常见问题及解决办法

1>如果需求是listview点击时,item无背景变色效果

步骤:

  1.drawable文件夹中新建timer_list_selector.xml内容如下

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_selected="true" android:drawable="@color/transparent"/></selector>

  2.values文件夹中新建colors.xml内容如下

<?xml version="1.0" encoding="utf-8"?><resources>
    <color name="transparent">#50000000</color></resources>

  3.布局文件中给listview加入如下属性

android:listSelector="@drawable/timer_list_selector"

2>listview设置分割线Divider样式,布局文件给listview加入如下属性(已分隔符为颜色为例)

  android:divider属性用来设置分割线颜色(或图片),当设置为#00000000时表示item之间无间隙;

  通过android:dividerHeight属性设置分割线高度

    android:divider="#ff999999"
       android:dividerHeight="1sp"

3>UI展现时常见问题

  问题1:listview设置背景,拖动listview时显示黑色,只有拖动完才会显示我们设置的背景颜色或图片
  产生原因:listview的背景是固定不变的,默认Listview的每项的背景是透明的,拖动滚动条的过程中需要实时将每个项的显示内容跟背景进行混合运算,android系统为了优化这个过程,使用了android:cacheColorHint属性,在黑色背景下默认颜色为#191919,所以出现了上面的黑色显示问题
  解决办法:(根据需求而定)

    1.如果只换背景颜色:将android:cacheColorHint设置成和背景颜色一样或android:scrollingCache="false"如下         

      android:cacheColorHint="#ff00ff00"
      android:background="#ff00ff00"

        或

      android:scrollingCache="false"
          android:background="#ff00ff00"

    2.如果用图片做背景:将android:cacheColorHint设为#00000000变为透明或android:scrollingCache="false"即可如下
           

      android:cacheColorHint="#00000000"
          android:background="@drawable/ic_launcher"

        或           

      android:scrollingCache="false"
          android:background="@drawable/ic_launcher"

  问题2:listview上面或下面有黑色阴影

  解决办法:布局文件中给listview加入android:fadingEdge="none" 
  UI总结:

  综上问题得到最终的布局为(带分割线+背景色)
     

ListView+Adapter适配器的使用与Listview常见的UI显示问题

<ListView        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="180dip"
        android:divider="#ff999999"
        android:dividerHeight="1sp"
        android:fadingEdge="none" 
        android:scrollingCache="false"
        android:background="#ff00ff00"
        />

ListView+Adapter适配器的使用与Listview常见的UI显示问题

 

 

 

 


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI