Fragment使用案例

import android.R.string;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import android.app.Fragment;
import android.app.ListFragment;
import android.content.res.Resources;
import android.app.PendingIntent.OnFinished;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.graphics.Color;

public class MoreFragment extends Fragment {
    private Callbacks mCallbacks;
    private ListView mListView;
    private ImageButton mFragmentExit;
    private ImageButton mImageExit;
    private Resources mResources;
    private LinearLayout ly;
    private Animation mEnterAnimation;
    private Animation mExitAnimation;

    public interface Callbacks {
        public void onItemSelected(int resId);

        public void onButtonClick();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mResources = getActivity().getResources();
        View view = inflater.inflate(R.layout.more_list, null);
        ly = (LinearLayout) view.findViewById(R.id.layout);
        mEnterAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.enter_anim);
        mExitAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.exit_anim);
        mExitAnimation.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation arg0) {
            }
            @Override
            public void onAnimationRepeat(Animation arg0) {
            }
            @Override
            public void onAnimationEnd(Animation arg0) {
                ((Callbacks) getActivity()).onButtonClick();
            }
        });
        ly.setAnimation(mEnterAnimation);
        mFragmentExit = (ImageButton) view.findViewById(R.id.fragmentexit);
        mFragmentExit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (getActivity() instanceof Callbacks) {
                    ly.setAnimation(mExitAnimation);
                }
            }
        });
        mImageExit = (ImageButton) view.findViewById(R.id.imageexit);
        mImageExit.setBackgroundColor(Color.TRANSPARENT);
        mImageExit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (getActivity() instanceof Callbacks) {
                    ly.setAnimation(mExitAnimation);
                }
            }
        });
        mListView = (ListView) view.findViewById(R.id.morelist);
        String[] arr = new String[3];
        if (getArguments().containsKey("first")) {
            arr[0] = mResources.getString(getArguments().getInt("first"));
        }
        if (getArguments().containsKey("second")) {
            arr[1] = mResources.getString(getArguments().getInt("second"));
        }
        if (getArguments().containsKey("third")) {
            arr[2] = mResources.getString(getArguments().getInt("third"));
        }
        List<Map<String, String>> listItems = new ArrayList<Map<String, String>>();
        for (int i = 0; i < arr.length; i++) {
            Map<String, String> listItem = new HashMap<String, String>();
            listItem.put("title", arr[i]);
            listItems.add(listItem);
        }
        SimpleAdapter mSimpleAdapter = new SimpleAdapter(getActivity(), listItems, R.layout.item_more_list, new String[] {"title"}, new int[] {R.id.frag_title});
        mListView.setAdapter(mSimpleAdapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                if (getActivity() instanceof Callbacks) {
                    ly.setAnimation(mExitAnimation);
                    switch (arg2) {
                    case 0:
                        ((Callbacks) getActivity()).onItemSelected(getArguments().getInt("first"));
                        break;
                    case 1:
                        ((Callbacks) getActivity()).onItemSelected(getArguments().getInt("second"));
                        break;
                    case 2:
                        ((Callbacks) getActivity()).onItemSelected(getArguments().getInt("third"));
                        break;
                    default:
                        ((Callbacks) getActivity()).onItemSelected(-1);
                        break;
                    }
                }
            }
        });
        return view;
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}

别忘了Activity implements MoreFragment.Callbacks, ……{^_^};

Activity中实现接口:

    public void onItemSelected(int resId) {
        switch (resId) {
        case 1 : 
 break; case 2 :
            
break; case 3 : break; default: LogUtils.d(TAG, "invalid selected item"); break; } } public void onButtonClick(){ mFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); mFragmentTransaction = mFragmentManager.beginTransaction(); mFragmentTransaction.remove(mMoreFragment).commit(); }

启动Fragment:

                MoreFragment mMoreFragment;
                mMoreFragment = new MoreFragment(); Bundle arguments = new Bundle(); arguments.putInt("first", 1); arguments.putInt("second", 2); arguments.putInt("third", 3); mMoreFragment.setArguments(arguments); mFragmentTransaction = mFragmentManager.beginTransaction(); mFragmentTransaction.add(R.id.flt, mMoreFragment); mFragmentTransaction.addToBackStack(null); mFragmentTransaction.commit();

关闭Fragment:

        mFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.remove(mMoreFragment).commit();

 

posted @ 2015-02-28 14:47  williamgufeng  阅读(777)  评论(0编辑  收藏  举报