android - 更改所选和按下的 ListView 项目的颜色

标签 android android-listview textview

我正在努力自定义我的 ListView 。

我更改了 listView 中 TextView 的背景。

<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"/>

然后我在drawable文件夹中创建了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>

编辑:这是 ListView 的代码 XML 文件...ListView 是抽屉导航的一部分

<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;     
}

最佳答案

从 TextView 中删除这一行:

android:background="@drawable/list_selector_orange"

并将其放入ListView

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

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

希望能帮到你。

关于android - 更改所选和按下的 ListView 项目的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17842812/

相关文章:

Android模拟器无法在eclipse上启动

android - 防止在 timePicker DialogFragment 中选择过去的日期

安卓工作室 : how to see a list of all warnings?

Android,findViewById 返回 null

android - 如何在 ListView onItemClick 中获取 HashMap id 值?

Android-Textview 在拖动操作时消失

android - 通过调用新的 Activity,位图大小超出 VM 预算

android - 在Android中放置点击对话框?

android - 向自定义 TextView 添加文本是不可见的

Android TextView 重力底部意外行为