安卓中點選不同按鈕切換不同到Fragment
阿新 • • 發佈:2019-01-24
整體效果如下:
實現方式:通過Activity的FragmentManage去實現
首先要先去建立兩個佈局檔案,分別為pay.xml和income.xml,代表兩個片段的內容,下面我只是貼了其中一個佈局檔案的內容
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#445543" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout>
簡單起見我只是把這兩個佈局檔案的background給設定了一下,用於區分。
然後寫兩個類,分別為PayFragment和IncomeFragment,讓它們都去繼承Fragment類,重寫onCreateView方法,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.pay, null);
return view;
}
Inflate()作用就是將xml定義的一個佈局找出來,inflate方法中間那個引數是制定的佈局檔案。
完成上面的工作後我們就該總佈局檔案的編寫了:
然後寫一個類去繼承Activity:<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=".SecondActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/pay" android:text="支出"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/income" android:text="收入"/> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/fl"> </FrameLayout> </LinearLayout>
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
public class SecondActivity extends FragmentActivity implements OnClickListener{
private Button pay;
private Button income;
private RelativeLayout rl;
private IncomeFragment incomeFragment;
private PayFragment payFragment;
FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initview();
}
private void initview(){
pay = (Button) findViewById(R.id.pay);
income = (Button) findViewById(R.id.income);
pay.setOnClickListener(this);
income.setOnClickListener(this);
fm = getSupportFragmentManager();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.income:
setTabSelection(0);
break;
case R.id.pay:
setTabSelection(1);
break;
}
}
private void setTabSelection(int index){
FragmentTransaction ft = fm.beginTransaction();
hideFragment(ft);
switch (index) {
case 0:
if(incomeFragment==null){
incomeFragment = new IncomeFragment();
ft.add(R.id.fl, incomeFragment);
}else{
ft.show(incomeFragment);
}
break;
case 1:
if(payFragment==null){
payFragment = new PayFragment();
ft.add(R.id.fl, payFragment);
}
ft.show(payFragment);
break;
}
ft.commit();
}
//用於隱藏fragment
private void hideFragment(FragmentTransaction ft){
if(incomeFragment!=null){
ft.hide(incomeFragment);
}if(payFragment!=null){
ft.hide(payFragment);
}
}
}
其中重要是用了FragmentTransaction的show和hide方法,當然,如果用replace方法也能實現,但是replace方法相比而言比用show和hide方法要浪費資源,因為replace方法其實就是remove方法和add方法的結合,當我們載入佈局檔案後當不需要顯示的時候就remove掉,當用的時候再去載入,這個中間要耗資源,如果用show和hide方法的話,如果用到某個佈局,那我們就show,不顯示佈局就hide,這樣避免了重複載入。