最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android從Fragment跳轉到其他Activity的簡單實例

 更新時間:2020年02月06日 10:33:21   作者:大鵬學Android  
這篇文章主要介紹了Android從Fragment跳轉到其他Activity的簡單實例,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

為了更好的理解以下內容,我們需要簡單了解一下Fragment的動態(tài)注冊方法

Android——Fragment的靜態(tài)注冊和動態(tài)注冊

為了實現(xiàn)從Fragment跳轉到其他Activity,下面需要創(chuàng)建以下文件:

第一步:簡單編寫布局文件fragment_activity.xml和抽象類TemplateFragmentActivity.java代碼如下:

fragment_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/temp_fragment_activity"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
</FrameLayout>

fragment_activity.xml布局主要用于承載各fragment布局,例如fragment_one.xml和fragment_two.xml。

TemplateFragmentActivity.java

package com.example.myapplication;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public abstract class TemplateFragmentActivity extends AppCompatActivity
{
 private FragmentManager fm;
 private FragmentTransaction ts;
 private Fragment fragment;
 //抽象方法,用于創(chuàng)建Fragment實例
 protected abstract Fragment createFragment();
 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.fragment_activity);
  fm = getSupportFragmentManager();
  ts = fm.beginTransaction();
  if (fragment == null){
   fragment = createFragment();
   ts.add(R.id.temp_fragment_activity,fragment);
   ts.commit();
  }
 }
}

第二步:分別使類FragmentOneActivity和FragmentTwoActivity繼承類TemplateFragmentActivity并實現(xiàn)抽象方法createFragment()

FragmentOneActivity.java

package com.example.myapplication;
import androidx.fragment.app.Fragment;
public class FragmentOneActivity extends TemplateFragmentActivity {
 @Override
 protected Fragment createFragment() {
  return new FragmentOne();
 }
}

FragmentTwoActivity.java與FragmentOneActivity.java類似,不在重復。
第三步:分別編寫fragment_one.xml和fragment_two.xml布局文件并通過編寫FragmentOne.java和FragmentTwo.java綁定對應的布局文件,并實現(xiàn)其具體功能。

fragment_one.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="center"
 android:background="@color/colorPrimaryDark"
 android:orientation="vertical">
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text="點擊下面的按鈕跳轉到FragmentTwoActivity"
  android:textSize="20sp"
  android:textAllCaps="false"
  android:textColor="#F70505">
 </TextView>
 <Button
  android:id="@+id/btn_fm_one"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="跳轉"
  android:textSize="30dp"
  android:layout_marginTop="20dp">
 </Button>
</LinearLayout>

fragment_two.xml與fragment_one.xml類似,不在重復。

FragmentOne.java

package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class FragmentOne extends Fragment {
 private Button mBtnFragmentOne;
 @Nullable
 @Override
 public View onCreateView(@NonNull LayoutInflater inflater,
        @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.fragment_one,container,false);
  mBtnFragmentOne = view.findViewById(R.id.btn_fm_one);
  mBtnFragmentOne.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent intent = new Intent(getActivity(),FragmentTwoActivity.class);
    startActivity(intent);
   }
  });
  return view;
 }
}

Fragment跳轉到Activity與Activity跳轉到Activity方法類似,如下:

Intent intent = new Intent(getActivity(),FragmentTwoActivity.class);
startActivity(intent);

FragmentTwo.java與FragmentOne .java類似,不在重復。

演示·:

總結

以上所述是小編給大家介紹的Android從Fragment跳轉到其他Activity的簡單實例,希望對大家有所幫助!

相關文章

最新評論

镇坪县| 泰安市| 县级市| 蒲城县| 鄂温| 饶平县| 水城县| 扎鲁特旗| 股票| 宁陕县| 宜兰市| 云霄县| 丹棱县| 星子县| 嘉禾县| 湛江市| 古蔺县| 丹凤县| 永胜县| 米易县| 富蕴县| 桑日县| 嘉义县| 镇康县| 绥宁县| 徐闻县| 二连浩特市| 安国市| 威远县| 贺州市| 吉水县| 宁国市| 进贤县| 五寨县| 故城县| 黑水县| 郑州市| 蚌埠市| 和平区| 固始县| 满城县|