fragment實現橫豎屏的切換
阿新 • • 發佈:2019-02-09
1.以新聞佈局為例,首先建立新聞標題fragment 和 新聞內容fragment
1)首先建立手機新聞標題的fragment佈局檔案
fragment_news_list.xml
2)建立手機新聞的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" 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>
3)建立新聞內容的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); } } }
fragment_detail_title.xml
4)建立新聞的內容的fragment<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>
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>