码迷,mamicode.com
首页 > 其他好文 > 详细

【Fragment精深系列8】Fragment对自身事件的处理

时间:2015-05-27 10:04:58      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

??Fragment自身的事件处理,不一定非要使用接口回调,在activity中实现具体的处理方法。如果事件处理比较简单,Fragment本身就可以处理,比如Fragment的上下文菜单的处理,简单的话,就不需要使用接口回调。下面就是一个简单的使用接口回调的案例:

一、案例:处理fragment中的popupmenu点击事件,跳转到其他的activity

技术分享

二、fragment的代码:

package com.ht.phoneguard.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.*;
import android.widget.*;
import com.ht.phoneguard.R;
import com.ht.phoneguard.adapter.ContactsAdapter;
import com.ht.phoneguard.adapter.DarkAdapter;
import com.ht.phoneguard.dao.DbManager;
import com.ht.phoneguard.pojo.Info;

import java.util.List;

/**
 * Created by IntelliJ IDEA
 * Project: com.ht.myapp150427.fragments
 * Author: 安诺爱成长
 * Email: 1399487511@qq.com
 * Date: 2015/4/29
 */
public class DarkFragment extends Fragment {
    private Callbacks mCallbacks;
    private ListView listView;
    private DarkAdapter adapter;
    private List<Info> infoList;
    int position;

    public interface Callbacks {
        public void addOnClick(View view);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (!(activity instanceof Callbacks)) {
            throw new IllegalStateException("DarkFragment所在的activity必须实现Callbacks接口");
        }
        mCallbacks = (Callbacks) activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //从数据库中得到黑名单的集合
        super.onActivityCreated(savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_dark, container, false);
        infoList = DbManager.getInstance().getDarkList();
        listView = (ListView) view.findViewById(R.id.dark);
        adapter = new DarkAdapter(getActivity(), infoList);
        listView.setAdapter(adapter);
        registerForContextMenu(listView);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        infoList.clear();
        infoList.clear();
        infoList.addAll(DbManager.getInstance().getDarkList());
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        position = 0;
        if (menuInfo instanceof AdapterView.AdapterContextMenuInfo) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
            position = info.position;
        }
        menu.add("删除:" + infoList.get(position).getPhonenumber());

    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        Info info = infoList.get(position);
        DbManager.getInstance().deleteDark(info);
        infoList.clear();
        infoList.addAll(DbManager.getInstance().getDarkList());
        adapter.notifyDataSetChanged();
        return super.onContextItemSelected(item);
    }
}

三、activity的代码:

package com.ht.phoneguard.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.ht.phoneguard.R;
import com.ht.phoneguard.dao.DbManager;
import com.ht.phoneguard.fragment.SmsFragment;
import com.ht.phoneguard.fragment.PhoneFragment;
import com.ht.phoneguard.fragment.DarkFragment;
import com.ht.phoneguard.adapter.MainFragmentPagerAdapter;
import com.ht.phoneguard.service.PhoneService;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements DarkFragment.Callbacks{


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DbManager.createInstance(getApplicationContext());
        ViewPager viewPager = (ViewPager) findViewById(R.id.main_view_pager);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new DarkFragment());
        fragments.add(new SmsFragment());
        fragments.add(new PhoneFragment());
        MainFragmentPagerAdapter adapter = new MainFragmentPagerAdapter(
                getSupportFragmentManager(),
                fragments
        );
        viewPager.setAdapter(adapter);

        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int count = group.getChildCount();
                int checkPos = 0;
                for (int i = 0; i < count; i++) {
                    View view = group.getChildAt(i);
                    if (view instanceof RadioButton) {
                        RadioButton rb = (RadioButton) view;
                        boolean checked = rb.isChecked();
                        if (checked) {
                            checkPos = i;
                            break;
                        }
                    }
                }
                viewPager.setCurrentItem(checkPos, false);
            }
        });


        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                View view = radioGroup.getChildAt(i);
                if (view != null) {
                    if (view instanceof RadioButton) {
                        RadioButton rb = (RadioButton) view;
                        rb.setChecked(true);
                    }
                }


            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        //初始的时候在黑名单界面
        RadioButton button = (RadioButton) radioGroup.getChildAt(0);
        button.setChecked(true);

        //开启服务
        Intent intent = new Intent(this, PhoneService.class);
        startService(intent);
    }

    //实现darkfragment中的接口方法
    @Override
    public void addOnClick(View view) {
        PopupMenu popupMenu = new PopupMenu(this, view);
        popupMenu.inflate(R.menu.add_menu);
        popupMenu.show();
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                int itemId = menuItem.getItemId();
                switch (itemId) {
                    //点击了从联系人添加的响应事件
                    case R.id.addfromcontacts:
                        Intent intent = new Intent(MainActivity.this, AddFromComtactsActivity.class);
                        startActivity(intent);
                        break;
                    //点击了手动添加的响应事件
                    case R.id.addshoudong:
                        Intent intent2 = new Intent(MainActivity.this, AddShoudongActivity.class);
                        startActivity(intent2);
                        break;
                }
                return true;
            }
        });
    }
}

【Fragment精深系列8】Fragment对自身事件的处理

标签:

原文地址:http://blog.csdn.net/a910626/article/details/46042267

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!