android - 在列表 fragment 中,第一次单击列表项不会更改项目的背景

标签 android background simplecursoradapter listitem android-listfragment

我正在使用列表 fragment 来显示项目列表,我希望这样,每当单击该项目时,它的背景应该改变并且也在改变,但是当第一次点击任何项目时,它们在该项目的背景。请帮助我。我的代码在这里

fragment 类:-

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);

      } 
     }

列表.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>

最佳答案

第一次触摸是给予焦点,第二次是点击。您需要禁用焦点,以便第一次触摸注册为点击。这应该可以为您解决问题:

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

关于android - 在列表 fragment 中,第一次单击列表项不会更改项目的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11649037/

相关文章:

javascript - 使用纬度和经度从链接启动谷歌地图方向的网络应用程序

android - 使用MVVM在RecyclerView的网格和线性之间切换的更好方法是什么?

Android 市场崩溃报告未报告 bugsense

android - ActionMode 背景不适用于 set support ActionBar with Toolbar

html - 如何在容器周围的 twitter bootstrap 中制作背景图像

android - 应用程序已在 android 模拟器中停止

html - Laravel:背景图片不会出现

android - 在 Android 中为 SimpleCursorAdapter 添加日期分隔符

android - 我如何在 Android 中使用 this.getSelectedItemId()

java - 无法从 Android 中的数据库填充 listView 中的数据