GridView is not Scrolling smoothly

2019-08-29 16:55发布

问题:

I have 100 plus images present in drawable folder I am creating gallery in GridView, But facing scrolling issues. gridView is not scrolling smoothly although I have used ViewHolder...

here is my code...

public class MainActivity extends Activity {
int[100] Names_images =    {R.drawable.img1,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6,...};//100 images

  String[100] names = {"name1","name2","name3","name4","name5","name6",...};// 100 names    
GridView gridview; 
CustomizedGridAdapter1 adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

gridview = (GridView) findViewById(R.id.gridView);

adapter = new CustomizedGridAdapter1();
gridview.setAdapter(adapter);

}


 static class viewHolderClass{
     TextView Name;
     ImageView name_img;
     TextView Number;
    }
         public class CustomizedGridAdapter1 extends BaseAdapter {

    LayoutInflater inflater=null; 
    @Override
    public int getCount() {
        return names.length;
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

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

        viewHolderClass viewHolder=null;


        if(v == null){
        inflater = (LayoutInflater)          (MainActivity.this).getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         v = inflater.inflate(R.layout.gridblock,parent, false);

         viewHolder = new viewHolderClass();

         viewHolder.Name = (TextView)v.findViewById(R.id.name);
         viewHolder.name_img = (ImageView)v.findViewById(R.id.name_image);
         viewHolder.Number = (TextView)v.findViewById(R.id.number);

         v.setTag(viewHolder);
         }else{
             viewHolder = (viewHolderClass)v.getTag();
         }

    viewHolder.Name.setText(names[position]);
    viewHolder.name_img.setImageResource(Names_images[position]);
    viewHolder.Number.setText(String.valueOf(position+1));  


        return v;
    }
  }//end of outer Class

回答1:

Try using image lazzy loader like:

https://github.com/nostra13/Android-Universal-Image-Loader



回答2:

Change your getItem as below:

@Override
public Object getItem(int position) {

    return names[position];
}