当 GridView 为1列时没有任何问题当设置多列就出现图片错位重复问题
public class MainActivity extends AppCompatActivity { private GridView G1; private LruCache<String,Bitmap> Lru; String[] img ={ "http://pic1.win4000.com/pic/6/02/f5ae6d2ae4_250_350.jpg", "http://pic1.win4000.com/pic/1/29/381f2a6941_250_350.jpg", "http://pic1.win4000.com/pic/d/dc/95bb6142d5_250_350.jpg", "http://pic1.win4000.com/pic/7/54/b3b4112b34_250_350.jpg", "http://pic1.win4000.com/pic/d/87/642d5de424_250_350.jpg", "http://pic1.win4000.com/pic/c/8c/4d004a28f5_250_350.jpg", "http://pic1.win4000.com/pic/4/f8/9f2e1e129c_250_350.jpg", "http://pic1.win4000.com/pic/8/40/b90bd3f95f_250_350.jpg", "http://pic1.win4000.com/pic/d/99/e7b336f4e7_250_350.jpg", "http://pic1.win4000.com/pic/1/44/8b046cb009_250_350.jpg", "http://pic1.win4000.com/pic/2/19/38ac8d7ad0_250_350.jpg", "http://pic1.win4000.com/pic/2/87/73d1cbf175_250_350.jpg", "http://pic1.win4000.com/pic/7/c5/cd8f5c8d1b_250_350.jpg", "http://pic1.win4000.com/pic/0/6c/38c8f4e423_250_350.jpg", "http://pic1.win4000.com/pic/5/c1/7bb3ec563d_250_350.jpg", "http://pic1.win4000.com/pic/2/f5/abc84bceac_250_350.jpg" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); G1 = findViewById(R.id.Gridv); int max = (int) Runtime.getRuntime().maxMemory() / 1024; int siz = max / 5; Lru = new LruCache<String, Bitmap>(siz){ @Override protected int sizeOf(String key, Bitmap value) { return super.sizeOf(key, value) / 1024; } }; G1.setAdapter(new ShiPei()); } //适配器 class ShiPei extends BaseAdapter{ Img im; @Override public int getCount() { return img.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null){ convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.imgtu,null); im =new Img(); im.Iv = convertView.findViewById(R.id.imageView); convertView.setTag(im); }else { im = (Img) convertView.getTag(); } new YiBu(img[position]).execute(img[position]); return convertView; } class Img{ ImageView Iv; } class YiBu extends AsyncTask<String,Void, Bitmap>{ String url; public YiBu(String x){ url = x; } @Override protected Bitmap doInBackground(String... strings) { Bitmap bitmap; String u1 = strings[0]; bitmap = Lru.get(u1); if(bitmap == null){ bitmap = getBitmap(u1); Lru.put(u1,bitmap); } return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); im.Iv.setImageBitmap(bitmap); } public Bitmap getBitmap(String s){ Bitmap b = null; OkHttpClient client = new OkHttpClient(); Request R = new Request.Builder().url(s).build(); try { Response response = client.newCall(R).execute(); byte[] byt = response.body().bytes(); b = BitmapFactory.decodeByteArray(byt,0,byt.length); } catch (IOException e) { e.printStackTrace(); } return b; } } }
来个大佬看下 网上搜半天什么设置tag验证得方式 也尝试过 一样没用
慕村443891