我无法弄清楚如何在同一活动上托管的两个片段之间共享数据。
目标: 我想从一个微调器的选定位置传输字符串,并从一个选定的列表视图位置将一个图像url字符串从片段A传输到片段B.
尝试: 我在这里阅读了有关此问题的片段文档http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity 并继续创建了以下接口,以便在片段和主机活动之间使用。
public interface OnSelectionListener {
public void OnSelectionListener(String img, String comments );
}
然后我继续在我的片段A的onCreateView方法中实现它,如下所示:
postList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ListData link = data.get(position);
String permalink = link.getComments();
String largeImg = link.getImageUrl();
Fragment newFragment = new DetailsView();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
//pass data to host activity
selectionListener.OnSelectionListener(permalink,largeImg);
}
});
还有onAttach方法
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
selectionListener = (OnSelectionListener)getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement onSelectionListener");
}
}
在主机活动中,我实现了我编写的界面并覆盖了这样的方法:
@Override
public void OnSelectionListener(String img, String comments) {
DetailsView detailsView = new DetailsView();
DetailsView dView = (DetailsView)getSupportFragmentManager().findFragmentByTag(detailsView.getCustomTag());
dView.setInformation(img, comments);
}
在片段B中,我按照以下方式设置“标签”
private String tag;
public void setCustomTag(String tag)
{
this.tag = tag;
}
public String getCustomTag()
{
return tag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setCustomTag("DETAILS_VIEW");
我的想法是,可以通过从主机活动调用此方法将信息传递给Fragment B
void setInformation (String info, String img){
RedditDetailsTask detailsTask = new RedditDetailsTask(null,DetailsView.this);
detailsTask.execute(info);
setDrawable(img);
}
我需要什么: 我想知道如何正确使用标签来实现这一点,我没有在我的xml中声明任何片段id,而是选择在fragment_container中交换片段。
我也不确定这是否是在片段之间传递多个字符串的好方法。我是一名新手程序员,所以我知道我的逻辑可能看起来很尴尬,但我正努力学会做到这一点。如果您有更高级的开发人员可以指出我这样做的正确方向,我将不胜感激。
答案 0 :(得分:1)
您不需要使用标签。看看这个例子。 Activity实现了一个接口,允许您从Fragment1回到Activity,然后Activity将信息转发到Fragment2。
我遗漏了所有关于FragmentManager等的android内容。
interface FragmentListener {
void onTalk(String s1);
}
public class MyActivity extends Activity implements FragmentListener {
Fragment2 fragment2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Find fragment2 and init
}
@Override
public void onTalk(String s1) {
fragment2.onListen(s1);
}
private static class Fragment1 extends Fragment {
private FragmentListener communication;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
communication = (FragmentListener) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// or in an onClick listener
communication.onTalk("blah blah");
}
}
private static class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
public void onListen(String s1) {
Log.d("TADA", s1);
}
}
}
答案 1 :(得分:0)
我的方法是,当您通过OnSelectionListener接口获取活动回调时,我将创建Fragment B对象并为其设置参数,如下所示:
@Override
public void OnSelectionListener(String img, String comments) {
DetailsView detailsView = new DetailsView();
Bundle args=new Bundle();
args.putString("img",img);
args.putString("comments",comments);
detailsView.setArguments(args);
//code here to replace the fragment A with fragment B
}
然后在Fragment B的onCreate方法中,您可以按如下方式检索值:
@Override
public void onCreate(Bundle savedInstanceState) {
Bundle args=getArguments();
String img=args.getString("img");
String comments=args.getString("comments");
//do whatever you want to do with the varaibles
}
答案 2 :(得分:0)
您可以尝试在B片段中创建两个公共静态字符串。
它看起来像这样的
public static String img;
public static String comment;
在将事务转换为片段B之前设置变量
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
SecondFragment.img = new String("imgString"); //Making a new string so incase you change the string in bfragment, the values wont change in here
SecondFragment.comment = new String("comment");
// Commit the transaction
transaction.commit();
然后在onStop()或onDestroy()中 - 取决于你希望变量为空的时间check this - 你将静态变量设置为null,所以它们不占用内存空间
public void onDestroy(){
super.onDestroy();
img = null;
comment = null;
}