Android中Fragment的分屏顯示處理橫豎屏顯示的實現(xiàn)方法
演示效果如下:

另外在豎屏的時候是這樣的效果:

布局文件如下:

可以看出有兩個資源文件,一個是處理橫屏一個是豎屏
第一個:
<?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="horizontal" >
<fragment
android:id="@+id/titles"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.xuliugen.frag.ListFragment" />
</LinearLayout>
第二個:
<?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="horizontal" >
<fragment
android:id="@+id/titles"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.xuliugen.frag.ListFragment" />
<FrameLayout
android:id="@+id/detail"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>
類代碼

Data.java
public final class Data {
// 標題
public static final String[] TITLES = { "線性布局", "表格布局", "幀布局", "相對布局"
};
// 詳細內容
public static final String[] DETAIL = {
"線性布局是將放入其中的組件按照垂直或水平方向來布局,也就是控制放入其中的組件橫向排列或縱向排列。"
+ "在線性布局中,每一行(針對垂直排列)或每一列(針對水平排列)中只能放一個組件。"
+ "并且Android的線性布局不會換行,當組件一個挨著一個排列到窗體的邊緣后,剩下的組件將不會被顯示出來。",
"表格布局與常見的表格類似,它以行、列的形式來管理放入其中的UI組件。"
+ "表格布局使用<TableLayout>標記定義,在表格布局中,可以添加多個<TableRow>標記,"
+ "每個<TableRow>標記占用一行,由于<TableRow>標記也是容器,所以在該標記中還可添加其他組件,"
+ "在<TableRow>標記中,每添加一個組件,表格就會增加一列。在表格布局中,列可以被隱藏,"
+ "也可以被設置為伸展的,從而填充可利用的屏幕空間,也可以設置為強制收縮,直到表格匹配屏幕大小。",
"在幀布局管理器中,每加入一個組件,都將創(chuàng)建一個空白的區(qū)域,通常稱為一幀,"
+ "這些幀都會根據(jù)gravity屬性執(zhí)行自動對齊。默認情況下,幀布局是從屏幕的左上角(0,0)坐標點開始布局,"
+ "多個組件層疊排序,后面的組件覆蓋前面的組件。",
"相對布局是指按照組件之間的相對位置來進行布局,如某個組件在另一個組件的左邊、右邊、上面或下面等。" };
}
DetailFragment.java
package com.xuliugen.frag;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;
public class DetailFragment extends Fragment {
// 創(chuàng)建一個DetailFragment的新實例,其中包括要傳遞的數(shù)據(jù)包
public static DetailFragment newInstance(int index) {
DetailFragment f = new DetailFragment();
// 將index作為一個參數(shù)傳遞
Bundle bundle = new Bundle(); // 實例化一個Bundle對象
bundle.putInt("index", index); // 將索引值添加到Bundle對象中
f.setArguments(bundle); // 將bundle對象作為Fragment的參數(shù)保存
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0); // 獲取要顯示的列表項索引
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
ScrollView scroller = new ScrollView(getActivity()); // 創(chuàng)建一個滾動視圖
TextView text = new TextView(getActivity()); // 創(chuàng)建一個文本框對象
text.setPadding(10, 10, 10, 10); // 設置內邊距
scroller.addView(text); // 將文本框對象添加到滾動視圖中
text.setText(Data.DETAIL[getShownIndex()]); // 設置文本框中要顯示的文本
return scroller;
}
}
ListFragment.java
package com.xuliugen.frag;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListFragment extends android.app.ListFragment {
boolean dualPane; // 是否在一屏上同時顯示列表和詳細內容
int curCheckPosition = 0; // 當前選擇的索引位置
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_checked, Data.TITLES)); // 為列表設置適配器
View detailFrame = getActivity().findViewById(R.id.detail); // 獲取布局文件中添加的FrameLayout幀布局管理器
dualPane = detailFrame != null
&& detailFrame.getVisibility() == View.VISIBLE; // 判斷是否在一屏上同時顯示列表和詳細內容
if (savedInstanceState != null) {
curCheckPosition = savedInstanceState.getInt("curChoice", 0); // 更新當前選擇的索引位置
}
if (dualPane) { // 如果在一屏上同時顯示列表和詳細內容
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); // 設置列表為單選模式
showDetails(curCheckPosition); // 顯示詳細內容
}
}
// 重寫onSaveInstanceState()方法,保存當前選中的列表項的索引值
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", curCheckPosition);
}
// 重寫onListItemClick()方法
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position); // 調用showDetails()方法顯示詳細內容
}
void showDetails(int index) {
curCheckPosition = index; // 更新保存當前索引位置的變量的值為當前選中值
if (dualPane) { // 當在一屏上同時顯示列表和詳細內容時
getListView().setItemChecked(index, true); // 設置選中列表項為選中狀態(tài)
DetailFragment details = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detail); // 獲取用于顯示詳細內容的Fragment
if (details == null || details.getShownIndex() != index) { // 如果如果
details = DetailFragment.newInstance(index); // 創(chuàng)建一個新的DetailFragment實例用于顯示當前選擇項對應的詳細內容
// 要在activity中管理fragment, 需要使用FragmentManager
FragmentTransaction ft = getFragmentManager()
.beginTransaction();// 獲得一個FragmentTransaction的實例
ft.replace(R.id.detail, details); // 替換原來顯示的詳細內容
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); // 設置轉換效果
ft.commit(); // 提交事務
}
} else { // 在一屏上只能顯示列表或詳細內容中的一個內容時
// 使用一個新的Activity顯示詳細內容
Intent intent = new Intent(getActivity(),
MainActivity.DetailActivity.class); // 創(chuàng)建一個Intent對象
intent.putExtra("index", index); // 設置一個要傳遞的參數(shù)
startActivity(intent); // 開啟一個指定的Activity
}
}
}
MainActivity.java
package com.xuliugen.frag;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// 創(chuàng)建一個繼承Activity的內部類,用于在手機界面中,通過Activity顯示詳細內容
public static class DetailActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 判斷是否為橫屏,如果為橫屏,則結束當前Activity,準備使用Fragment顯示詳細內容
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
finish(); // 結束當前Activity
return;
}
if (savedInstanceState == null) { //
// 在初始化時插入一個顯示詳細內容的Fragment
DetailFragment details = new DetailFragment();// 實例化DetailFragment的對象
details.setArguments(getIntent().getExtras()); // 設置要傳遞的參數(shù)
getFragmentManager().beginTransaction()
.add(android.R.id.content, details).commit(); // 添加一個顯示詳細內容的Fragment
}
}
}
}
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
相關文章
Android開發(fā)之RadioGroup的簡單使用與監(jiān)聽示例
這篇文章主要介紹了Android開發(fā)之RadioGroup的簡單使用與監(jiān)聽,結合實例形式分析了Android針對RadioGroup單選按鈕簡單實用技巧,需要的朋友可以參考下2017-07-07
Android自定義RecyclerView Item頭部懸浮吸頂
這篇文章主要為大家詳細介紹了Android自定義RecyclerView Item頭部懸浮吸頂,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
Android依據(jù)名字通過反射獲取在drawable中的圖片
依據(jù)圖片的名字,通過反射獲取其在drawable中的ID,在根據(jù)此ID顯示圖片,具體實現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
Android onActivityResult和setResult方法詳解及使用
這篇文章主要介紹了Android onActivityResult和setResult方法詳解及使用的相關資料,這里提供實例,幫助大家學習理解,需要的朋友可以參考下2016-12-12
Android編程操作嵌入式關系型SQLite數(shù)據(jù)庫實例詳解
這篇文章主要介紹了Android編程操作嵌入式關系型SQLite數(shù)據(jù)庫的方法,結合實例形式較為詳細的分析了Android操作SQLite數(shù)據(jù)庫的基本技巧與相關注意事項,需要的朋友可以參考下2016-01-01

