Fragment的创建方法步骤(简单的Fragment)
Fragment的方法解读
1.在MainActivity中继承FragmentActivity
2.创建一个java类继承Fragment重写里面的方法onCreatView(),通过inflate的inflate方法找到创建的布局
3.在开始的布局中添加一个FramLayout和一个RadioGroup放到布局中的最下面,就先qq上的布局一样,设置drawable或者background
4.在MainActivity中设置点击事件
5.声明一个FragmentManager。然后调用beginTransaction()方法生成一个FragmentTransaction对象,利用该对象hide、add、replace创建Fragment对象
6.最后commit一下
7.代码实现:
XML
<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" tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frame_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:gravity="center">
<RadioButton
android:id="@+id/radiobutton_smg"
style="@style/CheckedStyle"
android:drawableTop="@drawable/conversation_checked"/>
<RadioButton
android:id="@+id/radiobutton_link"
style="@style/CheckedStyle"
android:drawableTop="@drawable/contact_checked"/>
<RadioButton
android:id="@+id/radiobutton_dynamic"
style="@style/CheckedStyle"
android:drawableTop="@drawable/plugin_checked"/>
</RadioGroup>
</RelativeLayout>
Java类
public class FragmentExample extends Fragment {
private Button mBtnExample;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_example,null);
mBtnExample= (Button) view.findViewById(R.id.button_example);
mBtnExample.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "点击按钮", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
MainAcitvity
package com.my.fragmenttest;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import com.my.fragmenttest.fragment.FragmentExample;
import com.my.fragmenttest.fragment.FragmentExampleThree;
import com.my.fragmenttest.fragment.FragmentExampleTwo;
public class MainActivity extends FragmentActivity implements View.OnClickListener{
private FrameLayout mFrameLayoutFragment;
private Button mButtonOne;
private Button mButtonTwo;
private Button mButtonThree;
private FragmentExample mFragmentExample;
private FragmentExampleTwo mFragmentExampleTwo;
private FragmentExampleThree mFragmentExampleThree;
private FragmentManager mFragmentManager;
private android.support.v4.app.FragmentTransaction transaction;
private RadioButton mRadioButtonSmg;
private RadioButton mRadioButtonLink;
private RadioButton mRadioButtonDynamic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFrameLayoutFragment= (FrameLayout) findViewById(R.id.frame_fragment);
// mButtonOne= (Button) findViewById(R.id.button_fragment_one);
// mButtonTwo= (Button) findViewById(R.id.button_fragment_two);
// mButtonThree= (Button) findViewById(R.id.button_fragment_three);
// mButtonOne.setOnClickListener(this);
// mButtonTwo.setOnClickListener(this);
// mButtonThree.setOnClickListener(this);
mFragmentManager=getSupportFragmentManager();
mFragmentExample=new FragmentExample();
mFragmentExampleTwo=new FragmentExampleTwo();
mFragmentExampleThree=new FragmentExampleThree();
transaction=mFragmentManager.beginTransaction();
transaction.add(R.id.frame_fragment,mFragmentExample);
transaction.add(R.id.frame_fragment,mFragmentExampleTwo);
transaction.add(R.id.frame_fragment,mFragmentExampleThree);
transaction.hide(mFragmentExample);
transaction.hide(mFragmentExampleTwo);
transaction.commit();
mRadioButtonSmg= (RadioButton) findViewById(R.id.radiobutton_smg);
mRadioButtonLink= (RadioButton) findViewById(R.id.radiobutton_link);
mRadioButtonDynamic= (RadioButton) findViewById(R.id.radiobutton_dynamic);
mRadioButtonSmg.setOnClickListener(this);
mRadioButtonLink.setOnClickListener(this);
mRadioButtonDynamic.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.radiobutton_smg:
transaction=mFragmentManager.beginTransaction();
transaction.hide(mFragmentExampleTwo);
transaction.hide(mFragmentExampleThree);
transaction.show(mFragmentExample);
transaction.commit();
break;
case R.id.radiobutton_link:
transaction=mFragmentManager.beginTransaction();
String text=mFragmentExampleThree.setText();
mFragmentExampleTwo.getText(text);
transaction.hide(mFragmentExample);
transaction.hide(mFragmentExampleThree);
transaction.show(mFragmentExampleTwo);
transaction.commit();
break;
case R.id.radiobutton_dynamic:
transaction=mFragmentManager.beginTransaction();
transaction.hide(mFragmentExample);
transaction.hide(mFragmentExampleTwo);
transaction.show(mFragmentExampleThree);
transaction.commit();
break;
default:
break;
}
}
}