如何通过单击主列表视图列表项从一个主列表视图片段移动到另一个不同的列表视图片段

how to move from one main listview fragment to other different listview fragments by clicking main listview's list items

我正在尝试通过单击主列表视图上的不同项目从我的主列表视图片段调用另一个不同的列表视图片段。

为此,我编写了以下代码:

我的主要列表视图片段ShopFragment.java: (从这个列表项我想通过点击不同的项目来调用另一个片段)

package fragments.h.safmical;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import safmical.h.R;

/**
 * Created by hadvani on 4/13/2017.
 */

public class ShopFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View rootview= inflater.inflate(R.layout.fragment_shop,container,false);
        String[] list1={"x","y","z"};
        ListView listView= (ListView) rootview.findViewById(R.id.mainlist);
        ArrayAdapter<String> listviewadapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list1);
        listView.setAdapter(listviewadapter);
        FragmentManager fm =getFragmentManager();
        listView.setOnClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==0){

                       fm.beginTransaction().replace(R.id.content_frame,new ListOneFragment()).commit();
                }
                if(position==1){
                    fm.beginTransaction().replace(R.id.content_frame,new ListTwoFragment()).commit();
                }
                if(position==2){fm.beginTransaction().replace(R.id.content_frame,new ListThreeFragment()).commit();}
            }
        });
        return rootview;
    }
}

xml 主列表视图片段的文件 fragment_shop.xml:

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

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mainlist"/>
</FrameLayout>

ListOneFragment.java:

package fragments.h.safmical;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import safmical.h.R;

/**
 * Created by hadvani on 4/14/2017.
 */

public class ListOneFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.fragment_listone, container, false);
        String[] list1 = {"a", "b", "c"};
        ListView listView = (ListView) rootview.findViewById(R.id.list1);
        ArrayAdapter<String> listviewadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list1);
        listView.setAdapter(listviewadapter);
        return rootview;
    }
}

fragment_listone.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list1">

</ListView>
</FrameLayout>

我用同样的方法实现了 ListTwoFragment.java , ListThreeFragment.java , fragment_listtwo.xmlfragment_listthree.xml.

但它不起作用。 Gradle 构建显示以下错误:

Error:(30, 17) error: no suitable method found for setOnClickListener(<anonymous OnItemClickListener>)
method View.setOnClickListener(OnClickListener) is not applicable
(argument mismatch; <anonymous OnItemClickListener> cannot be converted to OnClickListener)
method AdapterView.setOnClickListener(OnClickListener) is not applicable
(argument mismatch; <anonymous OnItemClickListener> cannot be converted to OnClickListener)
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

我应该做任何更正还是有什么不同的方法可以做到这一点? 请帮我解决这个问题

你需要改变

listView.setOnClickListener(new AdapterView.OnItemClickListener() {

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

发生的事情是它正在尝试 OnClickListener 而不是 OnItemClickListener

您不能将 OnClickListener 用于 ListView。请改用 OnItemClickListener,记录在此处: https://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)