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

第一觸摸是給焦點,所述第二寄存器爲點擊。您需要禁用焦點,以便將第一次觸摸註冊爲點擊。這應該對你有用:

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