Fragment的跳转
2017-04-18 21:41 好名字啊 阅读(182) 评论(0) 收藏 举报第一个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"
android:gravity="center"
tools:context="com.example.jzuo2.MainActivity">
<LinearLayout
android:id="@+id/txt_show"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
<Button
android:id="@+id/btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_show"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:onClick="onClick"/>
<LinearLayout
android:id="@+id/txt_show2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
第二个xml
<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="com.example.jzuo2.FirstFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/txt_first"
android:text="@string/fragment_first"/>
</FrameLayout>
第三个xml
<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"
android:orientation="vertical"
tools:context="com.example.jzuo2.SecondFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/txt_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/fragment_second"/>
</FrameLayout>
主java
public class MainActivity extends AppCompatActivity {
FirstFragment FirstFragment;
SecondFragment SecondFragment;
private boolean qh = true;
private boolean tz = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
FirstFragment = new FirstFragment();
transaction.add(R.id.txt_show,FirstFragment);
transaction.commit();
}
public void onClick(View view){
if(view.getId() == R.id.btn_show){
tz = true;
if(qh) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (SecondFragment == null) {
SecondFragment = new SecondFragment();
transaction.replace(R.id.txt_show2, SecondFragment);
transaction.commit();
qh = false;
} else {
transaction.replace(R.id.txt_show2, SecondFragment);
transaction.commit();
qh = false;
}
}else{
Toast.makeText(this,"This is second fragment",Toast.LENGTH_SHORT).show();
}
}
}
public boolean onKeyDown(int keyCode,KeyEvent event) {
if(event.getKeyCode()==KeyEvent.KEYCODE_BACK&&tz){
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
qh = true;
tz = false;
transaction.replace(R.id.txt_show2,FirstFragment);
transaction.commit();
return false;
} else {
finish();
}
return super.onKeyDown(keyCode,event);
}
}
第二个java与第三个类似
public class FirstFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_first,container,false);
return view;
}
}
浙公网安备 33010602011771号