Fragment是碎片的意思,我们在做移动开发的时候,通常在不同的设备上显示的大小不一样,为了适应不同的设备的屏宽显示的东西不一样,我们用到了Fragment。现在我们在做页面时基本用到了fragment进行页面的拼接。
首先我们自定义一个fragment文件,把要进行的操作写好,在xml文件里有一个按钮和一个图片,点击按钮可以切换图片。
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<Button
android:id="@+id/btn"
android:layout_below="@id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击按钮更换Fragment的图片"/>
那么,在对应的activity里,设置fragment的信息
package com.example.fragment.frag;
import com.example.fragment.R;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
public class OneFragment extends Fragment implements OnClickListener{
private Activity mActivity;
public ImageView img;
//当Fragment添加到主Activity中调用,即得到主Activity的调用
@Override
public void onAttach(Activity activity) {
this.mActivity=activity;
super.onAttach(activity);
}
//设置Fragment全局信息
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
//创建Fragment界面
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.one_fragment, null);
img=(ImageView)view.findViewById(R.id.img);
Button btn=(Button) view.findViewById(R.id.btn);
Button btn2=(Button) view.findViewById(R.id.btn2);
btn.setOnClickListener(this);
btn2.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
img.setImageResource(R.drawable.ic_video);
break;
case R.id.btn2:
if(onBackgroundChangeListener!=null){
this.onBackgroundChangeListener.onBackgroundChange();
}
break;
default:
break;
}
}
}
然后在另外一个xml文件,设置fragment显示的位置,在scrollview里面显示。
<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" >
<LinearLayout
android:id="@+id/btnline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/addbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新增"/>
<Button
android:id="@+id/delbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"/>
<Button
android:id="@+id/changedbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="替换"/>
<Button
android:id="@+id/hidebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隐藏"/>
<Button
android:id="@+id/showbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btnline">
<LinearLayout
android:id="@+id/fragline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</RelativeLayout>
最后在主Activity里
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two_fragment);
findViewById(R.id.addbtn).setOnClickListener(this);
findViewById(R.id.delbtn).setOnClickListener(this);
findViewById(R.id.changedbtn).setOnClickListener(this);
findViewById(R.id.hidebtn).setOnClickListener(this);
findViewById(R.id.showbtn).setOnClickListener(this);
findViewById(R.id.changeimg).setOnClickListener(this);
btnadd=(Button)findViewById(R.id.addbtn);
}
@Override
public void onClick(View v) {
FragmentTransaction ft;
switch (v.getId()) {
case R.id.addbtn:
FragmentManager fm=getSupportFragmentManager();
ft=fm.beginTransaction();
ft.add(R.id.fragline, new OneFragment(), "one");
ft.commit();
break;
case R.id.delbtn:
Fragment f=getSupportFragmentManager().findFragmentByTag("one");
//删除fragment功能
ft=getSupportFragmentManager().beginTransaction();
ft.remove(f);
ft.commit();
break;
case R.id.changedbtn:
break;
case R.id.hidebtn:
ft=getSupportFragmentManager().beginTransaction();
Fragment f2=getSupportFragmentManager().findFragmentByTag("one");
ft.hide(f2);
ft.commit();
break;
case R.id.showbtn:
ft=getSupportFragmentManager().beginTransaction();
Fragment f3=getSupportFragmentManager().findFragmentByTag("one");
ft.show(f3);
ft.commit();
break;
case R.id.changeimg:
OneFragment oft=(OneFragment)getSupportFragmentManager().findFragmentByTag("one");
if(oft!=null){
ImageView img=(ImageView)oft.getView().findViewById(R.id.img);
img.setImageResource(R.drawable.ic_video);
}
break;
default:
break;
}
}