Android CalendarView slowing down layout

前端 未结 6 1315
生来不讨喜
生来不讨喜 2020-12-15 06:24

This is driving me crazy. I have a fragment in my Android app which is laid out using a RelativeLayout. Problem is that for some reason it takes ages to render, about 5 seco

相关标签:
6条回答
  • 2020-12-15 07:15

    It seems like CalendarView directly inside a RelativeLayout really doesn't work that well. The following solution with the trick of using a FrameLayout is loading much faster in the emulator:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20dp" >
    
        <TextView
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/time"
            android:text="TODAY"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />
    
        <TextView
            android:id="@id/time"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:gravity="right"
            android:text="11:32"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />
    
        <TextView
            android:id="@+id/date_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/title"
            android:layout_margin="20dp"
            android:text="Wednesday, 15th August 2012"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    
        <FrameLayout
            android:layout_width="250dp"
            android:layout_height="250dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true" >
    
            <CalendarView
                android:id="@+id/calendar_view"
                android:layout_width="250dp"
                android:layout_height="250dp"
                android:background="@drawable/white_back_black_border" />
        </FrameLayout>
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-15 07:17

    The above answers were right but, still hasn't solved one mystery.

    That is, When i want to include calendarview inside layout with other layout. It's just disappears or i can see only Week letters. I tried the above match parent and everything and still i couldn't solve it.

    If anyone struggling like me Here's the answer.

       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical">
    
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <CalendarView
            android:id="@+id/calendarService"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/linearLayout"></CalendarView>
    
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical"> 
    
    
         ///Your views 
    
    
         />
    
    0 讨论(0)
  • 2020-12-15 07:21

    I've had this problem too, both with relative layouts and linear layouts. It doesn't seem to be layout-related.

    Here is an example to see the error, just with a simple layout, no code at all:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".FechaHoraActivity" >
    
        <TimePicker
            android:id="@+id/timePicker1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp" />
    
        <CalendarView
            android:id="@+id/calendarView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    

    And now just change the linear layout height to:

    android:layout_height="match_parent"
    

    With this change the problem is gone, at least in my Samsung Galaxy Tab 2

    So it seems it is more "space" related, but I'm still not sure why this problem appears. And I haven't found any other interesting results on google searching "calendarview slow"...

    EDIT Let's see if we can find an answer with a small bounty

    0 讨论(0)
  • 2020-12-15 07:21

    Found that CalendarView widget needs height to be set to match_parent, then you can put it to any layout. For example in relative with height 200. This works fine on 4.0.3 emulator and on Samsung Galaxy Tab with 4.2.2.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true" >
    
            <CalendarView
                android:id="@+id/cv_dialog_filter_date"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </RelativeLayout>
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/relativeLayout2"
            android:layout_centerHorizontal="true"
            android:text="05.12.2013" />
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-15 07:23

    Putting the CalendarView in a FrameLayout will solve the problem.. here is a XML code example:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#cc00b1cc">
    
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="102dp"
            android:layout_alignParentBottom="true">
    
            <CalendarView
                android:layout_width="425dp"
                android:layout_height="374dp"
                android:id="@+id/calendarView"
                android:layout_gravity="left|top" />
        </FrameLayout>
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-15 07:30

    I take some hours to find the answer, but not figure out why. Here is something strange, may it can help you to find the answer.

    the cpu consumption when CalendarView slow enter image description here

    The getView in WeekAdapter has a param parent that was never used.

       public View getView(int position, View convertView, ViewGroup parent) {
           WeekView weekView = null;
            if (convertView != null) {
                weekView = (WeekView) convertView;
            } else {
                weekView = new WeekView(mContext);
                android.widget.AbsListView.LayoutParams params =
                    new android.widget.AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT);
                weekView.setLayoutParams(params);
                weekView.setClickable(true);
                weekView.setOnTouchListener(this);
            }
    
            int selectedWeekDay = (mSelectedWeek == position) ? mSelectedDate.get(
                    Calendar.DAY_OF_WEEK) : -1;
            weekView.init(position, selectedWeekDay, mFocusedMonth);
    
            return weekView;
        }
    

    Looking forward the answer.

    0 讨论(0)
提交回复
热议问题