1

목록 조각을 사용하여 항목 목록을 표시하고 있습니다. 항목을 클릭 할 때마다 배경이 변경되고 변경도 있지만 처음으로 어떤 항목을 클릭해도 그 항목의 배경에 변화가 없습니다. 제발 도와주세요. 내 코드는 여기목록 조각에서 처음으로 목록 항목을 클릭해도 항목의 배경이 변경되지 않음

조각 클래스입니다 : -

public class Categoryfragment extends ListFragment 

{ 
    DBhelper dh; 
    Cursor cursor; 
     @Override 
    public void onCreate(Bundle savedInstanceState) 
{  
// TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
     dh = new DBhelper(getActivity()); 
    refreshadpater(); 
} 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) 
    { 
    super.onActivityCreated(savedInstanceState); 
    } 

    public void refreshadpater() 
    { 
      dh = new DBhelper(getActivity()); 
      cursor= dh.fetchAllcategories(); 
     String []columns= {"category_name"}; 
     int []textid = {R.id.text1}; 
     SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.list, cursor, columns, textid); 
     setListAdapter(adapter); 

} 


    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) 
    { 

     getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     LinearLayout ii=(LinearLayout)getActivity().findViewById(R.id.lin); 
     ii.setBackgroundResource(R.drawable.item_selector); 

      int id1 =(int)id ; 

     Subcategoryfragment fragment = (Subcategoryfragment) getFragmentManager().findFragmentById(R.id.secondFragment); 
     TextView tv = (TextView)getActivity().findViewById(R.id.sub); 

     tv.setText("Sub-cat"); 
     if (fragment != null && fragment.isInLayout()) 
     {    
      fragment.refreshadpater1(id1); 

// ii.setBackgroundResource (R.drawable.item_selector);

 } 
    } 

}

list.xml

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

    <TextView android:id="@+id/text1" 

     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:layout_marginTop="10dp" 
     android:layout_marginLeft="10dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:minHeight="?android:attr/listPreferredItemHeight" 

     /> 

item_selector.xml 


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



    <item android:state_activated="true" > 
    <shape > 
     <solid android:color="#FF666666" /> 
    </shape> 
</item> 
<item android:state_pressed="true"> 
     <shape> 
     <solid android:color="#FF666666" /> 
    </shape> 
    </item> 


</selector> 

답변

1

제 터치주고 포커스 클릭으로서 제 2 레지스터. 첫 번째 터치가 클릭으로 등록되도록 포커스를 비활성화해야합니다. 이 트릭을 당신을 위해해야합니다 :

android:focusable="false" 
android:focusableInTouchMode="false"