如何在一个片段中的操作栏下执行选项菜单

时间:2016-01-09 12:59:46

标签: android android-fragments optionmenu

我正在开发一款社交Android应用,其中我有三个片段。在我的第二个片段(朋友)中,我想要一个选项菜单(搜索,联系人等)

这样的事情:

https://jsfiddle.net/mm7b7t5t/

1 个答案:

答案 0 :(得分:1)

您必须在此处实现Pageviewer,例如ViewPager

在xml文件中添加viewpager库

          <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
           android:visibility="gone"
            android:layout_height="match_parent">

        </android.support.v4.view.ViewPager>

herre是您在Mainactivity的oncreate方法中的viewpager实例

          viewPager = (ViewPager) findViewById(R.id.pager);
    mAdapter = new MultiPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    viewPager.setCurrentItem(1);
    GetAllApplications();

    custmAdapter = new CustomAdapter(this, systemAppsInfo);
    init();
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
    viewPager.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            viewPager.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

寻呼机的适配器在这里

        public class MultiPagerAdapter extends FragmentPagerAdapter {

public MultiPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 1:
        // Top Rated fragment activity
        return new ApplicationsFragment();
    case 2:
        // Games fragment activity
        return new SearchAppFragment();
    case 0:
            // Games fragment activity
            return new RecentAppsFragment();
    }


    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}

}

这里是实施搜索的片段

public class SearchAppFragment extends Fragment {
Utitlity utitlity;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.sec_fragment, container, false);
    final EditText editSearch = (EditText) rootView.findViewById(R.id.edit_search_fragment);
    ListView lv = (ListView) rootView.findViewById(R.id.app_list);
    final CustomAdapter customAdapter = new CustomAdapter(getContext(), MainActivity.systemAppsInfo);
    utitlity = new Utitlity(getContext());
    lv.setAdapter(customAdapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            try {
                Intent LaunchIntent = getContext().getPackageManager().getLaunchIntentForPackage(MainActivity.systemAppsInfo.get(position).getPname());
                startActivity(LaunchIntent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });