Fragment高級
Fragment回退棧
adioGroupId.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.person_id:
manager = getSupportFragmentManager();
fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.frame_layout_id,new FristFragment());
//入棧
fragmentTransaction.addToBackStack("11");
fragmentTransaction.commit();
break;
case R.id.message_id:
manager = getSupportFragmentManager();
//出棧
manager.popBackStack();
SelectActivity.this.fragmentTransaction = manager.beginTransaction();
SelectActivity.this.fragmentTransaction.replace(R.id.frame_layout_id,new SecondFragment());
SelectActivity.this.fragmentTransaction.commit();
break;
}
}
});
}
回退棧的意義在於點擊back的時候,不會退出activity,而會返回到之前的fragment界面
Fragment傳值
之前我們寫過關於Activity之間的值傳遞,在fragment中我們也有大量的需求需要值在每個fragment,activity之間傳遞
Activity給Fragment傳遞值
完成activity部分
java代碼:
class MainActivity extends AppCompatActivity {
private EditText etInput;
private Button btnSend;
private FragmentManager manager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etInput = (EditText) findViewById(R.id.et_input);
btnSend = (Button) findViewById(R.id.btn_send);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//獲取到值
String string = etInput.getText().toString();
BlankFragment blankFragment = new BlankFragment();
//創建bundle對象
Bundle bundle = new Bundle();
bundle.putString("val",string);
//給fragment賦值
blankFragment.setArguments(bundle);
//給fragment對象賦值
manager = getSupportFragmentManager();
fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(R.id.show,blankFragment);
fragmentTransaction.commit();
}
});
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:id="@+id/btn_send"
android:text="發送"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
接下來需要完成Fragment
java代碼:
public class BlankFragment extends Fragment {
private TextView tvShow;
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank, container, false);
tvShow = view.findViewById(R.id.tv_show);
Bundle arguments = getArguments();
if (arguments!=null){
String val = arguments.getString("val");
tvShow.setText(val);
}
return view;
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlankFragment">
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
Fragment給Activity傳遞值
第一種:
在Activity中調用getFragmentManager()得到fragmentManager,,調用findFragmentByTag(tag)或者通過findFragmentById(id)
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(tag);
第二種:
通過回調的方式,定義一個接口(可以在Fragment類中定義),接口中有一個空的方法,在fragment中需要的時候調用接口的方法,值可以作爲參數放在這個方法中,然後讓Activity實現這個接口,必然會重寫這個方法,這樣值就傳到了Activity中.
還是先上代碼:
Activity部分代碼
java:
public class Main2Activity extends AppCompatActivity implements MyFragment.MyListnenr {
private TextView textShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textShow = (TextView) findViewById(R.id.text_show);
}
@Override
public void sendMessage(String s) {
textShow.setText(s);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main2Activity">
<TextView
android:id="@+id/text_show"
android:layout_width="match_parent"
android:layout_height="50dp" />
<!-- 靜態加載fragment -->
<fragment
android:id="@+id/fg"
android:name="com.example.highday5_2_25.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"></fragment>
</LinearLayout>
fragment部分代碼
java:
public class MyFragment extends Fragment {
private EditText etIn;
private Button btn;
//聲明成員屬性
private MyListnenr myListnenr;
@Override
public void onAttach(@NonNull Context context) {
myListnenr = (MyListnenr) getActivity();
super.onAttach(context);
}
public MyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
etIn = (EditText) view.findViewById(R.id.et_in);
btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string = etIn.getText().toString();
if (string!=null){
//發送數據到Activity
myListnenr.sendMessage(string);
}
}
});
return view;
}
//自定義接口
public interface MyListnenr{
void sendMessage(String s);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyFragment">
<EditText
android:id="@+id/et_in"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:id="@+id/btn"
android:layout_marginTop="50dp"
android:text="F發送給A"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
*這裏我們用到了一種開發技巧叫接口回調
接口回調
首先需要用戶自定義一個接口,該接口可以是隻爲這一個類提供,也可以被其它類去繼承
//自定義接口
public interface MyListnenr{
void sendMessage(String s);
}
我們希望使用這個接口去完成我們的傳值所以要完成該接口的初始化
//聲明成員屬性
private MyListnenr myListnenr;
@Override
public void onAttach(@NonNull Context context) {
//拿到與當前fragment關聯的Activity.
myListnenr = (MyListnenr) getActivity();
super.onAttach(context);
}
後面我們需要去實現這個接口
public class Main2Activity extends AppCompatActivity implements MyFragment.MyListnenr
重新方法
@Override
public void sendMessage(String s) {
textShow.setText(s);
}
這就是最基本的接口回調實現
fragment 給 fragment 傳值
首先還是先寫Activity佈局文件
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".Main3Activity">
<fragment
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/left"
android:name="com.example.highday5_2_25.LeftFragment"/>
<fragment
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/right"
android:name="com.example.highday5_2_25.RightFragment"/>
</LinearLayout>
接下來需要兩個fragment完成傳值
LeftFragment:
java:
public class LeftFragment extends Fragment {
private EditText leftInput;
private Button leftBtn;
public LeftFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_left, container, false);
leftInput = (EditText) view.findViewById(R.id.left_input);
leftBtn = (Button) view.findViewById(R.id.left_btn);
leftBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string = leftInput.getText().toString();
FragmentManager fragmentManager = getFragmentManager();
RightFragment rightFragment = (RightFragment) fragmentManager.findFragmentById(R.id.right);
rightFragment.setRightText(string);
}
});
return view;
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LeftFragment">
<EditText
android:id="@+id/left_input"
android:layout_width="match_parent"
android:layout_height="40dp" />
<Button
android:id="@+id/left_btn"
android:layout_marginTop="40dp"
android:text="發送"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
RightFragment
java:
public class RightFragment extends Fragment {
private TextView rightText;
public RightFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_right, container, false);
rightText = (TextView) view.findViewById(R.id.right_text);
// rightText.setText();
return view;
}
public void setRightText(String s){
rightText.setText(s);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RightFragment">
<TextView
android:id="@+id/right_text"
android:text="Welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
簡單的案例(底部按鈕控制fragment切換)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main2Activity">
<FrameLayout
android:id="@+id/fl_show"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="12"></FrameLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1">
<RadioButton
android:id="@+id/btn_list"
android:layout_width="0dp"
android:onClick="onClick"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:textSize="20sp"
android:checked="true"
android:textColor="@drawable/text_color"
android:text="消息"
/>
<RadioButton
android:id="@+id/btn_2"
android:layout_width="0dp"
android:onClick="onClick"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:textSize="20sp"
android:textColor="@drawable/text_color"
android:text="聯繫人"
/>
<RadioButton
android:id="@+id/btn_3"
android:layout_width="0dp"
android:onClick="onClick"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:textSize="20sp"
android:textColor="@drawable/text_color"
android:text="空間"
/>
</RadioGroup>
</LinearLayout>
private RadioButton btnList;
private RadioButton btn_2;
private RadioButton btn_3;
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btnList = (RadioButton) findViewById(R.id.btn_list);
btnList = (RadioButton) findViewById(R.id.btn_2);
btnList = (RadioButton) findViewById(R.id.btn_3);
//創建fragment對象
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
//添加默認佈局
addFragment(fragment1);
}
private void addFragment(Fragment fragment) {
//創建管理者
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fl_show,fragment);
fragmentTransaction.commit();
}
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_list:
addFragment(fragment1);
break;
case R.id.btn_2:
addFragment(fragment2);
break;
case R.id.btn_3:
addFragment(fragment3);
break;
}
}