加载网格视图时滚动不平滑。滚动需要一些时间。 那么我如何优化gridview,以便我可以在该网格视图上放置更多图像。请告诉我一些优化网格视图的方法。
主Xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="180dp"
android:stretchMode="columnWidth"
android:id="@+id/imggrid">
</GridView>
</RelativeLayout>
主要活动
package com.blogspot.techyy360.ukook_splash;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_grid);
GridView gridView=(GridView) findViewById(R.id.imggrid);
gridView.setAdapter(new CustomAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),"You clicked on Recipie "+position, Toast.LENGTH_LONG).show();
}
});
}
}
用于图像网格视图的CustomAdapter
package com.blogspot.techyy360.ukook_splash;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
public class CustomAdapter extends BaseAdapter{
private Context mcontext;
@Override
public int getCount() {
return img.length;
}
@Override
public Object getItem(int position) {
return img[position];
}
@Override
public long getItemId(int position) {
return 0;
}
public CustomAdapter(Context c){
mcontext=c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgview;
if(convertView==null){
imgview=new ImageView(mcontext);
imgview.setLayoutParams(new RelativeLayout.LayoutParams(525, 400));
imgview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgview.setPadding(8, 8, 8, 8);
}else{
imgview=(ImageView) convertView;
}
imgview.setImageResource(img[position]);
return imgview;
}
public Integer[] img={
R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image3,
R.drawable.image1, R.drawable.image2,
R.drawable.image2, R.drawable.image3,
R.drawable.image3, R.drawable.image1,
R.drawable.image2, R.drawable.image1,
R.drawable.image2, R.drawable.image1,
R.drawable.image3, R.drawable.image1
};
}
答案 0 :(得分:0)
这是我的完整CustomAdapter代码,使用滑动库。它是快速和流畅的,但是,根据您的需要,考虑更改加载图像时使用的diskCacheStartegy
。
import android.content.Context;
import android.graphics.Point;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
public class CustomAdapter3 extends BaseAdapter {
Context context;
public static int[] imageId;
private static LayoutInflater inflater = null;
public CustomAdapter3(Context con, int[] prgmImages) {
context = con;
imageId = prgmImages;
inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return imageId.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder {
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.program_list3, parent, false);
holder = new Holder();
holder.img = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
Glide
.with(context)
.load(imageId[position])
.dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.placeholder(R.drawable.loading_spinner)
.into(holder.img);
return convertView;
}
}