Android에서 선택, 확인 및 활성화 된 상태의 차이점은 무엇입니까?
그 주가 무엇이 다른지 알고 싶습니다. 이것을 명확히하는 웹 페이지를 찾지 못했습니다.
Checked와 Activated의 차이점은 실제로 매우 흥미 롭습니다. Google 문서조차도 사과합니다 (아래에 강조가 추가됨).
... 예를 들어, 단일 또는 다중 선택이 활성화 된 목록보기에서 현재 선택 세트의보기가 활성화됩니다. (음, 예, 여기서 용어에 대해 깊이 죄송합니다.) 활성화 된 상태는 설정된 뷰의 하위 항목으로 전파됩니다.
따라서 차이점은 다음과 같습니다.
- Activated는 Honeycomb에 도입되었으므로 그 전에는 사용할 수 없습니다.
- Activated는 이제 모든보기의 속성입니다. setActivated () 및 isActivated () 메서드가 있습니다.
- 활성화 됨은 설정된 뷰의 하위 항목으로 전파됩니다.
- Checked는 Checkable 인터페이스를 구현하는 뷰를 중심으로 회전합니다. setChecked (), isChecked (), toggle () 메소드
ListView (Honeycomb 이후)는 아래와 같이 Android 버전에 따라 setChecked () 또는 setActivated ()를 호출합니다 (Android 소스 코드에서 가져옴).
if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) { if (child instanceof Checkable) { ((Checkable) child).setChecked(mCheckStates.get(position)); } else if (getContext().getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) { child.setActivated(mCheckStates.get(position)); } }
mCheckStates 변수에 유의하십시오. 목록에서 어떤 위치가 확인 / 활성화되었는지 추적합니다. 예를 들어 getCheckedItemPositions ()를 통해 액세스 할 수 있습니다. ListView.setItemChecked ()에 대한 호출은 위의 내용을 호출합니다. 즉, 동일하게 setItemActivated ()라고 할 수 있습니다.
Honeycomb 이전에는 목록 항목에 state_checked를 반영하는 해결 방법을 구현해야했습니다. 이것은 ListView가 레이아웃의 최상위 뷰에서만 setChecked ()를 호출하기 때문입니다 (레이아웃은 체크 할 수있는 기능을 구현하지 않음) ... 그리고 도움없이 전파되지 않기 때문입니다. 이러한 해결 방법은 다음과 같은 형식이었습니다. 루트 레이아웃을 확장하여 Checkable을 구현합니다. 생성자에서 Checkable을 구현하는 모든 자식을 재귀 적으로 찾습니다. setChecked () 등이 호출되면 해당 뷰에 대한 호출을 전달합니다. 이러한 뷰가 state_checked에 대해 다른 드로어 블을 사용하여 상태 목록 드로어 블 (예 : CheckBox)을 구현하면 체크 된 상태가 UI에 반영됩니다.
Honeycomb 이후 목록 항목에 멋진 배경을 만들려면 다음과 같이 state_activated 상태에 대한 드로어 블이있는 상태 목록 드로어 블이 있어야합니다 (물론 setItemChecked () 사용).
<item android:state_pressed="true" android:drawable="@drawable/list_item_bg_pressed"/> <item android:state_activated="true" android:drawable="@drawable/list_item_bg_activated"/> <item android:drawable="@drawable/list_item_bg_normal"/>
HoneyComb 이전에 목록 항목에 대한 멋진 배경을 만들려면 state_checked에 대해 위와 같은 작업을 수행하고 Checkable 인터페이스를 구현하기 위해 최상위 뷰를 확장해야합니다. 그런 다음 onCreateDrawableState ()를 구현하고 상태가 변경 될 때마다 refreshDrawableState ()를 호출하여 구현중인 상태가 참인지 거짓인지 Android에 알려야합니다.
<item android:state_pressed="true" android:drawable="@drawable/list_item_bg_pressed"/> <item android:state_checked="true" android:drawable="@drawable/list_item_bg_checked"/> <item android:drawable="@drawable/list_item_bg_normal"/>
... RelativeLayout에서 state_checked와 결합 된 Checkable을 구현하는 코드는 다음과 같습니다.
public class RelativeLayoutCheckable extends RelativeLayout implements Checkable {
public RelativeLayoutCheckable(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RelativeLayoutCheckable(Context context) {
super(context);
}
private boolean mChecked = false;
@Override
protected void onFinishInflate() {
super.onFinishInflate();
}
@Override
public boolean isChecked() {
return mChecked;
}
@Override
public void setChecked(boolean checked) {
mChecked = checked;
refreshDrawableState();
}
private static final int[] mCheckedStateSet = {
android.R.attr.state_checked,
};
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, mCheckedStateSet);
}
return drawableState;
}
@Override
public void toggle() {
setChecked(!mChecked);
}
}
다음 덕분에 :
http://sriramramani.wordpress.com/2012/11/17/custom-states/
Stackoverflow : 사용자 지정 단추 상태를 추가하는 방법
Stackoverflow : 선택기에 응답하는 사용자 지정 확인 가능한보기
http://www.charlesharley.com/2012/programming/custom-drawable-states-in-android/
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
http://blog.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/
According to the doc:
android:state_selected Boolean. "
true
" if this item should be used when the object is the current user selection when navigating with a directional control (such as when navigating through a list with a d-pad); "false
" if this item should be used when the object is not selected. The selected state is used when focus (android:state_focused) is not sufficient (such as when list view has focus and an item within it is selected with a d-pad).android:state_checked Boolean. "
true
" if this item should be used when the object is checked; "false
" if it should be used when the object is un-checked.android:state_activated Boolean. "
true
" if this item should be used when the object is activated as the persistent selection (such as to "highlight" the previously selected list item in a persistent navigation view); "false
" if it should be used when the object is not activated. Introduced in API level 11.
I think the doc is pretty clear, so what's the problem ?
Here is other solution for this problem: https://github.com/jiahaoliuliu/CustomizedListRow/blob/master/src/com/jiahaoliuliu/android/customizedlistview/MainActivity.java
I have overrided the method setOnItemClickListener and check differente cases in the code. But definitively the solution of Marvin is much better.
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
CheckedTextView checkedTextView =
(CheckedTextView)view.findViewById(R.id.checkedTextView);
// Save the actual selected row data
boolean checked = checkedTextView.isChecked();
int choiceMode = listView.getChoiceMode();
switch (choiceMode) {
// Not choosing anything
case (ListView.CHOICE_MODE_NONE):
// Clear all selected data
clearSelection();
//printCheckedElements();
break;
// Single choice
case (ListView.CHOICE_MODE_SINGLE):
// Clear all the selected data
// Revert the actual row data
clearSelection();
toggle(checked, checkedTextView, position);
//printCheckedElements();
break;
// Multiple choice
case (ListView.CHOICE_MODE_MULTIPLE):
case (ListView.CHOICE_MODE_MULTIPLE_MODAL):
// Revert the actual selected row data
toggle(checked, checkedTextView, position);
//printCheckedElements();
break;
}
}
});
'Programming' 카테고리의 다른 글
NSUserDefaults의 NSMutableArray에 사용자 지정 개체 저장 (0) | 2020.08.17 |
---|---|
제약 이름으로 테이블 이름 가져 오기 (0) | 2020.08.17 |
grep은 어떻게 그렇게 빨리 실행됩니까? (0) | 2020.08.17 |
Favicons-모범 사례 (0) | 2020.08.17 |
구체적인 클래스 이름을 문자열로 얻는 방법은 무엇입니까? (0) | 2020.08.17 |