使用接口将数据从Activity传递到Fragment(Passing data From Activity to Fragment using interface)

其他开发 IT屋
百度翻译此文   有道翻译此文
问 题

I want to pass data from activity to bundle using interface no bundle. Please have a look at the code below:

Interface:

public interface FragmentCommunicator {

   public void passData(String name);
}

MainActivity

public class MainActivity extends AppCompatActivity{

    FragmentCommunicator fragmentCommunicator;
    private Fragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b = (Button) findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragment= new Fragment();
                fragmentCommunicator = (FragmentCommunicator) getApplication();
                fragmentCommunicator.passData("hello");
                getSupportFragmentManager().beginTransaction().replace(R.id.container ,fragment).commit();

            }
        });

    }
}

Fragment

public class Fragment extends android.support.v4.app.Fragment implements FragmentCommunicator {

    FragmentCommunicator communicator;
    Context c;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment, null);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.c = context;

    }
    @Override
    public void passData(String name) {
        Toast.makeText(c, name, Toast.LENGTH_SHORT).show();
    }
}

I just want to pass some string when I click on button to launch fragment and when fragment is launched should show the toast containing that string.. Please help any help would be appreciated.

解决方案

Write this line of code after onCreate method.

public void passVal(FragmentCommunicator fragmentCommunicator) {
    this.fragmentCommunicator = fragmentCommunicator;
}

Somthing like this

public class MainActivity extends AppCompatActivity{

FragmentCommunicator fragmentCommunicator;
private Fragment fragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b = (Button) findViewById(R.id.button);

    fragment= new Fragment();
    fragmentCommunicator = (FragmentCommunicator) getApplication();
    //fragmentCommunicator.passData("hello");
    getSupportFragmentManager().beginTransaction().replace(R.id.container ,fragment).commit();

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            fragmentCommunicator.passData("Hello");

        }
    });

  }

   //Here is new method
   public void passVal(FragmentCommunicator fragmentCommunicator) {
       this.fragmentCommunicator = fragmentCommunicator;

   }
}

Then write this line of code into onCreateView() of your fragment. Something like this

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup  container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, null);

((MainActivity) getActivity()).passVal(new FragmentCommunicator() {
       @Override
       public void passData(String name) {
           Toast.makeText(c, name, Toast.LENGTH_SHORT).show();
       }
   });
  return view;
}

Note: no need to implement FragmentCommunicator interface in your fragment. Hope it work. It work for me. I have tested

本文地址:IT屋 » Passing data From Activity to Fragment using interface

问 题

我想使用interface no bundle将数据从活动传递到bundle。
请查看以下代码:



接口:



  public interface FragmentCommunicator {

public void passData(String name);
}


MainActivity



 公共类MainActivity扩展AppCompatActivity {

FragmentCommunicator fragmentCommunicator;
私有片段片段;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮b =(按钮)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
fragment = new Fragment();
fragmentCommunicator = (FragmentCommunicator)getApplication();
fragmentCommunicator.passData(“hello”);
getSupportFragmentManager()。beginTransaction()。replace(R.id.container,fragment).commit();

}
});

}
}


片段



  public class Fragment extends android.support.v4.app.Fragment implements FragmentCommunicator {

FragmentCommunicator communicator;
背景c;

@Nullable
@Override
public查看onCreateView(LayoutInflater inflater,@ Nullable ViewGroup容器,@ Nullable Bundle savedInstanceState){
查看视图= inflater.inflate(R .layout.fragment,null);

返回视图;
}

@Override
public void onViewCreated(View view,@ Nullable Bundle savedInstanceState){
super.onViewCreated(view,savedInstanceState);

}

@Override
public void onAttach(Context context){
super.onAttach(context);
this.c = context;

}
@Override
public void passData(String name){
Toast.makeText(c,name,Toast.LENGTH_SHORT).show();
}
}


我只想在点击时传递一些字符串按钮启动片段,当片段启动时应显示包含该字符串的吐司..
请帮助
任何帮助将不胜感激。


解决方案

在onCreate方法之后写下这行代码。



  public void passVal(FragmentCommunicator fragmentCommunicator){
this.fragmentCommunicator = fragmentCommunicator;
}


这样的事情



 公共类MainActivity扩展AppCompatActivity {

FragmentCommunicator fragmentCommunicator;
私有片段片段;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮b =(按钮)findViewById(R.id.button);

fragment = new Fragment();
fragmentCommunicator =(FragmentCommunicator)getApplication();
//fragmentCommunicator.passData(\"hello“);
getSupportFragmentManager()。beginTransaction()。replace(R.id.container,fragment).commit();

b.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){

fragmentCommunicator.passData(“您好“);

}
});

}

//这是新方法
public void passVal(FragmentCommunicator fragmentCommunicator){
this.fragmentCommunicator = fragmentCommunicator;

}
}


然后将这行代码写入片段的onCreateView()。这样的东西



  @Override 
public View onCreateView(LayoutInflater inflater,@ Nullable ViewGroup容器,@ Nullable Bundle savedInstanceState) {
查看视图= inflater.inflate(R.layout.fragment,null);

((MainActivity)getActivity())。passVal(new FragmentCommunicator(){
@Override
public void passData(String name){
Toast.makeText( c,name,Toast.LENGTH_SHORT)。show();
}
});
返回视图;
}


注意:无需在片段中实现FragmentCommunicator接口。希望它有效。它对我有用。我已经测试了


本文地址:IT屋 » 使用接口将数据从Activity传递到Fragment

官方微信
扫一扫关注IT屋
微信公众号搜索 “ IT屋 ” ,选择关注
与百万开发者在一起
}