Android - 每次从片段切换时都做一些事情

时间:2018-01-17 12:42:08

标签: android android-fragments

我的活动有两个片段。该活动具有自定义工具栏,并且片段使用屏幕上的剩余空间(一次一个)。在此工具栏中有一个SearchView.OnQueryTextListener但是,如果片段2符合条件,则应始终在片段1和片段2中提供此搜索。所以我需要的是一种方法来检查,每次我从片段切换,如果它占用屏幕,如果它是片段,我将searchView设置为可用,对于片段2,searchView将可用只有符合标准。

这是我的活动:

public class ActivityTeams extends BaseActivity implements SearchView.OnQueryTextListener {

    public ActivityTeams(){
        super(R.layout.activity_teams);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        token = SharedPrefsHelper.getString(getApplicationContext(), Constants.TOKEN);
        listview = (ListView) findViewById(R.id.listview);
        listview.setVisibility(View.INVISIBLE);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new InternsFragment(), getResources().getString(R.string.team_internal));
        adapter.addFragment(new ExternsFragment(), getResources().getString(R.string.team_external));
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

    @Override
    protected void initToolbar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if(toolbar!=null) {
            if (user_type == 1) {
                toolbar.setTitle(R.string.collaborators_title);
            } else if (user_type == 2) {
                toolbar.setTitle(R.string.admin_title);
            }
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            toolbar.setNavigationIcon(R.drawable.ic_action_back_black);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    onBackPressed();
                }
            });
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search_team, menu);

        SearchManager searchManager = (SearchManager)
                getSystemService(Context.SEARCH_SERVICE);
        searchMenuItem = menu.findItem(R.id.search);

        searchView = (SearchView) searchMenuItem.getActionView();
        ImageView searchSubmit = (ImageView) searchView.findViewById (android.support.v7.appcompat.R.id.search_go_btn);
        searchSubmit.setImageResource(R.drawable.ic_dots_w);

        searchSubmit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                //Clear query
                searchView.setQuery("", false);
                //Collapse the action view
                searchView.onActionViewCollapsed();
                //Collapse the search widget
                searchMenuItem.collapseActionView();

                listview.setVisibility(View.GONE);

                viewPager.bringToFront();

                appBarLayout.setVisibility(View.VISIBLE);
            }
        });

        for (TextView textView : findChildrenByClass(searchView, TextView.class)) {
            textView.setTextColor(Color.BLACK);
            textView.setHintTextColor(Color.BLACK);
        }

        searchView.setSearchableInfo(searchManager.
                getSearchableInfo(getComponentName()));
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(this);

        return true;
    }

这是片段1:

public class InternsFragment extends Fragment{

    public InternsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_interns, container, false);

        return rootView;
    }

这是片段2:

public class ExternsFragment extends Fragment {

    public ExternsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_externs, container, false);

        return rootView;
    }
在我的活动中,

searchMenuItem是我想根据其可见的片段改变可见性的事情。

1 个答案:

答案 0 :(得分:1)

使用ViewPager.OnPageChangeListener并在其中使用InvalidateOptionsMenu()

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
         currentFragment = position // use a field to store position
        invalidateOptionsMenu();
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

            }
        }
    });

然后根据位置,在onPrepareOptionsMenu

中设置菜单