从android中的Main活动调用Fragment的函数但不起作用

时间:2018-04-19 16:43:31

标签: android function android-fragments android-activity

实际上我想从Main活动调用特定ClickListner上的片段函数但不能正常工作。 这是我的片段xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipelayoutm"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/mThirdRecyclerview"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            class="android.support.v7.widget.RecyclerView"/>
    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>

这是我在ClickListner之后的主要活动java代码:

ThirdFragment fragment = (ThirdFragment) getFragmentManager().findFragmentById(R.id.swipelayoutm);
                                fragment.SearchCategoresFunction(text);

这是我的片段功能:

 public void SearchCategoresFunction(final String searchTxt){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dataStoredArrayList.clear();
                myRecyclerAdapter.notifyDataSetChanged();
              SecondParseDataClass  mparser = new SecondParseDataClass(getContext());
                mparser.setOnDataRetrievalCallback(new OnDataRetrievalCallback() {
                    @Override
                    public void onDataRetrieval(ArrayList<DataStored> dataSet) {
                        dataStoredArrayList.addAll(dataSet);
                        myRecyclerAdapter.notifyDataSetChanged();
                    }
                });
                mparser.execute("http://192.168.3.10/datafetchwithsubcatego.php", "3",searchTxt);

            }
        }, 0);
    }

这是My Main Activity xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"
        >
        <ExpandableListView
            android:id="@+id/lvExp"
            android:layout_marginTop="390dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:divider="@color/colorPrimary"
            android:layout_width="match_parent"/>

    </android.support.design.widget.NavigationView>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fitsSystemWindows="true"
        app:itemTextColor="@color/black">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include layout="@layout/practicefilterdesign" />
    </LinearLayout>
    </android.support.design.widget.NavigationView>
    <!--app:menu="@menu/activity_main_drawer"-->
</android.support.v4.widget.DrawerLayout>

请指导我如何删除此错误.............

1 个答案:

答案 0 :(得分:0)

为此,我可以推荐EventBus click here

只需创建一个包含数据的类,

public static class ClickEvent { /* Additional fields if needed */ }

在你的片段上添加

private onClickFromActivity(){ 
//write click operation here
}

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(ClickEvent event) {
   onClickFromActivity()
};

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
}

如果您想触发片段方法,请在您的活动代码

上写下此内容
EventBus.getDefault().post(new ClickEvent());

这是在android中进行互通的最佳方式