android - 显示自定义 ListView 的默认选择颜色

标签 android listview

我有一个带有自定义 BaseAdapter 的 ListView 。 ListView 的每一行都有一个 TextView 和一个 CheckBox。

问题是当我单击(或触摸)任何行时,textview 前景变为灰色,而不是默认行为(背景 -> 绿色,textview 前景 -> 白色)。

代码如下:

行.xml:

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


    <TextView android:id="@+id/main_lv_item_textView" 
              style="@style/textViewBig"
              android:layout_alignParentLeft="true"/>


    <CheckBox android:id="@+id/main_lv_item_checkBox" 
              style="@style/checkBox"
              android:layout_width="wrap_content"
              android:layout_alignParentRight="true"/>

</RelativeLayout>

自定义适配器:

public class CustomAdapter extends BaseAdapter {
        private List<Profile> profiles;
        private LayoutInflater inflater;
        private TextView tvName;
        private CheckBox cbEnabled;

        public CustomAdapter(List<Profile> profiles) {
            this.profiles = profiles;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public int getCount() {
            return profiles.size();
        }

        public Object getItem(int position) {
            return profiles.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            View row = inflater.inflate(R.layout.main_lv_item, null);

            final Profile profile = profiles.get(position);
            tvName = (TextView) row.findViewById(R.id.main_lv_item_textView);
            registerForContextMenu(tvName);
            cbEnabled = (CheckBox) row.findViewById(R.id.main_lv_item_checkBox);
            tvName.setText(profile.getName());
            if (profile.isEnabled()) {
                cbEnabled.setChecked(true);
            }

            tvName.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString(PROFILE_NAME_KEY, profile.getName());
                    Intent intent = new Intent(context, GuiProfile.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });

            tvName.setOnLongClickListener(new OnLongClickListener() {
                public boolean onLongClick(View v) {
                    selectedProfileName = ((TextView) v).getText().toString();
                    return false;
                }
            });

            cbEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!profile.isEnabled()) {
                        for (Profile profile : profiles) {
                            if (profile.isEnabled()) {
                                profile.setEnabled(false);
                                Database.getInstance().storeProfile(profile);
                            }
                        }
                    }

                    profile.setEnabled(isChecked);
                    Database.getInstance().storeProfile(profile);
                    updateListView();
                }
            });

            return row;
        }
    }

如有任何帮助,我们将不胜感激。

最佳答案

实际上,发生的不是错误,而是 Android 中一个不幸的输入问题。当您将背景颜色设置为 R.color.black 时,您将其设置为一个 id 而不是实际颜色。由于颜色只是整数,而 id 也只是整数,所以它不知道区别,并将其解释为颜色。这样做的实际方法是将其设置为从 Resources 类获得的颜色,如下所示:

int black = activity.getResources().getColor(android.R.color.black);
view.setBackgroundColor(black);

希望对您有所帮助。

关于android - 显示自定义 ListView 的默认选择颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3643023/

相关文章:

java - getLoaderManager.initLoader 出现恼人的错误

android - 防止键盘在 EditText 单击时弹出?

android - SearchView 展开后隐藏的操作栏图标

c# - 在 ListView 中双击 Item 时获取 SubItem 值

java - 在 Activity 之间传递数据

android - 如何检测 ListView 是否在android中向上或向下滚动?

Android - 折线编码算法?

android - 从 Windows 区域设置代码获取语言名称

android - 如何使用rxandroid监听gps位置更新

WPF - 当 ListView 为空时不显示上下文菜单