从导航链接等其他片段更改片段

时间:2015-07-28 11:10:16

标签: android fragment navigation-drawer

我有一个导航抽屉(所以我使用碎片):

  • MainActivity(FragmentActivity)
  • 片段A(具有片段B,C和D的图片链接的片段)
  • 片段B(片段)
  • 片段C(片段)
  • 片段D(片段)

我的导航抽屉工作正常,但我想要我的片段A来做菜单之类的链接。现在,在片段A中,我将其他片段称为:

ImageView contact = (ImageView) getView().findViewById(R.id.secondBlock);
contact.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Fragment fragment = new ContactezNous();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.accueil, fragment);
        fragmentTransaction.commit();
    }
});

但我不想用这种方式,因为我在查找其他片段的标签和ID方面遇到了问题。

我想知道如何通过点击链接来更改片段片段,就像我点击主要活动的导航抽屉链接一样。

主要活动:

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {

    private static String TAG = MainActivity.class.getSimpleName();
    private Toolbar mToolbar;
    private FragmentDrawer drawerFragment;

    //Initialisation de l activite avec les donnees necessaires
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        drawerFragment = (FragmentDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
        drawerFragment.setDrawerListener(this);
        // Affichage de la navigation
        displayView(0);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Ajout des items
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDrawerItemSelected(View view, int position) {
        displayView(position);
    }

    private void displayView(int position) {
        Fragment fragment = null;
        String title = getString(R.string.app_name);
        switch (position) {
            case 0:
                fragment = new Accueil();
                title = getString(R.string.title_accueil);
                break;
            case 1:
                fragment = new NosOffres();
                title = getString(R.string.title_nosoffres);
                break;
            case 2:
                fragment = new DemandeGratuite();
                title = getString(R.string.title_demandegratuite);
                break;
            case 3:
                fragment = new ContactezNous();
                title = getString(R.string.title_contact);
                break;
            case 4:
                fragment = new Actualites();
                title = getString(R.string.title_actu);
                break;
            case 5:
                fragment = new MentionsLegales();
                title = getString(R.string.title_mentions);
                break;
            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container_body, fragment);
            //fragmentTransaction.addToBackStack("name");
            fragmentTransaction.commit();
            // libelle du toolbar
            TextView titlet;
            titlet = (TextView) findViewById(R.id.main_toolbar_title);
            titlet.setText(title);
            titlet.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/GothamBook.ttf"));
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您应该使用以下常见的Android片段模式:

public class Accueil extends Fragment {
    // Your code here...

    public interface OnFragmentInteractionListener {
        void onInteraction(int id);
    }

    private OnFragmentInteractionListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    // ...more code...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // ...initialize your View...

        ImageView contact = (ImageView) getView().findViewById(R.id.secondBlock);
        contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // This is how you use the listener
                mListener.onInteraction(3);
            }
        });

        // ...more initialization...
    }
}

<强> MainActivity.java

public class MainActivity AppCompatActivity 
        implements FragmentDrawer.FragmentDrawerListener, OnFragmentInteractionListener {
    // Your code here...

    // Implement OnFragmentInteractionListener
    @Override
    public void onInteraction(int id) {
        // 'id' would be '3' for your 'ContactezNous' fragment
        displayView(id);
    }
}