2

我正在努力自定义我的列表视图。

我更改了 listView 中的 TextViews 的背景。

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_list_item_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#000"
android:background="@drawable/list_selector_orange"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

然后我在可绘制文件夹中创建了 list_selector_orange

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/selector_press_orange"/>
<item android:state_selected="true" android:drawable="@drawable/selector_focus_orange" />

如果我按下按钮,它可以工作:

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

    <gradient
        android:angle="270"
        android:endColor="#FE9A2E"
        android:startColor="#FE9A2E" />

</shape>

但是当前选择的项目无论如何都没有颜色......

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

    <gradient
        android:angle="90"
        android:endColor="#FF8000"
        android:startColor="#FF8000" />
</shape>

编辑:这是列表视图的代码 XML 文件...列表视图是导航抽屉的一部分

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"
    android:listSelector="@drawable/list_selector_orange" />

这是监听器的代码

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {
    // update the main content by replacing fragments
    if(fragment != null){
        //fragmentManager.beginTransaction().remove(fragment).commit();
    }

    Bundle args;
    switch(position){
    case 1:
        fragment = new de.sebspr.app08.viewpager.PagerFragment();
        args = new Bundle();
        args.putInt(de.sebspr.app08.viewpager.PagerFragment.ARG_PLAYER_NUMBER, position);
        fragment.setArguments(args);
        break;
    case 2:
        fragment = new de.sebspr.app08.viewpager.PagerFragment();
        args = new Bundle();
        args.putInt(de.sebspr.app08.viewpager.PagerFragment.ARG_PLAYER_NUMBER, position);
        fragment.setArguments(args);
        break;
    case 4:
        fragment = new de.sebspr.app08.halle.FragHalle();
        break;
    }

    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mListTitles[position].getTitle());
    mDrawerLayout.closeDrawer(mDrawerList);
}

我只是选择了一个自定义适配器,因为我想要有节标题。所以我创建了一个可点击的项目和一个不可点击的项目

public View getView(int position, View convertView, ViewGroup parent){
    Item item = items[position];
    ViewHolder holder;

    if(convertView == null){
        holder = new ViewHolder();
        if(item.isSection()){
            convertView = inflater.inflate(R.layout.drawer_list_section, null);
            holder.txt = (TextView) convertView.findViewById(R.id.drawer_list_section_txt);

            convertView.setLongClickable(false);
            convertView.setClickable(false);
            convertView.setOnClickListener(null);

            tFace = Typeface.createFromAsset(context.getAssets(),
                    "fonts/Roboto-BoldCondensed.ttf");
            holder.txt.setTypeface(tFace);

            convertView.setTag(holder);
        } else {
            convertView = inflater.inflate(R.layout.drawer_list_item, null);
            holder.txt= (TextView) convertView.findViewById(R.id.drawer_list_item_txt);

            tFace = Typeface.createFromAsset(context.getAssets(),
                    "fonts/Roboto-Regular.ttf");
            holder.txt.setTypeface(tFace);

            convertView.setTag(holder);
        }
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txt.setText(item.getTitle());

    return convertView;     
}
4

2 回答 2

1

从 TextView 中删除这一行:

android:background="@drawable/list_selector_orange"

并将其放入 ListView

<ListView
 .....
 android:listSelector"@drawable/list_selector_orange"
 .....
 />

之后,您选择的项目将改变颜色。

希望我能帮助你。

于 2013-07-24T19:18:29.457 回答
1

添加android:duplicateParentState="true"到您的 TextView,因此它将获得其父级的状态。

于 2013-07-30T05:18:52.700 回答