Android-优化GridView滚动
我有一个包含GridView的片段Android-优化GridView滚动,android,gridview,android-asynctask,Android,Gridview,Android Asynctask,我有一个包含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
<?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>
如何对其进行优化以使其滚动更平滑
另外,我的图像位于drawable文件夹中,格式为PNG。我的
活动
的标题中有一个图像视图
,我为它设置了一个非常大的图像。这是一场精彩的表演。GridView
中没有问题,请在adapterClass
中创建一个holder类,并在getView
中使用它。只需将GoogleBaseAdapter与holder类一起使用即可。我必须给你一些好处results@Mike谢谢你的评论。。。我找到了一些关于ASyncTask
的代码,但我找到的大多数代码都是关于从SD卡或internet加载图像的。。。是否可以对存储在drawable文件夹中的图像使用AsyncTask
?取决于AsyncTask
的doInBackground
运行的时间。一般来说,异步任务只适用于后台的短期进程!看看这个。。。。要获得您想要的技术;)@Mike我用了ViewHolder,它变得更平滑,但不够平滑。。。我发现这个。。。但是我不知道我的代码的哪一部分,我应该添加AsyncTask
!:(
<?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>
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;
}