Mam ViewPager
i GridView
, aby wyświetlić kalendarz miesięczny. Model GridView
ma około 30 elementów, a przesunięcie w lewo i prawo jest bardzo wolne. Zrobiłem przykładowy projekt do przetestowania bez dostępu do bazy danych i jest on powolny. Może ktoś widzi problem lub muszę wykonać asynchroniczne ładowanie stron? Jest to układ dla jednego elementu w GridView
. Chcę pokazać 9 małych ikon 5x5 pikseli w dowolnym elemencie, ale bez nich też jest wolny.Dlaczego ViewPager i GridLayout z 30 elementami są bardzo wolne?
<?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>
To instantiateItem
z GridView
adaptera:
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;
}
Użyj funkcji Traceview, aby określić, gdzie znajdują się wąskie gardła. – CommonsWare
Zajrzałem do TraceView, ale nie mogę zobaczyć, gdzie jest problem, jego jedyną reprezentację UI. – MOST2K2
Zawijacie Państwo ImageViews wewnątrz RelativeLayouts i TextView wewnątrz LinearLayout: To zupełnie niepotrzebne. Po drugie, masz wiele widoków GridView ... Czy masz wiele Adaptery? nie potrzebujesz wszystkich adapterów. Możesz zarządzać wszystkim w jednym adapterze! Wreszcie, po prostu kontemplując, nie potrzebujesz ViewPagera. Dzięki prostym animacjom możesz to zrobić za pomocą pojedynczego GridView (i za każdym razem jeden reset adaptera). Powodzenia –