Android------------fragment数据传递

Posted zhang11111wei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android------------fragment数据传递相关的知识,希望对你有一定的参考价值。

一、activity向fragment的数值之间的传递

  关键点:fragment.setArguments(bundle);---->activity发出的信息

          Bundle bundle = getArguments();---->在fragment里面去接受信息

  之间传递的关键函数fragemnt(自己创建的fragment去调用).setArgument(bundle) 和getArgument()

  以下有许多注意事项:

  MainActivity.java

public class MainActivity extends AppCompatActivity {

private FragmentManager manager;
private FragmentTransaction transition;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edit_content);
manager = getSupportFragmentManager();
transition = manager.beginTransaction();
transition.add(R.id.line_content, new ResultFragment());
transition.commit();
}

public void SendMessage(View view){
String info = editText.getText().toString().trim();
ResultFragment result = new ResultFragment();
Bundle bundle = new Bundle();
bundle.putString("info",info);
result.setArguments(bundle);

   //即使是全局对象也要重新得到transition的对象,否则会抛出异常,单步调试F7或者F8就可以知道了
manager = getSupportFragmentManager();
transition = manager.beginTransaction();
transition.replace(R.id.line_content,result);
transition.commit();

}
}


ResultFlagment.java
public class ResultFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.countfragment,null);
TextView textView = view.findViewById(R.id.textView);
Bundle bundle = getArguments();

     //一定要对Bundle就行判断,否则会抛出bundle.getString()得不到字符串的异常
if(bundle != null)
{
String info = bundle.getString("info");
textView.setText(info);
}
return view;
}
}


二、fragmeng向activity传数值

     

  
  注意事项:得到MyListner的对象一般在onCreate或者在onAttach()方法里面进行初始化
  ResourceFragment.java  
public class ResoureFragment extends Fragment {
private EditText editText;
private Button button;
private MyListner myListner;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myListner = (MyListner) getActivity();
Log.i("info111", "onCreate-------->myListner=="+myListner);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.resource,null);
button = view.findViewById(R.id.button);
editText = view.findViewById(R.id.edit_content);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString().trim();
myListner.SendMessage(s);
}
});

return view;
}
public interface MyListner{
void SendMessage(String s);
}
}

  MainActivity.java的讲解
public class MainActivity extends AppCompatActivity implements ResoureFragment.MyListner {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.getText);
}

@Override
public void SendMessage(String s) {
textView.setText(s);
}
}

activity_main.xml文件
注意事项:
引用其他的fragment的时候一定加上它的属性ID,否则会报错
<fragment
android:id="@+id/line_exercise"
android:name="com.example.zhang.fragment_activity.ResoureFragment"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="10dp" />

知识点的讲解:java的多态性
1.MainActivity extends AppCompatActivity implements ResoureFragment.MyListner
  MainActivity是AppCompatActivity的子类,同时也是MyListner的子类

2.myListner = (MyListner) getActivity();
  这一步的得到是fragment的当前的类的Activity的窗口,他又MyListner的子类,所以就出现回调函数这样的方法
  举例说明:
  A extent B
  B b = new A();
  其实b这个时候获得是A的对象,因为他也继承ResorceFragment这个抽象方法,和c++的virture很类似。

 

    

三、fragment和fragment的之间的数值传递
      R.id.fragmentrigh和R.id.fragmentleft在xml文件中定义的
           1.获取getFragmentmanager的fragment对象,通过findFragmentById找到对应的对象,然后调用它写方法
RightFragment rightFragment = (RightFragment) getFragmentManager().findFragmentById(R.id.fragmentright);
rightFragment.setView(s);

2.获取getFragmentmanager的fragment对象,通过findFragmentById找到对应的对象,
         然后再找到对应的view就可以进行findViewById
            View rightFragment1 = getFragmentManager().findFragmentById(R.id.fragmentright).getView();
TextView textView =rightFragment1.findViewById(R.id.textView);
textView.setText(s);

3.通过得到getAvtivity的对象,调用findViewById的方法来找到控件
TextView textView = getActivity().findViewById(R.id.textView);
textView.setText(s);


四、DialogFragment的使用
  好处:他不会随着activity的销毁而销毁(切换横竖屏的时候会用的)
  



  


















以上是关于Android------------fragment数据传递的主要内容,如果未能解决你的问题,请参考以下文章