1. 程式人生 > >Android------------fragment資料傳遞

Android------------fragment資料傳遞

一、activity向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的物件,和myListner = (MyListner) getActivity()方法相近

     

 

三、fragment和fragment的之間的數值傳遞