Last active
December 14, 2015 18:49
-
-
Save daichan4649/5132126 to your computer and use it in GitHub Desktop.
行選択可能(Checkable)な ListView に設定してるカスタムビュー内の 背景色/テキスト文字色 を selector だけで変更する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package daichan4649.test; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.Checkable; | |
import android.widget.LinearLayout; | |
public class CheckableLayout extends LinearLayout implements Checkable { | |
private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked }; | |
public CheckableLayout(Context context) { | |
super(context, null); | |
} | |
public CheckableLayout(Context context, AttributeSet attrs) { | |
super(context, attrs, 0); | |
} | |
public CheckableLayout(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
private boolean checked; | |
@Override | |
public boolean isChecked() { | |
return checked; | |
} | |
@Override | |
public void setChecked(boolean checked) { | |
if (this.checked != checked) { | |
this.checked = checked; | |
refreshDrawableState(); | |
for (int i = 0; i < getChildCount(); i++) { | |
View child = getChildAt(i); | |
if (child instanceof Checkable) { | |
((Checkable) child).setChecked(checked); | |
} | |
} | |
} | |
} | |
@Override | |
public void toggle() { | |
setChecked(!checked); | |
} | |
@Override | |
protected int[] onCreateDrawableState(int extraSpace) { | |
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); | |
if (isChecked()) { | |
mergeDrawableStates(drawableState, CHECKED_STATE_SET); | |
} | |
return drawableState; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package daichan4649.test; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.Checkable; | |
import android.widget.TextView; | |
public class CheckableTextView extends TextView implements Checkable { | |
private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked }; | |
public CheckableTextView(Context context) { | |
super(context, null); | |
} | |
public CheckableTextView(Context context, AttributeSet attrs) { | |
super(context, attrs, 0); | |
} | |
public CheckableTextView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
private boolean checked; | |
@Override | |
public boolean isChecked() { | |
return checked; | |
} | |
@Override | |
public void setChecked(boolean checked) { | |
if (this.checked != checked) { | |
this.checked = checked; | |
refreshDrawableState(); | |
} | |
} | |
@Override | |
public void toggle() { | |
setChecked(!checked); | |
} | |
@Override | |
protected int[] onCreateDrawableState(int extraSpace) { | |
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); | |
if (isChecked()) { | |
mergeDrawableStates(drawableState, CHECKED_STATE_SET); | |
} | |
return drawableState; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<daichan4649.test.CheckableLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@drawable/selector_listgroup" | |
android:orientation="vertical"> | |
<daichan4649.test.CheckableTextView | |
android:id="@+id/text1" | |
android:layout_width="match_parent" | |
android:layout_height="0dip" | |
android:layout_weight="1" | |
android:clickable="false" | |
android:focusable="false" | |
android:focusableInTouchMode="false" | |
android:gravity="center_vertical" | |
android:textColor="@color/selector_listgroup_text" /> | |
</daichan4649.test.CheckableLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="?android:attr/activatedBackgroundIndicator" | |
android:orientation="vertical" > | |
<TextView | |
android:id="@+id/text" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item android:drawable="@drawable/shape_blue" android:state_checked="true"/> | |
<item android:drawable="@drawable/shape_normal"/> | |
</selector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item android:state_checked="true" android:color="@color/White" /> | |
<item android:color="@color/Black" /> | |
</selector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
ListView listView = (ListView) view.findViewById(R.id.list); | |
・・・ | |
listView.setAdapter(adapter); | |
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); | |
・・・ | |
} |
・list_item_bg.xml
背景色をカスタムする必要がない(標準の色selectorでいい)場合は、「android:background="?android:attr/activatedBackgroundIndicator"」 を指定するのみでOK。独自selector不要。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
・ListView 内のレイアウトが 「android.R.layout.simple_list_item_activated_1」
・ListView#setChoiceMode(ListView.CHOICE_MODE_SINGLE)
これのカスタムビュー版。