王学岗Fragment(二)

来源:互联网 发布:西直门桥设计数据 编辑:程序博客网 时间:2024/06/11 15:55

先介绍下我们本项目的效果,在点击左侧的菜单的时候,右侧要出现响应的画面。
我们在继承Fragment去填充<fragment/>的时候,也可以继承ListFragment。ListFragment就是Fragment+ListView。我们可以写个类继承ListFragment填充左侧的<fragment/>。右侧使用<FramtLayout/>布局,实现对fragment的增删改查。

@Override    public void onItemClick(AdapterView<?> parent, View view, int position,            long id) {        //position就是点击的item,左侧点击相应的Item,右侧出现相应的内容。 想方法把把position传递给MyContentFragment,

上代码:
main_layout.xml

<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" >    <!-- 基础Fragment   也可以去基础 ListFragment -->    <fragment        android:id="@+id/fragment"        android:name="com.tz.katefragement_repalce.MyMenuFragment"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"        android:background="#0f0"       />    <FrameLayout        android:id="@+id/fl_content"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="2" >    </FrameLayout></LinearLayout>

MainActivity

package com.tz.katefragement_repalce;import android.os.Bundle;import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}

MyContentFragment

package com.tz.katefragement_repalce;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class MyContentFragment extends Fragment {    private int indext;    public int getIndext() {        return indext;    }    public void setIndext(int indext) {        this.indext = indext;    }    String[] days = new String[] { "今天是星期一", "今天是星期二", "今天是星期三", "今天是星期四",            "今天是星期五", "今天是星期六", "今天是星期日", };    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        TextView myText=new TextView(getActivity());        myText.setText(days[indext]);        return myText;    }    public static MyContentFragment getInstance(int position) {//      new对象是在调用onCreateView()之前。        MyContentFragment fragment = new MyContentFragment();        //two:在这里拿到position,定义一个下标,通过get和set方法,把position传给onCreateView();        fragment.setIndext(position);        return fragment;    }}

MyMenuFragment

package com.tz.katefragement_repalce;import android.os.Bundle;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentManager.OnBackStackChangedListener;import android.support.v4.app.FragmentTransaction;import android.support.v4.app.ListFragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;// ListFragment  :Fragment+ListView//ontach---oncreat---onCreatView--onActivityCreatepublic class MyMenuFragment extends ListFragment implements OnItemClickListener {    String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",            "Saturday", "Sunday", };    // fragemnt创建成功    // fragent所依附的activity创建成功    @Override    public void onActivityCreated(Bundle savedInstanceState) {        // 设置适配器后再调用父类方法        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),                android.R.layout.simple_list_item_1, android.R.id.text1, days);        this.setListAdapter(adapter);        // 点击listView中的每个条目事件        getListView().setOnItemClickListener(this);        super.onActivityCreated(savedInstanceState);    }    // fragment的显示的布局控件,这个方法创建一个ListView,我们可以在onActivityCreated()中为其折折设配器    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        return super.onCreateView(inflater, container, savedInstanceState);    }    @Override    public void onItemClick(AdapterView<?> parent, View view, int position,            long id) {        //position就是点击的item,左侧点击相应的Item,右侧出现相应的内容。 想方法把把position传递给MyContentFragment,//第一种方法:创建一个Fragment对象        MyContentFragment fragment=MyContentFragment.getInstance(position);//one:在new对象的时候把position传进去        //把创建出来的fragment放进FrameLayout布局中。需要用到以下FragmentManager类        FragmentManager manager=getActivity().getSupportFragmentManager();        FragmentTransaction transaction=manager.beginTransaction();        //第一次:用我们的fragment替换成FrameLayout        transaction.replace(R.id.fl_content, fragment);        transaction.commit();    }}
0 0
原创粉丝点击