我是Android开发的新手,所以我的担忧可能存在根本性的缺陷。我已经搜索了有关这方面的详细信息,并且找不到多少。
我的自定义CursorAdapter
为View
中的每个订单项创建了一个新的Cursor
对象,而不是根据可见列表项的数量创建View
个对象+ 2 *用于滚动支持和重用的列。
它似乎源于CursorAdapter
中实施的getView方法。使用Cursor
对象中每个行项目的null View参数调用该方法,强制每次调用newView()
。
这是正常行为吗? CursorAdapter
会造成大量内存占用,从而导致OOM异常。
这是完整的适配器:
public class MovieCursorAdapter extends CursorAdapter{
private final String LOG_TAG = MovieCursorAdapter.class.getSimpleName();
public MovieCursorAdapter(Context context, Cursor cursor, int flags){
super(context,cursor,flags);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.fragment_main_image_view,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
String url = cursor.getString(cursor.getColumnIndex(MoviesContract.MovieEntry.COLUMN_POSTER_PATH));// TODO continue
try {
Picasso.with(context).setIndicatorsEnabled(true);
Picasso.with(context).load(url).noFade().into(viewHolder.imageView);
} catch(Exception e) {
Log.e(LOG_TAG,"Error loading image:" +e.toString());
}
}
static class ViewHolder {
@BindView(R.id.imageview_movies)
ImageView imageView;
ViewHolder(View view){
ButterKnife.bind(this,view);
}
}
}
GridView的:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activity.MainActivity$MovieGridFragment">
<GridView
android:id="@+id/gridview_movies"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="@dimen/grid_image_view_movie_width"
android:verticalSpacing="@dimen/grid_image_view_movie_spacing"
/>
</FrameLayout>
ImageView项目:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageview_movies"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>