setOnItemClickListener没有对点击片段进行任何操作

问题描述 投票:0回答:1

我为listview设置了这个setOnItemClickListener()。但是,当我按下列表项时,它不会执行任何操作。我已经在StackOverflow上完成了所有相同的问题,但没有一个能回答我的问题。但是,当我长按列表项时,上下文菜单显示正常。

我的代码:

public class FragmentText extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final int TEXT_LOADER = 0;
    View textView;
    FloatingActionButton fabText;
    TextAdapter mTextAdapter;
    ListView editorList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    t    extView = inflater.inflate(R.layout.text_fragment, container, false);

        fabText = textView.findViewById(R.id.add_text);
        editorList = (ListView)textView.findViewById(R.id.editor_list);

        fabText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent editorIntent = new Intent(textView.getContext(), TextEditor.class);
                startActivity(editorIntent);
            }
        });

        mTextAdapter = new TextAdapter(textView.getContext(), null);
        editorList.setAdapter(mTextAdapter);
        registerForContextMenu(editorList);

        editorList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();
            }
        });

        getLoaderManager().initLoader(TEXT_LOADER, null, this);

        return textView;
    }

列表视图的xml:

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

    <ListView
        android:id="@+id/editor_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/transparent" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/add_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="20dp"
        android:src="@drawable/text"/>

</RelativeLayout>

列表项的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="3dp"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="2dp"
        android:longClickable="true"
        android:foreground="?android:attr/selectableItemBackground">
        <FrameLayout
            android:background="@color/colorPrimaryDark"
            android:layout_width="3dp"
            android:layout_height="match_parent"/>
        <TextView
            android:id="@+id/editor_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="About panda"
            android:textSize="20sp"
            android:layout_gravity="center_vertical"
            android:paddingLeft="8dp"/>

    </android.support.v7.widget.CardView>
</RelativeLayout>
android android-fragments android-listview
1个回答
0
投票

使用

Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show();

代替

Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();

供参考:https://stackoverflow.com/a/32227421/5348018

© www.soinside.com 2019 - 2024. All rights reserved.