简体   繁体   中英

Changing color of the border of the background of a list item

I was going through this tutorial and had the idea of making it so when you press a button, one of the cards borders changes color.

I looked at this solution , but nothing happens.

Here is what I have done:

I have an xml in drawable for the shape (feed_item.xml):

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

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Bottom 2dp Shadow -->
    <item>
        <shape  android:shape="rectangle">

            <solid android:color="#d8d8d8" />
            <corners android:radius="7dp" />

        </shape>
    </item>
    <!-- White Top color -->
    <item android:bottom="3px" android:id="@+id/feedsquare">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <!-- view background color -->
            <solid
                android:color="@color/white" >
            </solid>
            <!-- view border color and width (I WANT TO CHANGE THIS) -->
            <stroke
                android:width="1dp"
                android:color="@color/white" >
            </stroke>
            <!-- If you want to add some padding -->
            <padding
                android:left="4dp"
                android:top="4dp"
                android:right="4dp"
                android:bottom="4dp"    >
            </padding>
        </shape>
    </item>
</layer-list>

Then I have a layout that is basically how each card/list item looks (list_row.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:background="@drawable/feed_item"
    android:id="@+id/card"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:padding="2dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView android:id="@+id/title"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:layout_width="wrap_content"
        android:text="MMMMMMMMMM"
        android:textSize="@dimen/name3"
        android:textColor="@color/black"   />

    <!--android:scaleType="fitXY"-->
    <ImageView android:id="@+id/list_image"
        android:layout_height="180dp"
        android:layout_margin="2dp"
        android:layout_weight="0.04"
        android:layout_width="match_parent"
        android:scaleType="centerInside"
        android:adjustViewBounds="true"
        android:src="@mipmap/navigation_refresh"/>

    <TextView android:id="@+id/description"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:layout_width="wrap_content"
        android:text="MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"
        android:textSize="@dimen/description3"
        android:textColor="@color/black" />

</LinearLayout>

Now, I have a fragment with a listview. The fragment does exactly what the tutorial does (show 8 cards). I've added a button to the fragment that I want to change a specific cards border color (in the fragments onactivitycreated):

newpost = (Button) getActivity().findViewById(R.id.reply);
newpost.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        LayerDrawable layers = (LayerDrawable) getActivity().getResources().getDrawable(R.drawable.feed_item);

        GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.feedsquare));
        shape.setStroke(1 ,R.color.green);

    }
});

Nothing happens when I press the button. Any ideas as to what I can do?

To show selected item (row) in listview :

  1. create a variable in adapter class :

private int selectedPosition = 0;

  1. Create method in side adapter :

      public void setSelectedItem(int position) { this.selectedPosition = position; notifyDataSetChanged(); } 
  2. Add check in getView methode

    if (position == selectedPosition) { convertView.setBackground(context.getResources().getDrawable(R.drawable.selector_rect_black_trans_bg)); } else convertView.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));

  3. selector_rect_black_trans_bg.xml :

     <solid android:color="@color/gray1" /> <stroke android:width="2dp" android:color="@color/gray6" /> <corners android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp" android:topLeftRadius="0dp" android:topRightRadius="0dp" /> 

In Activity, only you need to pass position in adapter to show highlighted. its tested code and it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM