传递数据beetwen在viewpager片段 [英] Passing data beetwen fragments in viewpager
问题描述
我已经Viewpager机智2片段: CurrentWeatherFragment
和 ForecastFragment
。我需要从一个传递到另一个字符串,用荫像下面的界面,但我不断收到 NullPointerException异常
,消息不传递propertly ...
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
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
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);
}
}
我用这个方法成功地在其他应用,在那里我在一个活动有2个片段,我可以通过ID叫他们
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);
}
但这里片段不要有标识,我想是不是通过了消息,因为我不使用 FragmentManager()
,但如何找到viewpager片段没有ID,任何建议/想法?
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?
推荐答案
虽然有点哈克你可以做的是使用下面的code。通过它的标签获得的片段:
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);
在哪里R.id.pager是在布局和索引viewpager的ID在Viewpager适配器的位置(整数)。
我不能说这将永远工作,但它的那一刻我的作品。
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.
另类,我会使用一个LocalBroadcastManager和BroadcastReciver因为虽然你的片段之间在内部发送的数据表明其多一点工作,它有助于摆脱你最终可能会发现自己在试图意大利面条code局面直接引用片段。
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.
这篇关于传递数据beetwen在viewpager片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!