0

내 문제에 대한 해결책을 찾지 않고 웹 검색을 한 후에이 질문을 게시하고 있습니다. 나는 몇몇 다른 포스트에서 그것이 토론 된 ㄴ다는 것을 알고있다, 그러나 그중 아무도는 문제를 해결하는 것을 도왔다.안드로이드리스트보기 행의 하이라이트

선택한 항목이 강조 표시된 ListView를 구현했습니다. 모든 것이 매우 잘 작동하고 있는데, 행을 선택하면 myListView.setSelection (인덱스)을 사용할 때 강조 표시되지만, 실제로 해결해야하는 문제가 있습니다. 레이아웃에서 다른 뷰 (버튼, 확인란, 라디오 버튼 ecc) ListView가 선택을 잃습니다.

내가 표준 ListView에

public SelectableListView(Context context) { 
     super(context); 
     initStyle(null); 
    } 

    public SelectableListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initStyle(attrs); 
    } 

    public SelectableListView(Context context, AttributeSet attrs, int defStyle) { 

     super(context, attrs, defStyle); 
     initStyle(attrs); 

    } 

    private void initStyle(AttributeSet attrs) { 

     this.setBackgroundResource(R.drawable.red_border_fat); 
     this.setPadding(3, 3, 3, 3); 

     this.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
       //without the following 2 lines the selection won't work 
       requestFocusFromTouch();// 
       arg1.setSelected(true); 
      } 
     }); 

    } 

    @Override 
    public void setSelection(int position) { 
      //without the following line the selection won't work 
     requestFocusFromTouch(); 
     super.setSelection(position); 
    } 

목록보기이이

<my.name.space.SelectableListView 
     android:layout_width="wrap_content" 
     android:layout_height="100dp" > 
    </my.name.space.SelectableListView> 

에이버리 간단한 방법으로 레이아웃에 추가됩니다 드리고 있습니다 :

이리스트 뷰가 구현되는 방법입니다 어댑터

public static SimpleAdapter GetClassAdapter(Context context,ArrayList<SeatClass> items){ 
     try { 
      final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); 
      String[] fields = new String[] { "className" }; 
      int[] views = new int[] { R.skin_class.lblClassDescription}; 
      SimpleAdapter adapter = new SimpleAdapter(context, list, R.layout.skin_class_list, fields, views); 
      for (SeatClass sc : items) { 
       HashMap<String, String> temp = new HashMap<String, String>(); 

       temp.put("className", Typologic.getClasse(sc.Id).Description); 

       list.add(temp); 

      } 
      return adapter; 
     } catch (Exception e) { 
      LogHelper.WriteLogError("error in GetClassAdapter", e); 
      return null; 
     } 

    } 
,

이 각 행의 배치도이다 (R.layout.skin_class_list)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+skin_class/mainview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/selectable_listview_background_selector" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+skin_class/lblClassDescription" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="DESCRIPTION" 
     android:textColor="@drawable/selectable_listview_textcolor_selector" /> 

</LinearLayout> 

selectable_listview_background_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_selected="true" android:state_focused="true" android:drawable="@color/black" /> 
    <item android:state_selected="true" android:state_focused="false" android:drawable="@color/black" /> 
    <item android:state_pressed="true" android:state_focused="true" android:drawable="@color/black" /> 
    <item android:state_pressed="true" android:state_focused="false" android:drawable="@color/black" /> 

    <item android:drawable="@color/White" /> 

</selector> 

selectable_listview_textcolor_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_selected="true" android:color="@color/White" />: 
    <item android:state_pressed="true" android:color="@color/White" />: 
    <item android:color="@color/black" /> 

</selector> 

난이 완벽하게 작동 말했듯 그러나 다른보기를 클릭하면 강조 표시를 유지할 수 없습니다. 솔루션을 찾는 요일을 검색했지만 아무 것도 작동하지 않는 것 같습니다.

편집 : 이 솔루션을 찾았는데 더 이상 선택자를 사용하지 않습니다. 이 경우 무관 - - 여기 MyLabel은 일부 사용자 정의 텍스트 뷰는 기능 아직도 어떤 경우에 ... 더 나은 방법에 대한 제안을 기다리고

public abstract class SelectableListView<T> extends ListView { 

    protected ArrayList<T> _data; 
    protected int _selectedIndex = 0; 

    public SelectableListView(Context context) { 
     super(context); 
     initStyle(null); 
    } 

    public SelectableListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initStyle(attrs); 
    } 

    public SelectableListView(Context context, AttributeSet attrs, int defStyle) { 

     super(context, attrs, defStyle); 
     initStyle(attrs); 

    } 

    public abstract void loadData(ArrayList<T> data); 

    public T getSelectedItem() { 
     if (_selectedIndex < 0) 
      return null; 
     else 
      return _data.get(_selectedIndex); 
    } 

    public T getItemAt(int position) { 
     if (position < 0 || position > _data.size()) 
      return null; 
     else 
      return _data.get(position); 
    } 

    @Override 
    public void setSelection(int position) { 

     colorizeSelection(position); 
     super.setSelection(position); 
     _selectedIndex = position; 
    } 


    private void colorizeSelection(int index){ 
     try { 
      for (int i = 0; i < this.getCount(); i++) { 
       if(i==index) 
        this.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.Red)); 
       else 
        this.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.White)); 
       ViewGroup root=(ViewGroup) this.getChildAt(i); 
       colorizeSelection(root,i==index); 
      } 
     } catch (Exception e) { 
      LogHelper.WriteLogError("error in colorizeSelection function", e); 
     } 
    } 

    //this function is called recursively to scan all childs of a given view 
    private void colorizeSelection(ViewGroup v,boolean selected){ 
     for (int j = 0; j < v.getChildCount(); j++) { 
      if(v.getChildAt(j) instanceof ViewGroup) 
       colorizeSelection((ViewGroup)v.getChildAt(j),selected); 
      else if (v.getChildAt(j) instanceof MyLabel) { 
       if(selected) 
        ((MyLabel)v.getChildAt(j)).setTextColor(getResources().getColor(R.color.White)); 
       else 
        ((MyLabel)v.getChildAt(j)).setTextColor(getResources().getColor(R.color.black)); 
       ((NtvLabel)v.getChildAt(j)).invalidate(); 
      } 

     } 
    } 

    private void initStyle(AttributeSet attrs) { 
     _selectedIndex = -1; 
     this.setBackgroundResource(R.drawable.red_border_fat); 
     this.setPadding(3, 3, 3, 3); 

     this.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
       colorizeSelection(arg2); 
       _selectedIndex = arg2; 

      } 
     }); 

    } 



} 

이 지금 작동 :)

+3

ALLCAPS가 필요하지 않은 경우 모두 onLostFocus에서 선택하십시오. –

+0

죄송합니다. 무엇에 관해 말하고 있습니까? ALLCAPS는 어디에서 사용 했습니까? – Apperside

+0

당신은 그들을 [여기] (http://stackoverflow.com/revisions/13843119/1) 사용했습니다. –

답변

0

언제 선택을 취소하면 다른보기를 클릭하십시오. 코드를 작성하여 ListView

+1

고마워요, 나는이 문제를 가장 우아한 해결책을 제공하기를 희망하면서 일시적으로 사용하기 위해이 더러운 작업을 시도 할 것입니다. – Apperside

+0

onLostFocus 이벤트가없고 onFocusChanged 이벤트 만 있습니다. 다른보기를 클릭하면 해고. – Apperside

관련 문제