【问题标题】:How to highlight the group row of an ExpandableListView如何突出显示 ExpandableListView 的组行
【发布时间】:2012-08-06 10:42:14
【问题描述】:

使用基于State List Selector 的以下代码,当用户在显示屏上按下ExpandableListView 的单个子行时,我设法突出显示它:

XML 状态选择器代码:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

  <item 
      android:state_pressed="true"
      android:drawable="@color/highlighted_entry" /> 

  <item 
      android:state_selected="true"
      android:drawable="@color/highlighted_entry" />

  <item android:drawable="@color/transparent" />

</selector>

XML ExpandableListView 布局代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:background="@drawable/exp_list_entry_selector" 
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp" >

    <TextView android:id="@+id/exp_list_entry_title"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:gravity="center_vertical"
       android:layout_alignParentTop="true"
       android:layout_alignParentBottom="true"
       android:textStyle="bold"
       android:textSize="12dp"
       android:textColor="#000000" 
    />

</LinearLayout>

Java 代码:

@Override
public boolean onChildClick(    ExpandableListView parent, View v,
                                int groupPosition, int childPosition, long id ) 
{
    v.setSelected(true);    
    return false;
}

如果我尝试突出显示组行(可展开的 ListView 的条目),同样的方法将不起作用:当用户按下它时它会突出显示(并展开或折叠它的子项),但之后松开它,背景颜色将恢复正常。

无论是否展开,如何突出显示组行?

【问题讨论】:

  • 你试图在点击时突出显示展开列表,我的意思是你需要用户知道点击了哪个列表
  • @Rakshi - 是的,我需要突出显示列表的条目,无论它是子行还是父行,因为用户可能需要对其执行一些操作。有什么想法吗?
  • 您尝试添加一个像向下箭头一样的图像,以便用户知道它是一个扩展列表..
  • 你有什么想法吗?

标签: java android listview android-layout expandablelistview


【解决方案1】:

通过将android:background="@drawable/row_highlighter" 属性添加到expandableListViewchild.xml。并创建一个row_highlighter.xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@android:color/holo_orange_dark" android:state_activated="true"/>
<item android:drawable="@android:color/transparent"/>

</selector>

及源代码:

int index = parent.getFlatListPosition(ExpandableListView
                   .getPackedPositionForChild(groupPosition, childPosition));
parent.setItemChecked(index, true);

Highlighting the selected item in the ExpandableListView

【讨论】:

  • 感谢您的回答,我会检查并回复您,尽管这是一个非常过时的问题;D
【解决方案2】:

当你设置适配器时,像这样传递你自己的自定义布局

SimpleCursorTreeAdapter mAdapter = new ListinoProdottiAdapter(null, ListinoProdottiActivity.this,

                                R.layout.my_simple_expandable_list_item_1,
                                R.layout.listino_prodotti_row,
                                ....
                        );

R.layout.my_simple_expandable_list_item_1 内部(我将背景设置为红色)

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:background="@color/red"
/>

【讨论】:

  • 有没有使用State List Selector的解决方案?
  • 抱歉,State List Selector 是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多