Java Gridview未完全显示,并且在开始处有空白对象
试图创建gridview,但遇到了一些问题。gridview显示不清楚,即使屏幕上还有剩余空间,我也必须在其布局内滚动。它还显示其中的空项目。 下面是一张它看起来像什么的图片: 我不明白为什么在实际图像弹出之前会有这些空对象。这是我的密码: activity_main.xml:Java Gridview未完全显示,并且在开始处有空白对象,java,android,android-studio,android-layout,gridview,Java,Android,Android Studio,Android Layout,Gridview,试图创建gridview,但遇到了一些问题。gridview显示不清楚,即使屏幕上还有剩余空间,我也必须在其布局内滚动。它还显示其中的空项目。 下面是一张它看起来像什么的图片: 我不明白为什么在实际图像弹出之前会有这些空对象。这是我的密码: activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/places"
android:gravity="center"
android:padding="20dp"/>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/grid"
android:numColumns="3"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="@string/button" />
</LinearLayout>
MainActivity.java:
package com.example.placesofinterest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import java.util.ArrayList;
import static android.provider.Contacts.SettingsColumns.KEY;
public class MainActivity extends AppCompatActivity {
GridView gridView;
GridAdapter gridAdapter;
private final int THUMBNAIL_SIZE = 250;
ArrayList<Bitmap> img = new ArrayList<Bitmap>();
private String thumbURL[] = {"http://192.168.0.105/places/amsterdam_thumb.png",
"http://192.168.0.105/places/bali_thumb.png", "http://192.168.0.105/places/dubai_thumb.png",
"http://192.168.0.105/places/germany_thumb.png", "http://192.168.0.105/places/cambodia_thumb.png",
"http://192.168.0.105/places/ireland_thumb.png", "http://192.168.0.105/places/japan_thumb.png",
"http://192.168.0.105/places/prague_thumb.png", "http://192.168.0.105/places/sweden_thumb.png"};
private String fullURL[] = {"http://192.168.0.105/places/amsterdam.png",
"http://192.168.0.105/places/bali.png", "http://192.168.0.105/places/dubai.png",
"http://192.168.0.105/places/germany.png", "http://192.168.0.105/places/cambodia.png",
"http://192.168.0.105/places/ireland.png", "http://192.168.0.105/places/japan.png",
"http://192.168.0.105/places/prague.png", "http://192.168.0.105/places/sweden.png"};
private String location[] = {"Amsterdam", "Bali", "Dubai", "Germany", "Cambodia", "Ireland",
"Japan", "Prague", "Sweden"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i = 0; i < fullURL.length; i++) {
LoadingImages(fullURL[i]);
}
}
private void LoadingImages(String s) {
ImageLoader imageLoader = MySingleton.getInstance(getApplicationContext()).getImageLoader();
imageLoader.get(s, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
Bitmap image = response.getBitmap();
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(image, THUMBNAIL_SIZE, THUMBNAIL_SIZE);
img.add(thumbnail);
gridView = findViewById(R.id.grid);
gridAdapter = new GridAdapter(MainActivity.this, img);
gridView.setAdapter(gridAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error", error.getMessage());
}
});
}
}
package com.example.placesofinterest;
导入androidx.appcompat.app.appcompat活动;
导入android.content.Intent;
导入android.graphics.Bitmap;
导入android.media.ThumbnailUtils;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.GridView;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.ImageLoader;
导入java.util.ArrayList;
导入静态android.provider.Contacts.SettingsColumns.KEY;
公共类MainActivity扩展了AppCompatActivity{
GridView;
网格适配器;
专用最终整型缩略图大小=250;
ArrayList img=新的ArrayList();
私有字符串thumbURL[]={”http://192.168.0.105/places/amsterdam_thumb.png",
"http://192.168.0.105/places/bali_thumb.png", "http://192.168.0.105/places/dubai_thumb.png",
"http://192.168.0.105/places/germany_thumb.png", "http://192.168.0.105/places/cambodia_thumb.png",
"http://192.168.0.105/places/ireland_thumb.png", "http://192.168.0.105/places/japan_thumb.png",
"http://192.168.0.105/places/prague_thumb.png", "http://192.168.0.105/places/sweden_thumb.png"};
私有字符串fullURL[]={”http://192.168.0.105/places/amsterdam.png",
"http://192.168.0.105/places/bali.png", "http://192.168.0.105/places/dubai.png",
"http://192.168.0.105/places/germany.png", "http://192.168.0.105/places/cambodia.png",
"http://192.168.0.105/places/ireland.png", "http://192.168.0.105/places/japan.png",
"http://192.168.0.105/places/prague.png", "http://192.168.0.105/places/sweden.png"};
私有字符串位置[]={“阿姆斯特丹”、“巴厘岛”、“迪拜”、“德国”、“柬埔寨”、“爱尔兰”,
“日本”、“布拉格”、“瑞典”};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i
GridAdapter.java:
package com.example.placesofinterest;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import java.util.ArrayList;
public class GridAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private ArrayList<Bitmap> img;
public GridAdapter(Context c, ArrayList<Bitmap> img){
this.context = c;
this.img = img;
}
@Override
public int getCount() {
return img.size();
}
@Override
public Object getItem(int position) {
return img.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.places_layout, null);
MyViewHolder myViewHolder = new MyViewHolder(convertView);
convertView.setTag(myViewHolder);
}
MyViewHolder myViewHolder = (MyViewHolder)convertView.getTag();
myViewHolder.img.setImageBitmap(img.get(position));
return convertView;
}
public class MyViewHolder {
public ImageView img;
public MyViewHolder(View view) {
img = view.findViewById(R.id.imageView);
}
}
}
package com.example.placesofinterest;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.ImageView;
导入java.util.ArrayList;
公共类GridAdapter扩展了BaseAdapter{
私人语境;
私人充气机;
私人ArrayList img;
公共GridAdapter(上下文c、ArrayList img){
this.context=c;
this.img=img;
}
@凌驾
public int getCount(){
返回img.size();
}
@凌驾
公共对象getItem(int位置){
返回img.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
充气器=充气器。从(上下文);
convertView=充气机。充气(R.layout.places\U布局,空);
MyViewHolder MyViewHolder=新的MyViewHolder(convertView);
setTag(myViewHolder);
}
MyViewHolder MyViewHolder=(MyViewHolder)convertView.getTag();
myViewHolder.img.setImageBitmap(img.get(position));
返回视图;
}
公共类MyViewHolder{
公共图像视图img;
公共MyViewHolder(视图){
img=view.findviewbyd(R.id.imageView);
}
}
}
places_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/avatars" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/wish" />
</LinearLayout>
MySingleton.java:
package com.example.placesofinterest;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class MySingleton {
private static MySingleton instance;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private static Context context;
private MySingleton(Context context){
this.context = context;
requestQueue = Volley.newRequestQueue(context);
imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> cache = new LruCache<>(6);
@Override
public Bitmap getBitmap(String url) {
Bitmap bmp = cache.get(url);
if (bmp == null) {
System.out.println("Not in cache");
} else {
System.out.println("In cache");
}
return bmp;
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
System.out.println("Put in cache");
cache.put(url, bitmap);
}
});
}
public static synchronized MySingleton getInstance(Context context) {
if (instance == null) {
instance = new MySingleton(context);
}
return instance;
}
public ImageLoader getImageLoader() {
return imageLoader;
}
}
package com.example.placesofinterest;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.util.LruCache;
导入com.android.volley.RequestQueue;
导入com.android.volley.toolbox.ImageLoader;
导入com.android.volley.toolbox.volley;
公共课米辛格尔顿{
私有静态MySingleton实例;
私有请求队列请求队列;
私有图像加载器;
私有静态语境;
private MySingleton(上下文){
this.context=上下文;
requestQueue=Volley.newRequestQueue(上下文);
imageLoader=新的imageLoader(请求)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout> ...
</ScrollView>