4

저는 사용자 정의 갤러리에 연결되는 LinearLayout 하위 (ImageView 및 TextView 각각)가있는 확장 BaseAdapter를 보유하고 있습니다.Android : 사용자 정의 갤러리 setSelection() 문제

처음 액티비티를 시작할 때 setSelection(position)으로 전화를 걸어 ImageView에서 선택기를 "선택한"이미지로 변경하려고합니다. 이 기능은 이후 선택한 어린이에게 갤러리를 공개 한 후에 작동하지만 처음 앱을 시작한 것은 아닙니다.

내 선택 :

내 첫번째 추측은 내가 이런 식으로 할 시도한 setSelection()를 호출 한 후 어댑터 notifyDataSetChanged()를 호출하는 것이 었습니다
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_selected="true" 
    android:drawable="@drawable/home_image_select" /> 
<item android:state_selected="false" 
    android:drawable="@drawable/home_image" /> 
</selector> 

:

didn를
((CustomAdapter) gallery.getAdapter()).notifyDataSetChanged(); 

' 아무것도하지 마라. 또한 갤러리 클래스의 setSelection()을 재정의하여이 작업을 수행해 보았습니다.

View v = this.getAdapter().getView(position, null, this);  
((ImageView) v.findViewById(R.id.gallery_image)).setSelected(true); 

그 중 하나도 작동하지 않습니다. 내가 누락되었거나 시도 할 수있는 것이 있습니까?

답변

0

갤러리의 setSelection()을 덮어 쓰면 내 자신의 문제에 대한 해결책을 찾았습니다 (결국 작동했습니다).

@Override 
public void setSelection(int position) { 
    super.setSelection(position); 

    View v = this.getAdapter().getView(position, null, this); 
    v.setFocusable(true); 
    v.requestFocus(); 
} 
0

나는 데이터 집합을 기초하여 변경하는 경우, 선택 상태가 해제됩니다 당신이 notifyDataSetChanged() 호출하지한다고 생각합니다.

그냥 setSelection(position)으로 전화하면 내 앱에서 작동합니다.