0
回答
Android中使用ViewPager+Fragment中实现Fragment之间通信的问题
科大讯飞通用文字识别100000次/天免费使用。立即申请   


工程组织结构:

1.         ShowFragment.java   配套的布局文件是:fragment_setting.xml

2.         SettingFragment.java  配套的布局文件是:fragment_show.xml

3.         MainActivity.java  配套的布局文件是:activity_main.xml

 

工程的构思和目的:

SettingFragment中有个BUTTON,点击后可以跳转到ShowFragment上,并且传递给ShowFragment一个字符串,这里的传递是在SettingFragment中制作一个接口,MainActivity来实现这个接口,接口中有个方法:onQuery(String szContent),在SettingFragment中传递字符串给方法onQuery(String szContent)

MainActivity中接收该参数后使用

getFragmentManager().findFragmentById(这里是ShowFragmentID)来调用一个ShowFragment中的public方法show(String szContent),从而将SettingFragment传递来的字符串显示到ShowFragment上。

但是由于两个Fragmeng都是new出来的,不能够使用:

getFragmentManager().findFragmentById(R.id.showfragmentID)来实现。

那么我要在MainActivity中如何调用ShowFragment中的show(String szContent)呢。

下面贴出来整个工程的所有源码。

(不知道有没有说清楚)

 

下面是fragment_setting.xml的全部源码:


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


    <TextView
        android:text="这里是settingFragment"
        android:layout_width="fill_parent"
        android:layout_height="44dp" />

    <Button
        android:id="@+id/btn_trs"
        android:text="点我刷新另一个fragment"
        android:layout_width="fill_parent"
        android:layout_height="70dp"
        android:visibility="visible"/>

</LinearLayout>





下面是fragment_show.xml的全部源码:


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

    <TextView
        android:text="这里是showFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <TextView
        android:id="@+id/tv_show"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />
</LinearLayout>



下面是activity_main.xml的全部源码:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">



    <android.support.v4.view.ViewPager
        android:id="@+id/id_viewPager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </android.support.v4.view.ViewPager>
</LinearLayout>





下面是SettingFragment.java的全部源码:

package com.ccsoft.fragmentcommunication;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Created by Administrator on 2015/10/31.
 */
public class SettingFragment extends Fragment {
    private View mView;
    private IAtSettingFragment mOnQueryListener;

    private Button btn_query;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_setting,null);

        initView();
        return mView;
    }

    private void initView() {
        btn_query = (Button) mView.findViewById(R.id.btn_trs);
        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity)getActivity()).setSelected(1);
                mOnQueryListener.onQuery("我是在SettingFragment中就被设置好了");
            }
        });
    }

    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);

        if(!(activity instanceof IAtSettingFragment)){
            throw new IllegalStateException("Acitivty必须实现接口onQueryListener");
        }

        mOnQueryListener = (IAtSettingFragment)activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();

    }


    public interface IAtSettingFragment {
        public void onQuery(String szContent);
    }

}



下面是ShowFragment.java对的全部源码:

package com.ccsoft.fragmentcommunication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by Administrator on 2015/10/31.
 */
public class ShowFragment extends Fragment{
    private View mView;
    private TextView tv_show;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_show,null);
        initView();
        return mView;
    }

    private void initView() {
        tv_show = (TextView) mView.findViewById(R.id.tv_show);
    }

    public void show(String szContent){
        tv_show.setText(szContent);
    }

}




下面是MainActivity.java的全部源码:

package com.ccsoft.fragmentcommunication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends FragmentActivity implements SettingFragment.IAtSettingFragment{

    private ViewPager mViewPager;
    private List<Fragment> mListFragment;
    private FragmentPagerAdapter mAdapter;

    Fragment settingFragment;
    Fragment showFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }



    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.id_viewPager);
        settingFragment = new SettingFragment();
        showFragment = new ShowFragment();

        mListFragment = new ArrayList<Fragment>();
        mListFragment.add(settingFragment);
        mListFragment.add(showFragment);

        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return mListFragment.get(i);
            }

            @Override
            public int getCount() {
                return mListFragment.size();
            }
        };

        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int i) {
                int curItem = mViewPager.getCurrentItem();
                setSelected(curItem);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    public void setSelected(int curItem) {
        mViewPager.setCurrentItem(curItem);
    }


    @Override
    public void onQuery(String szContent) {
        //getFragmentManager().findFragmentById(showFragment.getId())

    }
}



 

 


举报
葬天尘
发帖于3年前 0回/5K+阅
顶部