【问题标题】:how to move from one main listview fragment to other different listview fragments by clicking main listview's list items如何通过单击主列表视图列表项从一个主列表视图片段移动到另一个不同的列表视图片段
【发布时间】:2017-04-13 20:46:28
【问题描述】:

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

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

我的主要列表视图片段 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;
    }
}

主listview片段的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.javaListThreeFragment.javafragment_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.

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

【问题讨论】:

    标签: android listview android-fragments android-intent android-arrayadapter


    【解决方案1】:

    你需要改变

    listView.setOnClickListener(new AdapterView.OnItemClickListener() {
    

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    

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

    【讨论】:

    • 是的,我已经更改了它,然后我将 fm 声明为 final 。所以错误解决了。谢谢
    【解决方案2】:

    【讨论】:

    • 是的,我已经更改了它,然后我将 fm 声明为 final 。所以错误解决了。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 2021-04-05
    相关资源
    最近更新 更多