家庭片段部分可见与当前片段android studio

时间:2016-11-25 11:25:39

标签: android android-fragments onclicklistener android-navigationview

我的Android应用程序中有一个导航抽屉,它的默认加载片段作为主屏幕是homefragment。主页片段上有图像视图,它可以监听点击并在每个图像视图上打开不同的片段,问题是片段在全屏显示,而我希望它们在导航视图的工具栏下看到,当我按下后退按钮时,应用程序退出到主屏幕登录界面。有人请建议我处理这个问题。 这是homefragment的代码,它是导航抽屉的默认值:

 public class HomeFragment extends Fragment {
ImageView im1,im2,im3,im4,im5,im6,im7,im8,im9,im10,im11,im12,im13,im14;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (container != null) {
        container.removeAllViews();
    }
    View view = inflater.inflate(R.layout.activity_home,
            container, false);
   im1 = (ImageView) view.findViewById(R.id.imageView37);
    im2 = (ImageView) view.findViewById(R.id.imageView38);
    im3 = (ImageView) view.findViewById(R.id.imageView39);
    im4 = (ImageView) view.findViewById(R.id.imageView40);
    im5 = (ImageView) view.findViewById(R.id.imageView41);
    im6 = (ImageView) view.findViewById(R.id.imageView42);
    im7 = (ImageView) view.findViewById(R.id.imageView43);
    im8 = (ImageView) view.findViewById(R.id.imageView44);
    im9 = (ImageView) view.findViewById(R.id.imageView45);
    im10 = (ImageView) view.findViewById(R.id.imageView46);
    im11 = (ImageView) view.findViewById(R.id.imageView48);
    im12 = (ImageView) view.findViewById(R.id.imageView50);
    im13 = (ImageView) view.findViewById(R.id.imageView51);
    im14 = (ImageView) view.findViewById(R.id.imageView52);

    im1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            DashboardFragment fragment = new DashboardFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    im2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            MyLawyerFragment fragment = new MyLawyerFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    im3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            AddHearingFragment fragment = new AddHearingFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    im4.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            MyCasesFragment fragment = new MyCasesFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    im5.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            MyPaymentsFragment fragment = new MyPaymentsFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    im6.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            MyClientsFragment fragment = new MyClientsFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    im7.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            ArchivedCasesFragment fragment = new ArchivedCasesFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    im8.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            SubscriptionFragment fragment = new SubscriptionFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    im9.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            SettingsFragment fragment = new SettingsFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    im10.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            WebsiteFragment fragment = new WebsiteFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    im11.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            KeyJudgementsFragment fragment = new KeyJudgementsFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    im12.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            DailyJudgementsFragment fragment = new DailyJudgementsFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    im13.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            JudgementLibraryFragment fragment = new JudgementLibraryFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    im14.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            LogoutFragment fragment = new LogoutFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.drawer_layout, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    return view;
}}

以下是homefragment上的一个图片视图的代码:dashboard - >

public class DashboardFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
private String tabTitles[] = new String[] { "Schedule", "Payments", "Judgements" };
private int[] imageResId = {
        R.drawable.schedule_active,
        R.drawable.payment_summary_active,
        R.drawable.judgements_active
};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /**
     *Inflate tab_layout and setup Views.
     */
    View x =  inflater.inflate(R.layout.tab_layout,null);
    tabLayout = (TabLayout) x.findViewById(R.id.tabs);
    viewPager = (ViewPager) x.findViewById(R.id.viewpager);
    tabLayout.setSelectedTabIndicatorHeight(0);

    /**
     *Set an Apater for the View Pager
     */
    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

    /**
     * Now , this is a workaround ,
     * The setupWithViewPager dose't works without the runnable .
     * Maybe a Support Library Bug .
     */

    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);

        }
    });
  //  createTabIcons();

    return x;

}


class MyAdapter extends FragmentPagerAdapter {

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

    /**
     * Return fragment with respect to Position .
     */

    @Override
    public Fragment getItem(int position)
    {
        switch (position){
            case 0 : return new Schedule();
            case 1 : return new Payment();
            case 2 : return new Judgement();
        }
        return null;
    }

    @Override
    public int getCount() {

        return int_items;

    }

    /**
     * This method returns the title of the tab according to the position.
     */

    @Override
    public CharSequence getPageTitle(int position) {

     Drawable image = getView().getResources().getDrawable(imageResId[position]);
        image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
        // Replace blank spaces with image icon
        SpannableString sb = new SpannableString(" " + tabTitles[position]);
        ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
        sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sb;
}}}

这是另一个片段,也可以在点击其中一个图像视图时调用:

public class MyLawyerFragment extends Fragment {

ImageButton im1;
LinearLayout l1;
ChangeFragListener listener;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listener = (ChangeFragListener)getActivity();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (container != null) {
        container.removeAllViews();
    }
    View v1 = inflater.inflate(R.layout.activity_mylawyer,
            container, false);
    im1 = (ImageButton) v1.findViewById(R.id.imageButton);
    l1 = (LinearLayout) v1.findViewById(R.id.linearLayout4);
    im1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            change("lawyer");

            AddLawyer fragment = new AddLawyer();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.drawer_layout, fragment);
        //    fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    l1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            LawyerDetailsFragment fragment = new LawyerDetailsFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.drawer_layout, fragment);
           // fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });

    return v1;
}

private void change(String lawyer) {
}}

我添加了这段代码,现在两个屏幕没有合并,但工具栏仍然缺失。

screenshot of lawyer fragment where toolbar of navigationview is missing like othe fragments

2 个答案:

答案 0 :(得分:2)

作为对可能遇到同样问题的其他人的帮助,我找到了解决问题的方法,因为我知道我使用了错误的id为fragmentTransaction.replace(); ,因为它应该与导航抽屉中使用的相同,即R.id.frame

答案 1 :(得分:0)

MyLawyer片段中替换此

fragmentTransaction.add(R.id.drawer_layout, fragment);

fragmentTransaction.replace(R.id.drawer_layout, fragment);