0

我是 android 新手和我的使用列表视图,我想在选择时更改列表项颜色,但它不起作用。

我检查了堆栈溢出的几乎所有链接并尝试了所有链接,但没有运气......请检查我的代码......并告诉我问题出在哪里。

public class Main extends Activity {

    private ListView listView;
    private String[] menuArray;
    private List<String> menuList = new ArrayList<String>();
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        menuArray = getResources().getStringArray(R.array.Menu);
        menuList = Arrays.asList(menuArray);

        listView = (ListView) findViewById(R.id.listView_main_menu);
        listView.setAdapter(new EfficientAdapter(this,menuList));       
    }

/******************************** EfficientAdapter ************************************/

    public class EfficientAdapter extends BaseAdapter{


        private LayoutInflater mInflater;
        @SuppressWarnings("unused")
        private String TAG=EfficientAdapter.class.getSimpleName();
        @SuppressWarnings("unused")
        private Context context;

        private List<String> data= new ArrayList<String>();

        public EfficientAdapter(Context context,List<String> data) {
            super();
            mInflater = LayoutInflater.from(context);
            this.context=context;
            this.data.addAll(data);
        }


        public EfficientAdapter(Context context) {
            super();
            mInflater = LayoutInflater.from(context);
            this.context=context;
        }
        public View getView(final int position, View convertView, final ViewGroup parent) {

            ViewHolder holder;

            if(convertView == null){
                convertView = mInflater.inflate(R.layout.main_list, null);              
                holder = new ViewHolder();
                holder.textLine = (TextView) convertView.findViewById(R.id.textView_mainlist_item);
                convertView.setTag(holder);
            }
            holder =(ViewHolder) convertView.getTag();
            holder.textLine.setText(getItem(position));

            convertView.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {

                }
            });

            return convertView;
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return data.size();
        }

        public String getItem(int position) {
            // TODO Auto-generated method stub
            return data.get(position);
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        class ViewHolder {
            TextView textLine;
        }
    }
}

这是我的带有列表视图的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android1="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/Gainsboro"
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">


    <ListView
        android:id="@+id/listView_main_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:choiceMode="singleChoice"
        android:divider="@color/DarkGray"
        android:dividerHeight="1dip"
        android:fadingEdge="none"
        android:listSelector="@drawable/row_color"
        android:scrollbars="none" >

    </ListView>

</RelativeLayout>

row_color.xml

<item android:state_pressed="false" android:state_focused="false"
    android:drawable="@color/Green" />

<item android:state_focused="true"
    android:drawable="@color/Blue" />
<item android:state_pressed="true"
    android:drawable="@color/Brown" />

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

main_list.xml; 这个 xml 包含正在膨胀的 textview

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">




    <TextView
        android:id="@+id/textView_mainlist_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingTop="10dp"
        android:text="TextView"
        android:textColor="@color/DarkSlateGray"
        android:textSize="20sp"
        android:textStyle="bold" />



    <TextView
        android:id="@+id/TextView01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right|center"
        android:paddingRight="30dp"
        android:paddingTop="6dp"
        android:text=">"
        android:textColor="@color/DarkSlateGray"
        android:textSize="28sp"
        android:textStyle="bold" />

</FrameLayout>

刚刚发现只有这部分工作正常

<item android:state_pressed="false" android:state_focused="false"
        android:drawable="@color/Green" />
4

2 回答 2

1

代替

convertView = mInflater.inflate(R.layout.main_list, null);

尝试

convertView = mInflater.inflate(R.layout.main_list, parent, false);

编辑

我明白你现在想要做什么。您不想android:ListSelector在 ListView 上拥有,而是在每个单独FrameLayout的 . 所以:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:listSelector="@drawable/row_color" >

您需要将其应用于每个单独的视图,以便在按下时让它们改变颜色等。

于 2012-07-26T19:31:23.320 回答
0

访问http://android-codes-examples.blogspot.in/2011/03/customized-listview-items-selection.html

希望这对你有用

于 2012-07-26T19:51:49.707 回答