我有一个名为 MainActivity 的活动和一个名为 Fragment 的片段!
片段位于 MainActivity
中MainActivity onCreate代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
pager = (ViewPager) findViewById(R.id.pager);
pager.setOffscreenPageLimit(0);
pager.setAdapter(adapter);
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(false);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
tabs.setViewPager(pager);
/*im using appcompat_v7 and above code for material design sliding Tabs*/
/*code for pass data to Fragment*/
Fragment Fragment =new Fragment();
Bundle bundle=new Bundle();
bundle.putString("name", "myname");
Fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.pager, Fragment).commit();
}
片段 onCreateView代码:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1,container,false);
Bundle bundle = getArguments();
if (bundle != null){
name= bundle.getString("name");
Toast.makeText(getActivity(), name+"", 1000).show();
/*change UI*/
TextView tv= (TextView) v.findViewById(R.id.tv);
tv.setText(name);
}
return v;
}
但问题出在这里!我不知道为什么电视在屏幕上显示什么,而Toast返回名称,不知道为什么下次当我收到电视文本时,它显示该名称与吐司!
这里发生了什么?有人帮我 PLZ
修改
tab_1 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:background="#e3dbdb"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@_@"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>