2014-09-18 3 views
0

enter image description here선택한 가로 목록 항목을 강조 표시합니다.

게임, 커뮤니티, 음악 등은 가로 목록보기입니다.

지금 커뮤니티가 선택되었으며 커뮤니티와 관련된 데이터가 아래에 나와 있습니다.

클릭하면 음악을 강조 표시하고 커뮤니티 색상을 기본값으로 변경하고 싶습니다.

listview.setOnItemClickListener(new OnItemClickListener() { 

@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
       long arg3) { 

((android.widget.LinearLayout)((android.widget.LinearLayout)arg1).getChildAt(2)).setBackgroundColor(getResources().getColor(R.color.MyBlue)); 
... 

이렇게하면 선택한 항목의 배경색이 변경되지만 스크롤 할 때 선택되지 않은 다른 항목도 변경됩니다. 위의 코드는 아래 표시된 항목의 xml 파일에서 정의되는 선형 레이아웃을 변경합니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="100dp" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/IVCategory" 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:contentDescription="@string/app_name" 
     android:layout_margin="10dp" 
     android:layout_gravity="center"  
     /> 

    <TextView 
     android:id="@+id/txtCatName" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:gravity="center" 
     android:background="@android:color/transparent" 
     /> 

    <LinearLayout 
     android:id="@+id/selectedBlue" 
     android:layout_width="match_parent" 
     android:layout_height="3dp" 
     android:orientation="vertical" > 
    </LinearLayout> 

</LinearLayout> 

그런 식으로 할 수 있습니까? 많이 검색했지만 셀렉터가 나를 어떻게 도울 수 있는지 이해할 수 없었습니다. 이 게시물에

+0

모습 [항목이 강조] [1] [1] : http://stackoverflow.com/questions/16189651/android-listview-selected-item-stay-highlighted – FreshD

답변

0
* Create Class: 

    public class Test { 
     private String name; 
     private int selected = 0; 


     public String getName() { 
      return name; 
     } 
     public void setName(String name) { 
      this.name = name; 
     } 

     public void setSelected(int selected) { 
      this.selected = selected; 
     } 

     public int getSelected() { 
      return selected; 
     } 
    } 


Create Custom adaptor: 

    public class TestAdaptor extends BaseAdapter { 
     Context context; 
     List<Test> list; 
     private LayoutInflater inflater; 
     public TestAdaptor(Context context, List<Test> list) { 
      this.context = context; 
      this.list = list; 
      this.inflater = LayoutInflater.from(context); 
     } 
     @Override 
     public int getCount() { 
      return list.size(); 
     } 
     @Override 
     public test getItem(int position) { 
      return list.get(position); 
     } 
     @Override 
     public long getItemId(int position) { 
      return position; 
     } 
     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      View rowView = convertView; 
      try { 
       if(rowView == null) 
        rowView = this.inflater.inflate(R.layout.volume_list_item, null); 

       if(list.get(position).getSelected() == 1){ 
        rowView.setBackgroundColor(this.context.getResources().getColor(R.color.app_color)); 
       }else{ 
        rowView.setBackgroundColor(this.context.getResources().getColor(R.color.gray)); 
       } 
       rowView.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(final View v) { 
         list.get(position).setSelected(1); 
         notifyDataSetChanged(); 
        } 
       }); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return rowView; 
     } 
    }* 


finally: 

listview.setAdaptor(new TestAdaptor(this, lsit)); 
+0

그들의 setSelected (int sel)이어야합니다 ?? – Yawar

+0

및 내 어댑터가 다른 파일에 있으므로 작동합니까? – Yawar

+0

예! 작동해야합니다, –

관련 문제