通过从片段主要活动字符串中viewpager片段活动 [英] pass string from fragment main activity to fragments activity in viewpager
问题描述
我想传递一个字符串从片段活动(主)的所有片段(子),可能是这幅画能解释一下正是我想做的事情
i wanna pass a string to all fragment(child) from fragment activity (main), may be this picture can explain what exactly what i want to do
https://dl.dropboxusercontent.com/u/57465028/SC20140205- 163325.png
所以,从上面的图片...我想妙传的EditText字符串由preSS一个按钮,在viewpager所有活动....我怎么能做到这一点?
so, from above picture...i wanna pass a string from edittext by press a button to all activity in viewpager....how could i do that?
我试图遵循这一code http://stackoverflow.com/a/12739968/2003393 但它无法解决我的问题。
i tried to follow this code http://stackoverflow.com/a/12739968/2003393 but it can't solved my problem..
请帮我...我卡
感谢提前。
下面是我从片段活动(MainActivity)code
here is my code from fragment activity (MainActivity)
公共类Swipe_Menu扩展FragmentActivity {
public class Swipe_Menu extends FragmentActivity {
//String KeyWord;
//private static final String KEYWORD = "keyword";
private ViewPager _mViewPager;
private ViewPagerAdapter _adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.swipe_menu_image);
Button Back = (Button)findViewById(R.id.account);
ImageButton Search = (ImageButton)findViewById(R.id.search);
EditText Keyword = (EditText)findViewById(R.id.keyword);
final String KeyWord = Keyword.getText().toString();
/**
* Back button click event
* */
Back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
setUpView();
setTab();
}
protected void sendValueToFragments(String value) {
// it has to be the same name as in the fragment
Intent intent = new Intent("my_package.action.UI_UPDATE");
intent.putExtra("UI_KEY", KeyWord );
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
和这里是我的片段(儿童活动)
and here is my fragment (Child Activity)
public class Store_Swipe extends Fragment {
public static final String ACTION_INTENT = "my_package.action.UI_UPDATE";
String KeyWord;
private TextView kata_keyword;
protected BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(ACTION_INTENT.equals(intent.getAction())) {
String value = intent.getStringExtra("UI_KEY");
updateUIOnReceiverValue(value);
}
}
};
private void updateUIOnReceiverValue(String value) {
// you probably want this:
KeyWord = value;
}
public static Fragment newInstance(Context context) {
Store_Swipe f = new Store_Swipe();
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ACTION_INTENT);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter);
}
@Override
public void onDestroy() {
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
super.onDestroy();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/*Bundle bundle = this.getArguments();
KeyWord = bundle.getString("keyword");*/
View view = inflater.inflate(R.layout.store_swipe, container, false);
init(view);
return view;
}
void init(View view) {
kata_keyword = (TextView) view.findViewById(R.id.keyword);
//ImageView image = (ImageView) view.findViewById(R.id.image_error);
kata_keyword.setText(KeyWord);
}
}
推荐答案
您不必直接访问到你的片段驻留在 ViewPager
,所以你不能引用他们直接。
You don't have access directly to your fragments that reside in ViewPager
so you can't reference them directly.
我在做什么在这种情况下是发送广播消息从活动支离破碎。为此注册 BroadcatReceiver
中的片段(无论是在的onCreate
或 onCreateView
- 你的决定)男,设置该接收器(如自定义操作my_package.actions.internal.BROADCAST_ACTION
),不要忘了注销。接收器的补充方法。
What I am doing in these cases is send a broadcast message from Activity to Fragments. For this reason register a BroadcatReceiver
in the fragment (either in onCreate
or onCreateView
- your decision)m, set a custom action for that receiver (ex. "my_package.actions.internal.BROADCAST_ACTION"
), don't forget to unregister the receiver from complementary method.
当你想从活动发送邮件,创建一个意图与上述动作,添加字符串的额外意图,并发送广播。
When you want to send a message from activity, create an intent with above mentioned action, add the string in intent extra and send the broadcast.
在接收机的的onReceive
(片段内)的方法,得到的意图放慢参数字符串有你有字符串。
In your receiver's onReceive
method (within the fragment), get the String from intent paramter and there you have the string.
有道理?
修改:为了提供一些code,下面是我会做的片段更改:
EDIT: To provide some code, below are the changes that I would make for fragment:
public class Store_Swipe extends Fragment {
public static final String ACTION_INTENT = "my_package.action.UI_UPDATE";
protected BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(ACTION_INTENT.equals(intent.getAction())) {
String value = intent.getStringExtra("UI_KEY");
updateUIOnReceiverValue(value);
}
}
};
private void updateUIOnReceiverValue(String value) {
// you probably want this:
kata_keyword.setText(value);
}
String KeyWord;
private TextView kata_keyword;
public static Fragment newInstance(Context context) {
Store_Swipe f = new Store_Swipe();
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ACTION_INTENT);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter);
}
@Override
public void onDestroy() {
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
super.onDestroy();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle bundle = this.getArguments();
KeyWord = bundle.getString("keyword");
View view = inflater.inflate(R.layout.store_swipe, container, false);
init(view);
return view;
}
void init(View view) {
kata_keyword = (TextView) view.findViewById(R.id.keyword);
ImageView image = (ImageView) view.findViewById(R.id.image_error);
kata_keyword.setText(KeyWord);
}
}
这code我会从活动中,参数是的EditText值:
And this code I would have from activity, the parameter is the value from EditText:
protected void sendValueToFragments(String value) {
// it has to be the same name as in the fragment
Intent intent = new Intent("my_package.action.UI_UPDATE");
intent.putExtra("UI_KEY", value);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
您会调用这个从点击监听器,你会在设定的onCreate
:
You would call this from the click listener that you would set in onCreate
:
findViewById(R.id.button_id).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String valueThatYouWantToSend = null; /// just the value
sendValueToFragments(valueThatYouWantToSend);
}
});
这篇关于通过从片段主要活动字符串中viewpager片段活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!