服务器之家

服务器之家 > 正文

Android编程使用Fragment界面向下跳转并一级级返回的实现方法

时间:2021-04-08 15:32     来源/作者:_YMW

本文实例讲述了Android编程使用Fragment界面向下跳转并一级级返回的实现方法。分享给大家供大家参考,具体如下:

1.首先贴上项目结构图:

Android编程使用Fragment界面向下跳转并一级级返回的实现方法

2.先添加一个接口文件BackHandledInterface.java,定义一个setSelectedFragment方法用于设置当前加载的Fragment在栈顶,主界面MainActivity须实现此接口,代码如下:

?
1
2
3
4
package com.example.testdemo;
public interface BackHandledInterface {
  public abstract void setSelectedFragment(BackHandledFragment selectedFragment);
}

3.定义一个抽象类BackHandledFragment继承自Fragment,后面跳转的Fragment界面都要继承自BackHandledFragment。抽象类BackHandledFragment中定义一个返回值为boolean类型的onBackPressed方法,用于处理点击返回按键(物理Back键)时的逻辑,若该方法返回false,表示当前Fragment不消费返回事件,而由Fragment所属的FragmentActivity来处理这个事件。代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.example.testdemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
public abstract class BackHandledFragment extends Fragment {
  protected BackHandledInterface mBackHandledInterface;
  /**
   * 所有继承BackHandledFragment的子类都将在这个方法中实现物理Back键按下后的逻辑
   */
  protected abstract boolean onBackPressed();
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (!(getActivity() instanceof BackHandledInterface)) {
      throw new ClassCastException(
          "Hosting Activity must implement BackHandledInterface");
    } else {
      this.mBackHandledInterface = (BackHandledInterface) getActivity();
    }
  }
  @Override
  public void onStart() {
    super.onStart();
    // 告诉FragmentActivity,当前Fragment在栈顶
    mBackHandledInterface.setSelectedFragment(this);
  }
}

4.主界面MainActivity要继承FragmentActivity才能调用getSupportFragmentManager()方法来处理Fragment。MainActivity还需重写onBackPressed方法用来捕捉返回键(Back Key)事件,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.example.testdemo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends FragmentActivity implements
    BackHandledInterface {
  private static MainActivity mInstance;
  private BackHandledFragment mBackHandedFragment;
  private Button btnSecond;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnSecond = (Button) findViewById(R.id.btnSecond);
    btnSecond.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        FirstFragment first = new FirstFragment();
        loadFragment(first);
        btnSecond.setVisibility(View.GONE);
      }
    });
  }
  public static MainActivity getInstance() {
    if (mInstance == null) {
      mInstance = new MainActivity();
    }
    return mInstance;
  }
  public void loadFragment(BackHandledFragment fragment) {
    BackHandledFragment second = fragment;
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.firstFragment, second, "other");
    ft.addToBackStack("tag");
    ft.commit();
  }
  @Override
  public void setSelectedFragment(BackHandledFragment selectedFragment) {
    this.mBackHandedFragment = selectedFragment;
  }
  @Override
  public void onBackPressed() {
    if (mBackHandedFragment == null || !mBackHandedFragment.onBackPressed()) {
      if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
        super.onBackPressed();
      } else {
        if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
          btnSecond.setVisibility(View.VISIBLE);
        }
        getSupportFragmentManager().popBackStack();
      }
    }
  }
}

5.分别添加两个子级Fragment,FirstFragment.java和SecondFragment.java,代码分别如下:

FirstFragment.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.example.testdemo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class FirstFragment extends BackHandledFragment {
  private View myView;
  private Button btnSecond;
  @Override
  public View onCreateView(LayoutInflater inflater,
      @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.fragment_first, null);
    initView();
    return myView;
  }
  private void initView() {
    btnSecond = (Button) myView.findViewById(R.id.btnSecond);
    btnSecond.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        SecondFragment second = new SecondFragment();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.firstFragment, second);
        ft.addToBackStack("tag");
        ft.commit();
      }
    });
  }
  @Override
  protected boolean onBackPressed() {
    return false;
  }
}

SecondFragment.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.example.testdemo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends BackHandledFragment {
  private View mView;
  @Override
  public View onCreateView(LayoutInflater inflater,
      @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.fragment_second, null);
    return mView;
  }
  @Override
  protected boolean onBackPressed() {
    return false;
  }
}

6.三个布局文件代码如下:

activity_main.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<RelativeLayout 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"
  android:orientation="vertical" >
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="FragmentActivity 父界面"
    android:textSize="26sp" />
  <Button
    android:id="@+id/btnSecond"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="跳转到FirstFragment" />
  <FrameLayout
    android:id="@+id/firstFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
  </FrameLayout>
</RelativeLayout>

fragment_first.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#e5e5e5"
  android:orientation="vertical" >
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="FirstFragment"
    android:textColor="#000000"
    android:textSize="26sp" />
  <Button
    android:id="@+id/btnSecond"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="打开SecondFragment" />
</RelativeLayout>

fragment_second.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#e5e5e5"
  android:orientation="vertical" >
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="SecondFragment"
    android:textColor="#000000"
    android:textSize="26sp" />
</RelativeLayout>

7.最后奉上实例链接:

完整实例代码代码点击此处本站下载

希望本文所述对大家Android程序设计有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部