在 viewpager 中传递数据 beetwen 片段

Passing data beetwen fragments in viewpager(在 viewpager 中传递数据 beetwen 片段)
本文介绍了在 viewpager 中传递数据 beetwen 片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I've Viewpager wit 2 fragments: CurrentWeatherFragment and ForecastFragment. I need to pass string from one to another, Iam using interface like below, but I keep getting NullPointerException, the message is not passing propertly...

public class CurrentWeatherFragment extends Fragment {

SendMessage SM

public void onCreateView(...) {
String Message = "Hello"
SM.sendData(Message);
}

interface SendMessage
{
    public  void sendData(String message);
}

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

    try {
        SM = (SendMessage) activity;
    } catch(ClassCastException e) {
        throw  new ClassCastException("Musisz zaimplementowac metode sendData");
    }
}
}

MainActivity.java

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


public class MainActivity extends FragmentActivity implements CurrentWeatherFragment.SendMessage {

ViewPager viewPager;


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

    //setting fragment view pager
    viewPager = (ViewPager)findViewById(R.id.pager);
    PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(pagerAdapter);
}

public void sendData (String message){
    ForecastFragment FF = new ForecastFragment();
    FF.getData(message);
}

}

ForecastFragment.java

public class ForecastFragment extends Fragment {

public View onCreateView(){
TextView txt = (TextView)v.findViewById(R.id.txt_forecast);
}

public void getData(String message){
        txt.setText(message);
}
}

I've used this method succesfully in other app where I've had 2 fragments in one activity and i could call them by ID

public void sendData(String message) {

    SecondFragment f2 = (SecondFragment)getFragmentManager().findFragmentById(R.id.F2);
    f2.getData(message);
}

But here Fragments dont have IDs and I think that message is not passed because i dont use FragmentManager(), but how to find fragment in viewpager without ID, any suggestion/ideas?

解决方案

Although a little hacky what you can do is get the fragment by its tag by using the following code:

String tag = "android:switcher:" + R.id.pager + ":" + index;
Fragment f = getSupportFragmentManager().findFragmentByTag(tag);

Where R.id.pager is the id of the viewpager in your layout and index is the position (as an integer) in the Viewpager Adapter. I can't say this will work forever but it works for me at the moment.

The Alternative i would suggest is using a LocalBroadcastManager and a BroadcastReciver to send data internally between your fragments as although its a little more work it helps get rid of the spaghetti code situation you may end up finding yourself in trying to reference the fragments directly.

这篇关于在 viewpager 中传递数据 beetwen 片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)