2016-07-13 60 views
-2

加載網格視圖時滾動不平滑。它需要一些時間來滾動。 那麼如何優化GridView,以便我可以在該網格視圖上放置更多圖像。請建議我一些方法來優化網格視圖。如何在Android中優化圖像GridView?

  1. 主要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> 
    
  2. 主要活動

    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(); 
    
        } 
    }); 
        } 
        } 
    
  3. 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

如何在不看代碼的情況下提出優化建議?用兩個字:使用RecyclerView – Chaosit

+0

好吧,我發送你到代碼 –

+0

編輯和添加代碼,使用持有人,並不總是膨脹視圖,因爲它正在回收,所以你可以使用轉換視圖,也可以考慮使用Glide庫加載圖像,我會添加我的代碼。 – DAVIDBALAS1

回答

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

請在我的代碼中進行更改 –

+0

@AmitKumawat我給你一個適配器的完整代碼,使用持有者和滑行,只需將它實現到你的代碼中......我不是你的老師。 – DAVIDBALAS1