首页 > 其他 > 详细

使用Fragment 实现动态UI

时间:2016-03-16 18:48:57      阅读:151      评论:0      收藏:0      [点我收藏+]

首先写好每个Fragment:

1.在第一个Fragment写一个按钮,使其加载下一个Fragment

布局:

<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"
    tools:context=".MainActivity"
    android:orientation="horizontal" >


        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="加载"/>

</LinearLayout>

java代码:

public class LeftFragment extends Fragment{


    OnClickButton mCallback;
    //定义一个接口
    public interface OnClickButton{
        //并实现一个方法,用来传值并在(onAttach()中绑定activity)
        public void onClickB();
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //绑定布局文件并获取到里面的控件,特别 注意里面的  view
        View view = inflater.inflate(R.layout.fragment_left,null);
        Button button = (Button) view.findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCallback.onClickB();
            }
        });
        return view;
    }

    /**
     * 绑定到activity
     * @param activity
     */
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallback = (OnClickButton) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }
}

加载显示出来的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="新闻内容" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

java文件:

public class RightFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_right, null);
        Button button = (Button) view.findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //getActivity()   获取父类的activity
                Toast.makeText(getActivity(), "我是fragment", Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
}

主类:

布局

给Fragment创建一个容器activity_main.xml  

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

注意:一定要写明id。

然后就在activity中实现Fragment add进去就行了!

//实现LeftFragment中定义的接口,主要用来传值或者按钮点击事件
public class MainActivity extends Activity implements LeftFragment.OnClickButton {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化第一个Fragment
        if (findViewById(R.id.fragment_container) != null){
            if (savedInstanceState != null) {
                return;
            }
        LeftFragment leftFragment = new LeftFragment();
        leftFragment.setArguments(getIntent().getExtras());
        getFragmentManager().beginTransaction().add(R.id.fragment_container,leftFragment).commit();
        }
    }

    /**
     * 实现接口中的方法和点击按钮后加载的fragment
     */
    @Override
    public void onClickB() {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        RightFragment rightFragment = new RightFragment();
        transaction.replace(R.id.fragment_container, rightFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}

这样就实现了一个很小的demo!

使用Fragment 实现动态UI

原文:http://www.cnblogs.com/android-host/p/5284455.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!