ListView整个列表获取选择的颜色而不是子

时间:2018-03-19 10:24:40

标签: android listview selector

我正在尝试在listView中设置一个孩子的背景颜色,但无论出于何种原因,整个列表都会获得背景颜色。

这是选择器

    <?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
        android:drawable="@color/colorPrimary"/>
    <item
        android:state_pressed="true"
        android:drawable="@color/colorPrimary"/>
</selector>

我把它放在listView这里

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/feedback_popup" android:layout_height="match_parent" android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/background_normal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent" android:layout_height="match_parent"
            android:drawSelectorOnTop="true"
            android:background="@drawable/bg_key"
            android:id="@android:id/list">
        </ListView>
    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="Ok"/>

    <Button
        android:id="@+id/back_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:visibility="invisible"
        android:text="@string/action_back"/>

</RelativeLayout>

我也尝试在ListFragment中设置背景颜色,但由于某种原因,我根本没有得到那种颜色。

我喜欢这个

 @Override
    public void onStart() {
        super.onStart();
        ListView view = getListView();
        TextView previousSelected = null;
        int selectedPosition = 0;
        if(view != null) {
            int adapterSize = view.getAdapter().getCount();
            if (selectedView != null) {
                for (int i = 0; i < adapterSize; i++) {
                    Log.d(TAG, "view getchildat " + view.getAdapter().getView(i, null, view) + " selected " + selectedView);
                    if (((TextView) view.getAdapter().getView(i, null, view)).getText().toString().equalsIgnoreCase(((TextView) selectedView).getText().toString())){
                        previousSelected = (TextView) view.getAdapter().getView(i, null, view);
                        Log.d(TAG, "selected is " + previousSelected.getText().toString());
                        view.setSelection(i);
                        view.setSelected(true);
                        view.setFocusable(true);
                        selectedPosition = i;
                    }
                }
            } else if (tagScan.getLastFeedback()[0] != null) {
                Log.d(TAG, "count fragment is " + count);
                for(int i = 0; i < adapterSize; i++){
                    Log.d(TAG, "view getchildat " + ((TextView)view.getAdapter().getView(i, null, view)).getText().toString() + " selected " + tagScan.getLastFeedback()[count]);
                    if (((TextView) view.getAdapter().getView(i, null, view)).getText().toString().equalsIgnoreCase(tagScan.getLastFeedback()[count])){
                        previousSelected = (TextView) view.getAdapter().getView(i, null, view);
                        Log.d(TAG, "selected is " + previousSelected.getText().toString());
                        view.setSelection(i);
                        view.setSelected(true);
                        view.setFocusable(true);
                        selectedPosition = i;
                    }
                }
            }
            if(previousSelected != null){
                Log.d(TAG,"set color of the previous selection " + previousSelected.getText().toString());
                previousSelected.setBackgroundColor(Color.BLUE);
                previousSelected.setTextColor(Color.WHITE);
                selectedView = previousSelected;
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

new ArrayAdapter<String>(this, R.layout.feedback_list, feedback.getOptions()){
        @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        view.setBackgroundColor(Color.parseColor("#4286f4")); // change color code here as you want
        return view;
    }
};

如果您不想在运行时中设置项目背景颜色,请

更改您的R.layout.feedback_list

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="@color/someColor">
</RelativeLayout>

答案 1 :(得分:-1)

如果您使用的是自定义适配器,则可以使用view.setBackgroundColor(Color)为特定视图设置背景颜色。创建一个新类以便更清晰。

public class MyAdapter extends BaseAdapter {

    public MyAdapter(List<YourDataType> list, Context ctx) {
        super(ctx, R.layout.row_layout, list);
        this.list = list;
        this.context = ctx;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        // inflate layout and reuse view for better performance
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_layout, parent, false);
        }

        ...

        /*
         * HERE YOU SET YOUR BACKGROUND COLOR FOR THE VIEW
        */
        convertView.setBackgroundColor(0xFF00FF00);

        ...

        return convertView;
    }

    // some other implemented methods from BaseAdapter

}