(ListFragment)OnItemClickListener究竟应该附加什么?

时间:2015-10-09 06:04:11

标签: android android-listfragment onitemclicklistener event-listener

我正在编写一个应用程序,其主要Activity包含一个fragment-container(在纵向视图中以编程方式添加,在横向视图中从XML布局创建两个Fragment视图)和一个包含List的ListFragment片段查看与一堆项目(通过适配器放入视图)。主Activity实现了ListFragment的回调接口。

我无法使ListFragment的onListItemClick()正常工作。从在线阅读和处理其他示例,似乎应该足够了,但它不起作用。我已经尝试将事件监听器添加到ListView和单个列表项中,但似乎都不起作用;实际上,我不确定那些应该去哪里。我已经尝试将它们放在XML文件中的onCreateView()和'onActivityCreated(). I've also made sure that the Views are set with android:clickable =“true”`中,并且没有其他View对象从ListView项目中窃取焦点。

我在这里缺少什么?

ListFragment活动:

public class IdeaListFragment extends ListFragment {    
    /** various methods and variables **/
  @Override
    // Makes sure the container Activity has implemented the callback interface
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mCallback = (OnIdeaSelectedListener) context;
        } catch(ClassCastException cce) {
            Log.e(TAG, "OnIdeaSelectedListener not implemented: ", cce);
            throw new ClassCastException(context.toString()
                    + " must implement OnIdeaSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int pos, long id) {
        super.onListItemClick(l, v, pos, id);
        mCallback.onIdeaSelected(mBucket.getIdeaAt(pos));
        getListView().setItemChecked(pos, true);
    }
}

主要活动:

public class MainIdeasListViewActivity extends AppCompatActivity
    implements IdeaListFragment.OnIdeaSelectedListener,
    IdeaDetailFragment.OnIdeaUpdatedListener {
    /** various methods and variables **/
    public void onIdeaSelected(IdeaItem idea) {
    IdeaDetailFragment ideaDetailFrag = (IdeaDetailFragment)
            getSupportFragmentManager().findFragmentById(
                    R.id.idea_detail_fragment);

    if(ideaDetailFrag != null)
        ideaDetailFrag.newInstance(idea);
    else {
        IdeaDetailFragment newDetailFrag = new IdeaDetailFragment();
        Bundle args = new Bundle();
        args.putString(UUIDKEY, idea.getUUID().toString());
        newDetailFrag.setArguments(args);
        FragmentTransaction transaction = getSupportFragmentManager()
                                            .beginTransaction();
        transaction.replace(R.id.fragment_container, newDetailFrag);
        transaction.addToBackStack(null);
        transaction.commit();
        }
    }
}

listitem XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
              android:clickable = "true"
              android:longClickable = "true"
              android:orientation = "vertical"
              android:layout_width = "match_parent"
              android:layout_height = "wrap_content"
              android:descendantFocusability="blocksDescendants">

    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:orientation = "horizontal"
        style = "@style/ListItem">

        <ImageView
            android:id = "@id/icon_idea_item_status"
            android:contentDescription = ""
            style = "@style/ListMainIcon" />

        <LinearLayout style = "@style/ListItemBody">
            <TextView
                android:id = "@id/text_idea_title"
                style = "@style/ListItemBodyTitle" />

            <TextView
                android:id = "@id/text_idea_description"
                style = "@style/ListItemBodySubtitle"/>
        </LinearLayout>

        <ImageView
            android:id = "@id/icon_idea_item_submenu"
            android:focusableInTouchMode = "true"
            android:contentDescription = "@string/list_idea_submenu"
            style = "@style/ListSubmenuIcon"
            android:clickable = "true"/>

    </LinearLayout>

    <View style = "@style/divider" />

</LinearLayout>

ListView片段XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
              xmlns:tools = "http://schemas.android.com/tools"
              android:orientation = "vertical"
              android:layout_width = "match_parent"
              android:layout_height = "wrap_content"
              tools:context = ".IdeaListFragment"
              style = "@style/AppMain">

    <ListView
        android:id = "@android:id/list"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:clickable = "true"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

remove android:clickable = "true" from listview fragment XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
              xmlns:tools = "http://schemas.android.com/tools"
              android:orientation = "vertical"
              android:layout_width = "match_parent"
              android:layout_height = "wrap_content"
              tools:context = ".IdeaListFragment"
              style = "@style/AppMain">

    <ListView
        android:id = "@android:id/list"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        />
</LinearLayout>




Add  focusable and clickable false to each child item in listitem XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"

              android:orientation = "vertical"
              android:layout_width = "match_parent"
              android:layout_height = "wrap_content"
              android:descendantFocusability="blocksDescendants">

    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:orientation = "horizontal"
        style = "@style/ListItem"
        android:focusable="false"
        android:clickable="false">

        <ImageView
            android:id = "@id/icon_idea_item_status"
            android:contentDescription = ""
            style = "@style/ListMainIcon" 
            android:focusable="false"
            android:clickable="false"/>

        <LinearLayout style = "@style/ListItemBody"
         android:focusable="false"
         android:clickable="false">
            <TextView
                android:id = "@id/text_idea_title"
                style = "@style/ListItemBodyTitle" 
                 android:focusable="false"
                  android:clickable="false"/>

            <TextView
                android:id = "@id/text_idea_description"
                style = "@style/ListItemBodySubtitle"
                 android:focusable="false"
                   android:clickable="false"/>
        </LinearLayout>

        <ImageView
            android:id = "@id/icon_idea_item_submenu"
            android:focusableInTouchMode = "true"
            android:contentDescription = "@string/list_idea_submenu"
            style = "@style/ListSubmenuIcon"
            android:focusable="false"
            android:clickable="false"/>

    </LinearLayout>

    <View style = "@style/divider" 
    android:focusable="false"
     android:clickable="false"/>

</LinearLayout>