2017-01-20 84 views
0

我在我的應用程序中使用Gridview顯示SD卡上的文件夾中的圖像... 我的問題是GridView中的滾動,它不像Gallery應用程序那樣流暢。 這裏的適配器代碼 -GridView滾動不光滑

public class GridViewAdapter extends BaseAdapter { 

    // Declare variables 
    private Activity activity; 
    private String[] filepath; 
    private String[] filename; 

    private static LayoutInflater inflater = null; 

    public GridViewAdapter(Activity a, String[] fpath, String[] fname) { 
     activity = a; 
     filepath = fpath; 
     filename = fname; 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    public int getCount() { 
     return filepath.length; 

    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 
     if (convertView == null) 

      vi = inflater.inflate(R.layout.gridview_item, null); 

     // Locate the ImageView in gridview_item.xml 
     ImageView img = (ImageView) vi.findViewById(R.id.image); 


     // Set file name to the TextView followed by the position 
     TextView txt = (TextView) vi.findViewById(R.id.name); 

     // Decode the filepath with BitmapFactory followed by the position 
     Bitmap bmp = BitmapFactory.decodeFile(filepath[position]); 

     // Set the decoded bitmap into ImageView 
     img.setImageBitmap(bmp); 
     txt.setText(filename[position]); 
     return vi; 
    } 
} 

如何來解決這個問題,使滾動順暢?

+1

您應該使用ViewHolder – Rachit

回答

1

你可以做這個。

  1. 實現異步圖像加載讓每一個圖像獲取加載在縮略圖大小異步
  2. 利用第三方ImageLoaders和負載的圖像。 For.ex UniversalImageLoader。檢查this更多詳細信息
  3. 您可以使用RecyclerViewGridLayoutManager而不是GridView,它是更優化的控制。爲前:http://www.android-examples.com/android-recyclerview-with-gridview-gridlayoutmanager/

附加參考:http://www.technotalkative.com/android-select-multiple-photos-from-gallery/

0

BitmapFactory.decodeFile是在UI線程一次又一次地進行繁重的操作。你已經使用了一些緩存庫。

您可以使用一些圖像加載庫,如Universal image loaderGlidepicasso

請使用持有人嘗試此操作。

class Holder{ 
ImageView img ; 
TextView txt; 
} 


public View getView(int position, View convertView, ViewGroup parent) {  
    Holder holder; 
    if (convertView == null) 
{ 
    convertView = inflater.inflate(R.layout.gridview_item, null); 
     holder=new Holder(); 

    holder.img = (ImageView) convertView.findViewById(R.id.image); 

    holder.txt = (TextView) convertView.findViewById(R.id.name); 

    convertView.setTag(holder) 
    } 
    else 
{ 
    holder=(Holder)convertView.getTag(); 
} 


    Picasso.with(activity) .load(filepath[position]) .into(holder.img); 
    txt.setText(filename[position]); 
    return convertView; 
} 
+0

我用畢加索,但它並沒有顯示images..my Imageviews是空白.. Picasso.with(活動) \t \t .load(文件路徑[位置]) \t (img); – DarShan

+0

嘗試實施這種使用方式,因爲我編輯我的答案。 – Ankur1994a

+0

我使用Glide,效果更好,然後畢加索 – DarShan