Android碎片Fragment的点击切换

来源:互联网 发布:淘宝店推广引流的技巧 编辑:程序博客网 时间:2024/05/17 00:08

Fragment碎片的简单使用 

1.MainActivity

点击切换Fragment碎片

package com.example.fragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.ActionBarActivity;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends ActionBarActivity  implements OnClickListener{//知识点1:Fragment 碎片//核心 Activity与view的结合体//既可以作逻辑处理等价于Activity//又可以作view  试图控件Button button1,button2;//使用步骤//1.新建类去继承Fragment//2.重写oncrateview//3.新建布局文件并修改2中方法//4.在Activity的布局文件里使用(name:)  @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button1=(Button) findViewById(R.id.button1);button2=(Button) findViewById(R.id.button2);button1.setOnClickListener(this);button2.setOnClickListener(this);}public void button2(View view){}public void button1(View view){}//Fragment替换private void replaceFragment(Fragment fragment) {// TODO Auto-generated method stubFragmentManager fragmentManager=getSupportFragmentManager();FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();fragmentTransaction.replace(R.id.myfragment, fragment);fragmentTransaction.addToBackStack(null);//返回栈 fragmentTransaction.commit();}@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubswitch (arg0.getId()) {case R.id.button1:FragmentManager fragmentManager=getSupportFragmentManager();FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();fragmentTransaction.replace(R.id.myfragment, new MyFragment1());fragmentTransaction.commit();break;case R.id.button2:replaceFragment(new MyFragment2());break;default:break;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}

2.布局文件里效果和代码



<LinearLayout 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="horizontal"    tools:context="com.example.fragment.MainActivity" >    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:orientation="vertical" >        <Button            android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="button1"            android:text="Button1" />        <Button            android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="button2"            android:text="Button2" />    </LinearLayout>    <fragment        android:id="@+id/myfragment"        android:name="com.example.fragment.MyFragment1"        android:layout_width="0dp"        android:layout_height="fill_parent"        android:layout_weight="1" /></LinearLayout>

3.Fragment1第一个碎片

R.layout.myfragment1为要加载的布局

第二个碎片同理

package com.example.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment1 extends Fragment {
// Fragment 特点一:必须依赖Activity
@Override
//onCreateView加载布局用的
public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 View v=inflater.from(getActivity()).inflate(R.layout.myfragment1, container, false);
 return v;

}
}


原创粉丝点击