如何从导航抽屉调用片段?

时间:2015-03-24 08:08:08

标签: android fragment navigation-drawer android-recyclerview

请帮助我,我是android新手。 我可以通过单击导航抽屉中的项目来显示吐司,但是如何从导航抽屉打开新片段。

我在NavigationDrawerFragment中的代码是

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
        recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
        adapter = new EduAdapter(getActivity(), getData());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
            @Override
            public void onClick(View view, int position) {
                Toast.makeText(getActivity(), "On click  " + position, Toast.LENGTH_LONG).show();

            mDrawerLayout.closeDrawer(GravityCompat.START);
                Toast.makeText(getActivity(), "On Long click  " + position, Toast.LENGTH_LONG).show();

            }

            @Override
            public void onLongClick(View view, int position) {
                Toast.makeText(getActivity(), "On long click  " + position, Toast.LENGTH_LONG).show();
            }
        }));
        return layout;
    }

主要活动是:

public class MainActivity extends ActionBarActivity {


    private Toolbar toolbar;

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

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        NavigationDrawerFragment drawerFragment =
                (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

请帮我从抽屉打开新片段,因为我只能显示吐司。 我跟随Slidenerd Material design Video。

3 个答案:

答案 0 :(得分:0)

创建一个扩展Fragment Activity的类,然后你可以从新类中打开如下所示的新片段:

    DetailViewFragment f1 = new DetailViewFragment(); 
   getSupportFragmentManager().beginTransaction().add(R.id.fragment_layout, f1).commit();

在onItemClicked方法中导航到创建的类。

希望这会对你有所帮助。

答案 1 :(得分:0)

在您的RecyclerView项目上单击,您只需以这种方式添加相应的片段。

 getSupportFragmentManager()
                    .beginTransaction()
                    .replace(your_container_id,
                            new YourFragment(), YOUR_FRAGMENT_STRING_TAG)
                    .commit();

请注意,对于每个位置点击recyclerview,您将以上述方式添加片段。您可以在recyclelerView的onItemClick中使用switch case。 看看它是否对你有所帮助!如果您需要任何其他帮助,请告诉我!!

答案 2 :(得分:0)

创建方法并在Recylerview的onitemclicklistener中传递项目位置

public void changeFragment(int position) {
 switch (position) {
   case 0:{                getSupportFragmentManager().beginTransaction().replace(R.id.container, calenderFragment).setTransition                (FragmentTransaction.TRANSIT_FRAGMENT_OPEN).addToBackStack(null).commit();
                break;
            }
            case 1:{                            getSupportFragmentManager().beginTransaction().replace(R.id.container, new PatientProfileFragment()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).addToBackStack(null).commit();
                break;
            }     
        }
    }