fragment实现横竖屏的切换

来源:互联网 发布:mac怎么免费下载软件 编辑:程序博客网 时间:2024/05/18 22:55

1.以新闻布局为例,首先创建新闻标题fragment 和 新闻内容fragment

1)首先创建手机新闻标题的fragment布局文件

fragment_news_list.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"              android:orientation="vertical"             tools:context="com.wwj_fly.newstoday.fragments.NewsListFragment">  <ListView      android:id="@+id/news_list"      android:layout_width="match_parent"      android:layout_height="match_parent"/></LinearLayout>
2)创建手机新闻的fragment

public class NewsListFragment extends Fragment implements AdapterView.OnItemClickListener {    /*    * fragment 定义的接口 用于给activity传递一个listview点击事件    *    * */    public  interface  OnNewsSelectedListener{        void onNewsSelected(Bundle bundle);    }    private OnNewsSelectedListener mOnNewsSelectedListener;    public NewsListFragment() {        // Required empty public constructor    }<span style="white-space:pre"></span>//当引用Fragment生命周期的onAttach()方法时,就自动获取MainActivity的上下文,所以不用再通过该方法传递上下文了   // public  void setOnNewsSelectedListener(OnNewsSelectedListener OnNewsSelectedListener){   //    mOnNewsSelectedListener = OnNewsSelectedListener;   // }    @Override    public void onAttach(Context context) {        super.onAttach(context);        //内部设置 接口回调        if (context instanceof OnNewsSelectedListener){            mOnNewsSelectedListener = (OnNewsSelectedListener) context;        }else {            throw new IllegalArgumentException("Activity must OnNewsSelectedListener");        }    }    @Override    public void onDetach() {        mOnNewsSelectedListener = null;        super.onDetach();    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        // Inflate the layout for this fragment        View ret = inflater.inflate(R.layout.fragment_news_list, container, false);        ListView listView = (ListView) ret.findViewById(R.id.news_list);        //设置点击        listView.setOnItemClickListener(this);        if (listView != null){            ArrayList<String> data = new ArrayList<>();            for (int i = 0; i < 100; i++) {                data.add("News" + i);            }        ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(                getContext(),android.R.layout.simple_list_item_1,                data        );            listView.setAdapter(mAdapter);        }        return ret;    }    @Override    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {        if (mOnNewsSelectedListener != null){            Bundle bundle = new Bundle();            bundle.putInt("position",position);            bundle.putLong("id",id);            mOnNewsSelectedListener.onNewsSelected(bundle);        }    }}
3)创建新闻内容的fragment布局文件

fragment_detail_title.xml

<FrameLayout 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="com.wwj_fly.newstoday.fragments.DetailFragment">    <!-- TODO: Update blank fragment layout -->    <TextView        android:id="@+id/detail_title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_blank_fragment"/></FrameLayout>
4)创建新闻的内容的fragment

public class DetailFragment extends Fragment {    private TextView mTextTitle;    public DetailFragment() {        // Required empty public constructor    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        // Inflate the layout for this fragment        View ret = inflater.inflate(R.layout.fragment_detail, container, false);        mTextTitle = (TextView)ret.findViewById(R.id.detail_title);        Bundle arguments = getArguments();        if (arguments != null){            long id = arguments.getLong("id");            mTextTitle.setText("id = " + id);        }        return ret;    }    public void setDetailTitle(String title){        mTextTitle.setText(title);    }}
2.创建UI线程用来放置fragment,考虑到横屏竖屏的问题,需要创建不同的activity_main.xml布局文件

1)竖屏的布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?><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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:orientation="vertical"    tools:context="com.wwj_fly.newstoday.MainActivity">    <fragment        android:id="@+id/fragment_list"        class="com.wwj_fly.newstoday.fragments.NewsListFragment"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>
2)横屏的布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="horizontal"              android:layout_width="match_parent"              android:layout_height="match_parent">   <fragment       android:id="@+id/fragment_list"       class="com.wwj_fly.newstoday.fragments.NewsListFragment"       android:layout_width="250dp"       android:layout_height="match_parent"/>    <fragment        android:id="@+id/fragment_detail"        class="com.wwj_fly.newstoday.fragments.DetailFragment"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>

3)UI线程的程序代码

public class MainActivity extends AppCompatActivity implements NewsListFragment.OnNewsSelectedListener{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    @Override    public void onNewsSelected(Bundle bundle) {        Log.d("OnNewsSelected", "bundle: " + bundle);        FragmentManager manager = getSupportFragmentManager();        Fragment fragment = manager.findFragmentById(R.id.fragment_detail);        if (fragment != null && fragment.isVisible()){            DetailFragment detailFragment = (DetailFragment) fragment;            detailFragment.setDetailTitle(bundle.toString());        }else{            Intent intent = new Intent(this,DetailActivity.class);            intent.putExtras(bundle);            startActivity(intent);        }    }}

3.当手机是竖屏状态时,点击listview中的item需要发生跳转,所以需要重新创建新的UI线程

1) 示例代码如下:

public class DetailActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_detail);        // 如果 savedInstanceState == null 代表一次创建 否则代表屏幕发生过旋转        if (savedInstanceState == null) {            Bundle extras = getIntent().getExtras();            if (extras != null){                DetailFragment detailFragment = new DetailFragment();                //将从UI线程传递过来的bundle ,通过下面的方法传递到fragment中                detailFragment.setArguments(extras);                FragmentManager manager = getSupportFragmentManager();                FragmentTransaction tx = manager.beginTransaction();                tx.replace(R.id.detail_fragment_container,detailFragment);                //在事物提交之后 会进入到 fragment的创建的生命周期                // 不能在这个语句执行的附近                tx.commit();            }        }    }}

2)该UI线程的布局文件如下:

<?xml version="1.0" encoding="utf-8"?><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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:orientation="vertical"    tools:context="com.wwj_fly.newstoday.DetailActivity">    <FrameLayout        android:id="@+id/detail_fragment_container"        android:layout_width="match_parent"        android:layout_height="match_parent">    </FrameLayout></LinearLayout>







0 0
原创粉丝点击