Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are ViewPager and GridLayout with 30 Elements very slow?

I have a ViewPager and a GridView to show a monthly calendar. The GridView has about 30 elements and swipe to left and right is very slow. I made a sample project to test without any database access and it is slow, too. Maybe someone sees the problem or do I have to make an asynchronous load of the pages?

That is the layout for one element in the GridView. I want to show 9 little 5x5 pixel icons in any element, but without them, it is slow, too.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Layout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="1dp">
  <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
    <TextView android:id="@+id/date" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="5dp" android:gravity="center_vertical" android:textSize="14sp" android:textStyle="bold"/>
  </LinearLayout>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
    <ImageView android:id="@+id/dot1" android:layout_width="wrap_content" android:drawingCacheQuality="auto" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="#ffffff" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:paddingBottom="1dp" android:paddingLeft="1dp" android:paddingTop="5dp"/>
  </RelativeLayout>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
    <ImageView android:id="@+id/dot2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
  </RelativeLayout>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
    <ImageView android:id="@+id/dot3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_gravity="right" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
  </RelativeLayout>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
    <ImageView android:id="@+id/dot4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_gravity="center_horizontal" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
  </RelativeLayout>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
    <ImageView android:id="@+id/dot5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_gravity="left" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
  </RelativeLayout>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
    <ImageView android:id="@+id/dot6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_gravity="left" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
  </RelativeLayout>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
    <ImageView android:id="@+id/dot7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_gravity="left" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
  </RelativeLayout>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
    <ImageView android:id="@+id/dot8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_gravity="left" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
  </RelativeLayout>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
    <ImageView android:id="@+id/dot9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_gravity="left" android:background="#ffffff" android:paddingBottom="5dp" android:paddingLeft="1dp"/>
  </RelativeLayout>
</LinearLayout>

That's the instantiateItem of GridView adapter:

public Object instantiateItem(ViewGroup container, int position) {
    GridView gv = new GridView(m_context);
    gv.setAdapter(new BaseAdapter() {
        @Override
        public View getView(int pos, View convertView, ViewGroup parent) {
            View v;
            if (convertView == null) {
                // if it's not recycled, initialize some attributes
                v = mInflater.inflate(R.layout.calendar_month_item, null);
            } else {
                v = convertView;
            }
            TextView dayView = (TextView) v.findViewById(R.id.date);
            dayView.setText(pos + "");
            return v;
        }

        @Override
        public long getItemId(int p) {
            return 0;
        }

        @Override
        public Object getItem(int p) {
            return null;
        }

        @Override
        public int getCount() {
            return 30;
        }
    });
    gv.setNumColumns(5);
    ((ViewPager) container).addView(gv);
    return gv;
}
like image 885
MOST2K2 Avatar asked Jun 11 '12 05:06

MOST2K2


1 Answers

I looked around for a bit and I think I found exactly what you are looking for:

  • content XML, you will probably have to change or redo your layout as it is turns out quite large, as you said.
  • CalendarAdapter.java, in line 112 you can see that they added an icon there. Just change the code around so you can add your nine icons programmatically instead of having all of these layouts.
  • How to implement CalendarView in Android
like image 98
CommonKnowledge Avatar answered Oct 19 '22 01:10

CommonKnowledge