如何在android中平滑我的gridview滚动? [英] how to smooth my gridview scrolling in android?

查看:102
本文介绍了如何在android中平滑我的gridview滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中使用class用于gridview.我怎样才能通过viewholder或其他方法使我的gridview滚动平滑? 我只希望我的滚动gridview性能非常平稳.我使用这个gridview来显示图像和名称,并通过单击eacj项打开另一个活动以获取有关的详细信息.

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;
}
}

推荐答案

始终尝试对列表和网格使用ViewHolder模式,这会提高列表和网格的性能:

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;
}

这篇关于如何在android中平滑我的gridview滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆