Set card background using StateListDrawable

This commit is contained in:
Mygod
2017-01-05 21:21:51 +08:00
parent af9e8c786d
commit 69d2bc1ee5
5 changed files with 29 additions and 34 deletions
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<selector>
<item android:state_selected="true">
<shape android:shape="rectangle">
<solid android:color="@color/material_green_700" />
<corners android:radius="4dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@color/material_primary_300" />
<corners android:radius="4dp"/>
</shape>
</item>
</selector>
</item>
<item android:left="8dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:bottomRightRadius="4dp" android:topRightRadius="4dp"/>
</shape>
</item>
</layer-list>
@@ -1,15 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/material_primary_300" />
<corners android:radius="4dp"/>
</shape>
</item>
<item android:left="8dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:bottomRightRadius="4dp" android:topRightRadius="4dp"/>
</shape>
</item>
</layer-list>
@@ -1,15 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/material_green_700" />
<corners android:radius="4dp"/>
</shape>
</item>
<item android:left="8dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:bottomRightRadius="4dp" android:topRightRadius="4dp"/>
</shape>
</item>
</layer-list>
-1
View File
@@ -6,7 +6,6 @@
android:layout_height="wrap_content">
<com.github.shadowsocks.widget.BoundedCardView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_selectable"
android:layout_marginTop="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
@@ -79,11 +79,12 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic
private val title = itemView.findViewById(R.id.title).asInstanceOf[TextView]
private val address = itemView.findViewById(R.id.address).asInstanceOf[TextView]
private val traffic = itemView.findViewById(R.id.traffic).asInstanceOf[TextView]
private val indicator = itemView.findViewById(R.id.indicator).asInstanceOf[ViewGroup]
private val edit = itemView.findViewById(R.id.edit)
edit.setOnClickListener(_ => startConfig(item.id))
edit.setOnLongClickListener(cardButtonLongClickListener)
itemView.setOnClickListener(this)
// it will not take effect unless set in code
itemView.findViewById(R.id.indicator).setBackgroundResource(R.drawable.background_profile)
private var adView: NativeExpressAdView = _
@@ -115,10 +116,10 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic
TrafficMonitor.formatTraffic(tx), TrafficMonitor.formatTraffic(rx)))
if (item.id == app.profileId) {
indicator.setBackgroundResource(R.drawable.background_selected)
itemView.setSelected(true)
selectedItem = this
} else {
indicator.setBackgroundResource(R.drawable.background_selectable)
itemView.setSelected(false)
if (selectedItem eq this) selectedItem = null
}