简体   繁体   English

我如何使用interface.b共享数据黑白片段,我想在片段2的文本视图中单击打开按钮单击片段1中的编辑文本数据

[英]How can i share data b/w fragments using interface.i want the on button click Edit Text's data in fragment1 to be shown in the text view of fragment2

Main class is 主班是

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 is: 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 is.. 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) {
 }
 }

Interface is: 界面是:

public interface MyInterface {
String setOnDataChangedListener(String Text);
void onFragmentChanged(int value);}

Pageadabter is: 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();
}}

On button click fragment1, I am able to move at fragment2 but I am not able to move with the data.it seems in fragment1 under onclicklistener body obj.onFragmentChanged(1); 在按钮上单击fragment1,我可以移动到fragment2,但是不能移动数据。它似乎在onclicklistener主体obj.onFragmentChanged(1)下的fragment1中; is working fine.but obj.setOnDataChangedListener(data.); 工作正常。但是obj.setOnDataChangedListener(data。); is not working.. If anybody can help, I will be very thankful to you. 无法正常工作..如果有人可以提供帮助,我将非常感谢您。

Many problems here. 这里有很多问题。 The two that are causing this problem are mainly 导致此问题的两个主要原因

First, in Fragment1 you're casting context as MyInterface and using that to set the data. 首先,在Fragment1中,将上下文转换为MyInterface并使用它来设置数据。 Context on that case would be the activity containing Fragment1, not Fragment2 在这种情况下,上下文将是包含Fragment1而不是Fragment2的活动

Second, your setDataChangedListener does basically nothing. 其次,您的setDataChangedListener基本上不执行任何操作。 It recieves a String and then returns the same thing without ever storing it anywhere. 它接收一个String,然后返回相同的东西,而无需将其存储在任何地方。

You need Fragment2 to implement setDataChangedListener, recieve a String and then stores it in a class variable. 您需要Fragment2来实现setDataChangedListener,接收一个String,然后将其存储在类变量中。

Do you really want to share data using interface? 您真的要使用界面共享数据吗? You can share data using Bundle , or using a public field of MainActivity . 您可以使用BundleMainActivity的公共字段共享数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过单击对话框[NOT DIALOG FRAGMENT](由fragment1调用)的按钮从fragment2替换fragment1 - how to replace the fragment1 from fragment2 from a button click of a dialog[NOT DIALOG FRAGMENT] (which is called by fragment1) 如何从 Fragment1 按钮打开 Activity2? 考虑 Fragment1 属于 Activity1 - How do I open Activity2 from Fragment1 button? Considering Fragment1 belongs to Activity1 在片段之间共享变量,单击fragment1中的按钮在fagment2中显示结果 - share variables between fragments, click button in fragment1 show result in fagment2 ClassCastException: Fragment1 不能转换为 Fragment2 - ClassCastException: Fragment1 cannot be cast to Fragment2 我想从 Activity 获取编辑文本数据并显示在 Fragment 中,但它给了我错误 - I want to get edit text data from Activity and and show in Fragment but it give me error 我想要如图所示的轮廓文本字段,未单击edittext或微调器我想要与按钮单击上的视图相同 - I want outlined text field with as shown in picture not clicked on edittext or spinner i want same as it is view on Button click 我想将数据活动发送到片段但从未调用过接口方法 - I want to Send Data Activity to fragment But InterFace Method Never Called 片段,我想解释的MainActivity类型的Fragment1()方法未定义 - Fragments, The method Fragment1() is undefined for the type MainActivity I would like explaining please Android片段-编辑另一个片段的文本 - Android Fragments - Edit Text Of Another Fragment Acivity1 Fragment2 -&gt; Activity2(单击按钮后)-&gt; Activity1 Fragment2 从活动导航到特定片段 - Acivity1 Fragment2 -> Activity2 (after button click) -> Activity1 Fragment2 navigate from activity to specific fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM