为什么日历在导航抽屉不工作? [英] Why is Calendar in Navigation Drawer not working?

查看:174
本文介绍了为什么日历在导航抽屉不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的日历代码

  public class Calender extends android.support.v4.app.Fragment {
CalendarView日历;
public Calender(){
//必需的空公共构造函数
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState ){
super.onCreate(savedInstanceState);
查看fragmentRootView = inflater.inflate(R.layout.fragment_calender,container,false);

calendar =(CalendarView)fragmentRootView.findViewById(R.id.calendar);
calendar.setShowWeekNumber(false);
calendar.setFirstDayOfWeek(2);
calendar.setSelectedWeekBackgroundColor(getResources()。getColor(R.color.green));
calendar.setUnfocusedMonthDateColor(getResources()。getColor(R.color.transparent));
calendar.setWeekSeparatorLineColor(getResources()。getColor(R.color.transparent));
calendar.setSelectedDateVerticalBar(R.color.darkgreen);
calendar.setOnDateChangeListener(new OnDateChangeListener(){
@Override
public void onSelectedDayChange(CalendarView view,int year,int month,int day){
Toast.makeText(getApplicationContext ),day +/+ month +/+ year,Toast.LENGTH_LONG).show();
}
}

return fragmentRootView;
}

}

getApplicationContext (),
,下面是MainActivity.java上的代码,我试图创建一个片段,以便从导航栏打开日历

  navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(MenuItem item){
switch(item.getItemId b $ b {
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;
}
});

我已经为日历创建了一个新的xml文件,我在主xml中使用框架布局。

 <?xml version =1.0encoding =utf-8?& 

 $ c>< RelativeLayout 
android:layout_width =match_parent
android:layout_height =match_parent>

< android.support.design.widget.TabLayout
android:id =@ + id / tab_layout
android:layout_width =match_parent
:layout_height =wrap_content
android:layout_alignParentLeft =true
android:layout_alignParentRight =true
android:background =@ color / colorPrimary
android:minHeight = ?attr / actionBarSize
app:tabGravity =fill
app:tabIndicatorColor =@ color / colorAccent
app:tabSelectedTextColor =@ color / tabTextColor
:tabTextColor =#FFF>

< /android.support.design.widget.TabLayout>

< android.support.v4.view.ViewPager
android:id =@ + id / view_pager
android:layout_width =match_parent
:layout_height =match_parent
android:layout_alignParentBottom =true
android:layout_below =@ + id / tab_layout>



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

< ListView
android:layout_width =wrap_content
android:layout_height =wrap_content
android:id =@ + id / lvPump
android:layout_centerHorizo​​ntal =true
android:layout_below =@ + id / tab_layout/>




< / RelativeLayout>
< FrameLayout
android:layout_width =match_parent
android:layout_height =match_parent
android:id =@ + id / main_container>< / FrameLayout> ;

< android.support.design.widget.NavigationView
android:id =@ + id / navigation_view
android:layout_width =wrap_content
:layout_height =match_parent
android:layout_gravity =start
app:headerLayout =@ layout / navigation_header
app:menu =@ menu / navigation_item>

< /android.support.design.widget.NavigationView>

解决方案>

在Fragment中没有方法 getApplicationContext()。如果要访问本地上下文,请写 getActivity()而不是 getApplicationContext()


The following is my code for my Calendar

public class Calender extends android.support.v4.app.Fragment {
CalendarView calendar;
public Calender() {
    // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
   View fragmentRootView = inflater.inflate(R.layout.fragment_calender, container, false);

    calendar = (CalendarView) fragmentRootView.findViewById(R.id.calendar);
    calendar.setShowWeekNumber(false);
    calendar.setFirstDayOfWeek(2);
    calendar.setSelectedWeekBackgroundColor(getResources().getColor(R.color.green));
    calendar.setUnfocusedMonthDateColor(getResources().getColor(R.color.transparent));
    calendar.setWeekSeparatorLineColor(getResources().getColor(R.color.transparent));
    calendar.setSelectedDateVerticalBar(R.color.darkgreen);
    calendar.setOnDateChangeListener(new OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
            Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
        }
    });

    return fragmentRootView;
}

}

There is an error in the getApplicationContext() , and the following is the code on MainActivity.java, im trying to create a fragment in order to open a calendar from a navigation drawer

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;
        }
    });

I have created a new xml file for the calendar too, and am using frame layout in the main xml.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabSelectedTextColor="@color/tabTextColor"
        app:tabTextColor="#FFF">

    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/tab_layout">



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

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lvPump"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/tab_layout" />




</RelativeLayout>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_container"></FrameLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/navigation_header"
    app:menu="@menu/navigation_item">

</android.support.design.widget.NavigationView>

解决方案

There is no method getApplicationContext() in Fragment. If you want to get access to local context, write getActivity() instead of getApplicationContext().

这篇关于为什么日历在导航抽屉不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆