加載網格視圖時滾動不平滑。它需要一些時間來滾動。 那麼如何優化GridView,以便我可以在該網格視圖上放置更多圖像。請建議我一些方法來優化網格視圖。如何在Android中優化圖像GridView?
主要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>
主要活動
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(); } }); } }
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 }; }
如何在不看代碼的情況下提出優化建議?用兩個字:使用RecyclerView – Chaosit
好吧,我發送你到代碼 –
編輯和添加代碼,使用持有人,並不總是膨脹視圖,因爲它正在回收,所以你可以使用轉換視圖,也可以考慮使用Glide庫加載圖像,我會添加我的代碼。 – DAVIDBALAS1