4月18日Fragment切换
本次作业我主要分为主界面FranmentActivity和两个作为切换的界面FirstFrgmentActivity和SecondFragmentActivity
FragmentActivity的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="com.example.caicm.fragmenttwo.FragmentActivity">
<LinearLayout
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="88dp"
android:onClick="onClick"
android:layout_gravity="center|top"
android:text="@string/button" />
</LinearLayout>
</LinearLayout>
FirstFragment的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.caicm.fragmenttwo.FirstFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/first"
android:textColor="#000000"
android:textSize="26sp" />
</FrameLayout>
SecondFragment的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.caicm.fragmenttwo.SecondFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/second"
android:textColor="#000000"
android:textSize="26sp" />
</FrameLayout>
FirstFranmentActivity中的Java代码为:
package com.example.caicm.fragmenttwo;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import static com.example.caicm.fragmenttwo.R.layout.fragment_first;
/**
* A simple {@link Fragment} subclass.
*/
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first,container,false);
return view;
}
}
SecondFragmentActivity中的Java代码为:
package com.example.caicm.fragmenttwo;
import android.os.Bundle;
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class SecondFragment extends Fragment {
public SecondFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_second,container,false);
return view;
}
}
FragmentActivity中的Java代码:
package com.example.caicm.fragmenttwo;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
public class FragmentActivity extends AppCompatActivity implements View.OnClickListener {
FirstFragment FirstFragment;
SecondFragment SecondFragment;
boolean zhuanhuan = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
FirstFragment = new FirstFragment();
transaction.add(R.id.top, FirstFragment);
transaction.commit();
}
@Override
public void onClick(View view) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
switch (view.getId()) {
case R.id.button:
if (zhuanhuan) {
if (SecondFragment == null) {
SecondFragment = new SecondFragment();
transaction.replace(R.id.top, SecondFragment);
transaction.commit();
zhuanhuan = false;
} else {
transaction.replace(R.id.top, SecondFragment);
transaction.commit();
zhuanhuan = false;
}
} else {
Toast.makeText(this, "This is second fragment", Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.button, FirstFragment);
transaction.commit();
finish();
}
return super.onKeyDown(keyCode, event);
}
}
运行效果为:
这次作业当中感觉很模糊,逻辑方面还是有点乱,而且最后一个要求怎么调试都没有效果,尝试用上上节课教的传值并返回,没有效果,需要设置一个Button,这样不是我想要的效果,也不知道这一节内容讲的快,还是什么原因,就是晕,布局设置可以很容易做出来,但是Java代码逻辑还是搞不清,不过现在知道语句块之间的关系了,希望老师在看作业的时候给我指出最后一个返回First Fragment 这个怎么做的,这个我只会写方法,照着上课您讲的内容往里面套,总觉得少些什么,出现闪退的现象,还有可不可以用上上节课传值并返回来实现。