how to smooth my gridview scrolling in android?

拥有回忆 提交于 2019-12-12 04:34:43

问题


i am use a class by below code for gridview. how i can smooth my gridview scrolling by viewholder or other method? i want just my scrolling gridview performance very smoothly. i use this gridview for show images and names and by click on eacj item open another activity for detail about.

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomGrid extends BaseAdapter{

private Context mContext;
private final String[] web;
private final int[] Imageid;


public CustomGrid(Context c, String[] web, int[] Imageid) {
    mContext = c;
    this.Imageid = Imageid;
    this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
    return web.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
    View grid;
    LayoutInflater inflater = (LayoutInflater)         mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    grid = new View(mContext);
    grid = inflater.inflate(R.layout.grid_item, null);
    TextView textView = (TextView) grid.findViewById(R.id.grid_text);
    ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
    textView.setText(web[position]);
    imageView.setImageResource(Imageid[position]);


    return grid;
}
}

回答1:


Always try to use ViewHolder Pattern for lists and girds this improves the performance of Lists and Grids :

    @Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder = null;
    if(convertView==null){
        viewHolder = new ViewHolder();
        convertView = inflater.inflate(R.layout.grid_item,parent,false);

        convertView.setTag(viewHolder);
    }
    else {
        viewHolder=(ViewHolder)convertView.getTag();
    }
    viewHolder.image=(ImageView)convertView.findViewById(R.id.thumbnail);
    viewHolder.title=(TextView)convertView.findViewById(R.id.title);
    viewHolder.title.setText(Html.fromHtml(values.getPosts().get(position).getTitle()));

    imageView.setImageResource(Imageid[position]);
    return convertView;
}

private static class ViewHolder{
    public static ImageView image;
    public static TextView title;
}


来源:https://stackoverflow.com/questions/42139486/how-to-smooth-my-gridview-scrolling-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!