How to change the dynamic color of the pressed color to a different color of the selector?

what is my selector for the list: (item_selector.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@color/orange" />
    <item android:state_focused="true" android:drawable="@color/orange" />
    <item android:state_pressed="true" android:drawable="@color/orange" />
    <item android:drawable="@color/transparent" />
</selector>

what's my list line (row_item.xml)

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/alarm_item_drawer_selector"
    >

    <ImageView 
        android:id="@+id/item_icon"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerHorizontal="true"
        android:paddingTop="5dp"
        android:paddingBottom="2dp"
        android:src="@drawable/ic_launcher"/>

    <TextView      
        android:id="@+id/item_title"
        style="@style/DrawerItemTextStyle"
        android:textColor="@color/text_drawer_item_selector"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/item_icon" />

</RelativeLayout>

I want to change android: state_pressed = "true" android: drawable = "@ color / orange" to another color dynamically. My goal is to make the pressed color for each line different. Can this be done?

+4
source share
3 answers

Affects each item with a new StateListDrawable.

    StateListDrawable stateListDrawable= new StateListDrawable();
    stateListDrawable.addState(new int[] {android.R.attr.state_pressed}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor)));
    stateListDrawable.addState(new int[] {android.R.attr.state_focused}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor)));
    stateListDrawable.addState(new int[] {android.R.attr.state_selected}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor)));
    stateListDrawable.addState(new int[] {}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor)));

    view.setBackgroundDrawable(stateListDrawable);
+7
source

, , , , . , .

:

listview.xml( )

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/parent"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:id="@+id/text"
    android:text="Text"
    android:padding="20dp"/>

</LinearLayout>

getView() :

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    Holder holder = new Holder();
    if (view == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.listview, viewGroup, false);
    } else {

        holder = (Holder) view.getTag();
    }

    holder.parent = (LinearLayout) view.findViewById(R.id.parent);
    setRandomListViewSelector(holder.parent);
    holder.text = (TextView) view.findViewById(R.id.text);
    holder.text.setText(strs.get(i));

    return view;
}

, :

public static void setRandomListViewSelector(View parentView) {

        int i = new Random().nextInt(2);

        switch (i) {
            case 0:
                parentView.setBackgroundResource(R.drawable.list_selector_blue);
                break;

            case 1:
                parentView.setBackgroundResource(R.drawable.list_selector_red);
                break;
       }

}

list_selector_blue.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape>
        <solid android:color="@android:color/holo_blue_dark"/>
    </shape>
</item>
</selector>

Random 0 1. , 1, , .

+1

, . onTouchListener, , .

    final TextView textView =findViewById(R.id.item_title);
    textView.setOnTouchListener(new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent)
        {
            switch (motionEvent.getAction()){
                case MotionEvent.ACTION_DOWN:
                    textView.setTextColor(Color.RED);
                    break;
                case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:
                    textView.setTextColor(Color.BLACK);
                    break;
            }
            return false;
        }
    });

, . Color.RED

0
source

All Articles