主类是
public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener, MyInterface {
ViewPager vp;
TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.mViewpager_ID);
this.addPages();
tabLayout = (TabLayout) findViewById(R.id.mTab_ID);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(vp);
tabLayout.setOnTabSelectedListener(this);
}
private void addPages() {
MyPagerAdapter pagerAdapter = new MyPagerAdapter(this.getSupportFragmentManager());
pagerAdapter.addFragment(new Fragment1());
pagerAdapter.addFragment(new Fragment2());
pagerAdapter.addFragment(new Fragment3());
vp.setAdapter(pagerAdapter);
}
public void onTabSelected(TabLayout.Tab tab) {
vp.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
@Override
public String setOnDataChangedListener(String Text) {
return Text;
}
@Override
public void onFragmentChanged(int value) {
if (value == 0) {
vp.setCurrentItem(value);
}
if (value == 1) {
vp.setCurrentItem(value);
}
if (value == 2) {
vp.setCurrentItem(value);
}
}}
Fragment1是:
public class Fragment1 extends Fragment {
private MyInterface obj;
String a;
Context c;
@Override
public void onAttach(Context context) {
super.onAttach(context);
obj = (MyInterface) context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.activity_fragment1,container,false);
EditText edttext=(EditText)rootView.findViewById(R.id.edttxt);
final String data=edttext.getText().toString();
a="testing_communication";
Button btn=(Button)rootView.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
obj.setOnDataChangedListener(data);
obj.onFragmentChanged(1);
}
});
return rootView;
}
@Override
public String toString() {
String title="Fragment:1";
return title;}}
Fragment2是..
public class Fragment2 extends Fragment implements MyInterface {
@Nullable
TextView tv;
String text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View bootView2=inflater.inflate(R.layout.activity_fragment2,container,false);
tv = (TextView) bootView2.findViewById(R.id.textv);
setOnDataChangedListener(text);
tv.setText(text);
return bootView2;
}
@Override
public String toString() {
String title="Fragment:2";
return title;
}
@Override
public String setOnDataChangedListener(String Text) {
return Text;
}
@Override
public void onFragmentChanged(int value) {
}
}
接口是:
public interface MyInterface {
String setOnDataChangedListener(String Text);
void onFragmentChanged(int value);}
Pageadabter是:
public class MyPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments=new ArrayList<>();
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
public void addFragment(Fragment f)
{
fragments.add(f);
}
@Override
public CharSequence getPageTitle(int position) {
String title=fragments.get(position).toString();
return title.toString();
}}
在按钮上单击fragment1,我可以在fragment2处移动,但不能随数据一起移动。它似乎在onclicklistener主体obj.onFragmentChanged(1)下的fragment1中;工作正常。但是obj.setOnDataChangedListener(data。);不管用.. 如果有人可以提供帮助,我将非常感谢您。
答案 0 :(得分:1)
这里有很多问题。造成此问题的两个原因主要是
首先,在Fragment1中,将上下文转换为MyInterface并使用它来设置数据。在这种情况下,上下文将是包含Fragment1而不是Fragment2的活动
第二,您的setDataChangedListener基本上不执行任何操作。它接收一个String,然后返回相同的东西,而无需将其存储在任何地方。
您需要Fragment2来实现setDataChangedListener,接收一个String,然后将其存储在类变量中。
答案 1 :(得分:0)
您真的要使用界面共享数据吗?
您可以使用Bundle
或MainActivity
的公共字段来共享数据。
答案 2 :(得分:0)