1. 程式人生 > >多個Fragment的使用

多個Fragment的使用

先上BaseActivity,我都喜歡封裝下的


package com.jds.base;



import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;


/**
 * 
 * @author Deson Created on 2015-10-29
 *
 */
public abstract class BaseActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
}


public abstract void findViewById();


public abstract void setListener();


public abstract void initView();


@SuppressWarnings("unchecked")
public <T extends View> T getView(int id) {
T result = (T) findViewById(id);
if (result == null) {
throw new IllegalArgumentException("view 0x"
+ Integer.toHexString(id) + "doesn't exist");
}


return result;
}


/**
* 模板方法
*/
public void templateMethod() {
findViewById();
setListener();
initView();
}

}




然後就是MainActivity


package com.jds.testexternal;


import com.jds.base.BaseActivity;
import com.jds.fragment.Tab1Fragment;
import com.jds.fragment.Tab2Fragment;
import com.jds.fragment.Tab3Fragment;
import com.jds.fragment.Tab4Fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.Toast;


/**
 * 
 * @author  Deson Created on 2015-10-29
 *
 */
public class MainActivity extends BaseActivity implements OnClickListener {


private FragmentManager fm;
private Tab1Fragment tab1Fragment;
private Tab2Fragment tab2Fragment;
private Tab3Fragment tab3Fragment;
private Tab4Fragment tab4Fragment;
private FragmentTransaction transaction;
private Fragment[] fragments;
private RelativeLayout rl_container;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
templateMethod();


}


@Override
public void findViewById() {
rl_container = getView(R.id.rl_container);
Toast.makeText(this, "findViewById", Toast.LENGTH_LONG).show();


}


@Override
public void setListener() {
findViewById(R.id.btn_music).setOnClickListener(this);
findViewById(R.id.btn_movie).setOnClickListener(this);
findViewById(R.id.btn_community).setOnClickListener(this);
findViewById(R.id.btn_interact).setOnClickListener(this);


}


@Override
public void initView() {
tab1Fragment = new Tab1Fragment();
tab2Fragment = new Tab2Fragment();
tab3Fragment = new Tab3Fragment();
tab4Fragment = new Tab4Fragment();
fragments = new Fragment[] { tab1Fragment, tab2Fragment, tab3Fragment,
tab4Fragment };
//start the transaction
transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.rl_container, fragments[0])
.add(R.id.rl_container, fragments[1])
.add(R.id.rl_container, fragments[2])
.add(R.id.rl_container, fragments[3]).hide(fragments[0])
.hide(fragments[1]).hide(fragments[2]).hide(fragments[3])
.show(fragments[0]).commit();


}


/**
* 主類View.OnClickListener

* @param v
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_music:
showSelectFragment(0);
break;
case R.id.btn_movie:
showSelectFragment(1);
break;
case R.id.btn_community:
showSelectFragment(2);
break;
case R.id.btn_interact:
showSelectFragment(3);
break;


default:
break;
}


}


@Override
public boolean dispatchKeyEvent(KeyEvent event) {
Toast.makeText(this, "onKeyEvent", Toast.LENGTH_LONG).show();
return super.dispatchKeyEvent(event);
}


private void showSelectFragment(int index) {
FragmentTransaction trx = getSupportFragmentManager()
.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i != index) {
trx.hide(fragments[i]);
} else {
trx.show(fragments[i]);
}
}
trx.commitAllowingStateLoss();


}


}


然後是佈局檔案:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <LinearLayout
        android:id="@+id/main_bottom"
        android:layout_width="match_parent"
        android:layout_height="54dp"
        android:layout_alignParentBottom="true"
        android:background="#ffffff"
        android:gravity="center_vertical"
        android:orientation="horizontal" >


        <Button
            android:id="@+id/btn_music"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:text="音樂" />


        <Button
            android:id="@+id/btn_movie"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:text="影視" />


        <Button
            android:id="@+id/btn_community"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:text="社群" />


        <Button
            android:id="@+id/btn_interact"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:text="互動" />
    </LinearLayout>


    <View
        android:id="@+id/view_temp"
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:layout_above="@id/main_bottom"
        android:background="#d6d6d6" />


    <RelativeLayout
        android:id="@+id/rl_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/view_temp"
        android:layout_below="@+id/relativeLayout" />


</RelativeLayout>


然後就是Tab1Fragment中的程式碼了

package com.jds.fragment;


import com.jds.testexternal.R;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Tab1Fragment extends Fragment {


private View view;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_tab1, container, false);
/*
* findViewById(); initView();
*/
return view;
}


}


然後其他的Fragment程式碼都是一樣的,只是佈局檔案不同而已,大家可以自己寫,完事!