【发布时间】:2016-12-14 21:16:11
【问题描述】:
我有一个包含 GridView 的片段
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:orientation="vertical" >
<GridView
android:padding="20dp"
android:id="@+id/sub_category_gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="20dp"
android:numColumns="4"
android:verticalSpacing="10dp"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:fastScrollEnabled="true"
tools:listitem="@layout/sub_category_view" />
</LinearLayout>
这个 GridView 的每个子视图都包含一个 TextView 和一个 ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/sub_category_view">
<ir.motorel.carbuyinstruduction.SquareLinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/button">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/picture1"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_margin="10dp"
android:id="@+id/sub_category_img"/>
</ir.motorel.carbuyinstruduction.SquareLinearLayout>
<TextView
android:layout_marginTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:gravity="center"
android:textColor="#C36518"
android:id="@+id/sub_category_name"/>
</LinearLayout>
当我用文本和图像填充 GridView 时,滚动时它变得非常迟钝和缓慢。这是我的适配器类:
public class CustomGrid extends BaseAdapter{
private Context mContext;
public CustomGrid(Context c) {
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return subCatTitle.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.sub_category_view, parent, false);
viewHolder = new ViewHolder();
viewHolder.subCatView = (LinearLayout) convertView.findViewById(R.id.sub_category_view);
viewHolder.subCatImg = (ImageView) convertView.findViewById(R.id.sub_category_img);
viewHolder.subCatTitle = (TextView) convertView.findViewById(R.id.sub_category_name);
//subCatImg.setImageResource(imgIDs.getResourceId(position, -1));
convertView.setTag(viewHolder);
}
else{
//imgIDs.recycle();
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.subCatTitle.setText(subCatTitle[position]);
return convertView;
}
}
public class ViewHolder{
public TextView subCatTitle;
public ImageView subCatImg;
public LinearLayout subCatView;
}
如何优化它以更流畅地滚动?
PS。我的图片在 drawable 文件夹中,它们的格式是 PNG。
【问题讨论】:
-
在您的
adapterClass中创建一个持有者类,并在您的getView中使用它。只需将 google baseadapter 与持有者类一起使用。一定会给你一些好的结果 -
@Mike 感谢您的评论...我发现了一些关于
ASyncTask的信息,但我发现的大部分代码都是关于从 SD 卡或互联网加载图像...是否可以使用AsyncTask对于存储在可绘制文件夹中的图像? -
取决于您的
AsyncTask的doInBackground运行了多长时间。通常 asynctask 仅用于后台的短期进程! -
看看这个stackoverflow.com/questions/12797550/… .... 获得您想要的专业知识 ;)...
-
@Mike 我使用了 ViewHolder,它变得更平滑但不够平滑......我发现了这个developer.android.com/training/improving-layouts/…......但我不知道我的代码的哪一部分,我应该添加@ 987654332@! :(
标签: android gridview android-asynctask