setOnItemClickListener对片段中的单击不执行任何操作

时间:2019-04-30 13:42:17

标签: android android-fragments android-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>

1 个答案:

答案 0 :(得分:0)

使用

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

代替

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

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