简体   繁体   English

为什么片段没有在导航抽屉中打开?

[英]Why is the fragment not opening in Navigation drawer?

My MainActivity.java contains我的 MainActivity.java 包含

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.calendar:
                fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.main_container,  new Calender());
                fragmentTransaction.commit();
                getSupportActionBar().setTitle("Calender");
                item.setChecked(true);
                break;
           // drawer.closeDrawers();
        }
        return true;
    }
});

and my Calender.java contains我的 Calender.java 包含

public class Calender extends android.support.v4.app.Fragment{
    Activity a;
    CalendarView calendar;
    public Calender() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)  {
        // Inflate the layout for this fragment
        a = new Activity();
        a.setContentView(R.layout.fragment_calender);

        initializeCalendar();
        return inflater.inflate(R.layout.fragment_calender, container, false);

    }

    public void initializeCalendar() {

        calendar = (CalendarView) a.findViewById(R.id.calendar);
        // sets whether to show the week number.
        calendar.setShowWeekNumber(false);
        // sets the first day of week according to Calendar.
        // here we set Monday as the first day of the Calendar
        calendar.setFirstDayOfWeek(2);
        //The background color for the selected week.
        calendar.setSelectedWeekBackgroundColor(getResources().getColor(R.color.green));
        //sets the color for the dates of an unfocused month.
        calendar.setUnfocusedMonthDateColor(getResources().getColor(R.color.transparent));
        //sets the color for the separator line between weeks.
        calendar.setWeekSeparatorLineColor(getResources().getColor(R.color.transparent));
        //sets the color for the vertical bar shown at the beginning and at the end of the selected date.
        calendar.setSelectedDateVerticalBar(R.color.darkgreen);
        //sets the listener to be notified upon selected date change.
        calendar.setOnDateChangeListener(new OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
                Toast.makeText(a.getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
            }
        });

    }
}

is there any problem with my fragment?我的片段有问题吗? or is it in main activity?还是在主要活动中? please help asap.The app opens but where i try to click on the calender button, nothins shows up.请尽快帮助。应用程序打开但我尝试单击日历按钮的地方,没有显示任何内容。

Why do you have an instance of activity in your fragment?为什么你的片段中有一个活动实例? Change your onCreateView to:将您的 onCreateView 更改为:

public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.content_main, parent, false);
    calendar = (CalendarView) view.findViewById(R.id.calendar);
    return view;
}

and then in onViewCreated method do this:然后在 onViewCreated 方法中执行以下操作:

initializeCalendar();

make sure the id of your calendar view in content_main is calendar.确保 content_main 中日历视图的 ID 是日历。

If you use Navigation Architecture Component , then you don't have to manually connect your fragments to your drawer MenuItem. 如果使用Navigation Architecture Component ,则不必手动将片段连接到抽屉MenuItem。 The system will automatically tie you MenuItem to specific fragment If the id of the MenuItem in your menu/xyz_menu.xml matches the id of the destination in navigation/xyz_nav.xml , the NavController can then navigate to that destination. The system will automatically tie you MenuItem to specific fragment如果menu/xyz_menu.xml中的MenuItem的ID与navigation/xyz_nav.xml中的目标的ID相匹配,则NavController可以导航至该目标。

Follow the correct steps mentioned in the official documentation : https://developer.android.com/guide/navigation/navigation-ui#Tie-navdrawer 请遵循官方文档中提到的正确步骤: https : //developer.android.com/guide/navigation/navigation-ui#Tie-navdrawer

Note : They have a sample app linked to the same article which you can refer. 注意:他们有一个示例应用程序链接到您可以参考的同一篇文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM