如何阻止图像在 android 应用程序上的 gridview 中移动位置

发布于 2024-11-05 08:58:51 字数 2535 浏览 1 评论 0原文

我有一个 GridView,它使用适配器来设置 ImageView,当我打开 AlertDialog 并关注其 TextView 时,GridView 中的图像会移动位置。我该如何阻止这种情况发生?

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter {
private Context context;
private String[] directory;

public ImageAdapter(Context c, String[] d) {
    this.context = c;
    this.directory = d;
}

public int getCount() {
    return directory.length;
}

public Object getItem(int position) {
    return directory[position];
}

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

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    System.gc();
    View v;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        LayoutInflater li = LayoutInflater.from(context);
        v = li.inflate(R.layout.iblock, null);

        ImageView iv = (ImageView)v.findViewById(R.id.foodImage);
        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inSampleSize = 2;
        iv.setImageBitmap(BitmapFactory.decodeFile(directory[position], bfo));

        TextView tv = (TextView)v.findViewById(R.id.foodName);
        String[] s = directory[position].split("[\\/.]+");

    } else {
        v = convertView;
    }
    return v;
}
}


protected Dialog onCreateDialog(int id) {
    switch(id) {
    case DIALOG_SEARCH_ID:
        LayoutInflater factory = LayoutInflater.from(this);
        View searchView = factory.inflate(R.layout.searchbar2, null);

        final EditText searchfield = (EditText) searchView.findViewById(R.id.searchfield);

        ImageButton ib = (ImageButton) searchView.findViewById(R.id.searchbutton);
        ib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), SearchResults.class);
                i.putExtra("query", searchfield.getText().toString());
                startActivity(i);
            }
        });

        AlertDialog ad = new AlertDialog.Builder(SearchResults.this)
            .setView(searchView)
            .create();
        Window win = ad.getWindow();
        win.setGravity(48);
        return ad;
    }
    return null;
}

I have a GridView that uses an adapter to set ImageViews and when I open up an AlertDialog and focus on its TextView, the images in the GridView shift positions. How do I stop that from happening?

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter {
private Context context;
private String[] directory;

public ImageAdapter(Context c, String[] d) {
    this.context = c;
    this.directory = d;
}

public int getCount() {
    return directory.length;
}

public Object getItem(int position) {
    return directory[position];
}

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

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    System.gc();
    View v;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        LayoutInflater li = LayoutInflater.from(context);
        v = li.inflate(R.layout.iblock, null);

        ImageView iv = (ImageView)v.findViewById(R.id.foodImage);
        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inSampleSize = 2;
        iv.setImageBitmap(BitmapFactory.decodeFile(directory[position], bfo));

        TextView tv = (TextView)v.findViewById(R.id.foodName);
        String[] s = directory[position].split("[\\/.]+");

    } else {
        v = convertView;
    }
    return v;
}
}


protected Dialog onCreateDialog(int id) {
    switch(id) {
    case DIALOG_SEARCH_ID:
        LayoutInflater factory = LayoutInflater.from(this);
        View searchView = factory.inflate(R.layout.searchbar2, null);

        final EditText searchfield = (EditText) searchView.findViewById(R.id.searchfield);

        ImageButton ib = (ImageButton) searchView.findViewById(R.id.searchbutton);
        ib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), SearchResults.class);
                i.putExtra("query", searchfield.getText().toString());
                startActivity(i);
            }
        });

        AlertDialog ad = new AlertDialog.Builder(SearchResults.this)
            .setView(searchView)
            .create();
        Window win = ad.getWindow();
        win.setGravity(48);
        return ad;
    }
    return null;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2024-11-12 08:58:51

您的View回收已损坏。当您获得要回收的 View 时,您仍然需要将数据绑定到它(在本例中,将图像放入 ImageView 中)。回收 View 时唯一可以跳过的部分是膨胀布局。

Your View recycling is broken. When you are given a View to recycle, you still need to bind your data to it (in this case, putting the image in the ImageView). The only part you can skip when recycling a View is inflating the layout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文