方向改变时图像空白?

发布于 2024-10-24 21:17:28 字数 1944 浏览 1 评论 0原文

我想问为什么我的网格视图在方向更改后无法显示图像

我设置了 onConfigurationChange() 和 onRetainNonCofigurationInstance() 但没有解决我的问题。

这是我的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category_layout);

    data = getLastNonConfigurationInstance();

    setUpView();


}

private void setUpView() {
    gridview = (GridView) findViewById(R.id.gridlayout);
    if(data == null){
        imageGridViewAdapter = new ImageGridViewAdapter(this, category, image_category);
    }
    else{
        imageGridViewAdapter = (ImageGridViewAdapter) data;
    }
    gridview.setAdapter(imageGridViewAdapter);

    gridview.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1,
                final int arg2, long arg3) {

            if (subCategory[arg2].length > 1) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        CategoryActivity.this);
                builder.setTitle(category[arg2]);
                builder.setItems(subCategory[arg2],
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int item) {
                                linkNewsCategory(arg2, item);
                            }
                        });
                builder.show();
            } else {
                linkNewsCategory(arg2, 0);
            }

        }

    });
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setUpView();
}



@Override
public ImageGridViewAdapter onRetainNonConfigurationInstance() {
    ImageGridViewAdapter a = new ImageGridViewAdapter(this, category, image_category);
    return a;
}

这是 gridview 上的错误吗?

I want to ask why my grid view can't show image after orientation change

I have set onConfigurationChange() and onRetainNonCofigurationInstance() but not resolve my problem.

this my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category_layout);

    data = getLastNonConfigurationInstance();

    setUpView();


}

private void setUpView() {
    gridview = (GridView) findViewById(R.id.gridlayout);
    if(data == null){
        imageGridViewAdapter = new ImageGridViewAdapter(this, category, image_category);
    }
    else{
        imageGridViewAdapter = (ImageGridViewAdapter) data;
    }
    gridview.setAdapter(imageGridViewAdapter);

    gridview.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1,
                final int arg2, long arg3) {

            if (subCategory[arg2].length > 1) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        CategoryActivity.this);
                builder.setTitle(category[arg2]);
                builder.setItems(subCategory[arg2],
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int item) {
                                linkNewsCategory(arg2, item);
                            }
                        });
                builder.show();
            } else {
                linkNewsCategory(arg2, 0);
            }

        }

    });
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setUpView();
}



@Override
public ImageGridViewAdapter onRetainNonConfigurationInstance() {
    ImageGridViewAdapter a = new ImageGridViewAdapter(this, category, image_category);
    return a;
}

Is it bug on gridview?

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

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

发布评论

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

评论(1

东北女汉子 2024-10-31 21:17:28

终于我找到了解决方案..
我在设置图像和文本时有错误的适配器..

这是我的旧 getView() :

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        v = mInflater.inflate(R.layout.category, null);
        holder = new ViewHolder();
        holder.category_title = (TextView) v.findViewById(R.id.text);
        holder.category_image = (ImageView) v.findViewById(R.id.image);
        v.setTag(holder);
    } else{
        holder = (ViewHolder) v.getTag();
    }

    holder.category_title.setText(data[position]);
    holder.category_image.setImageResource(image[position]);

    return v;
}

我用这个进行更改:

public View getView(int position, View convertView, ViewGroup parent) {

    v = convertView;
    if(convertView == null){
        LayoutInflater li = activity.getLayoutInflater();
        v = li.inflate(R.layout.category, null);

        category_title = (TextView) v.findViewById(R.id.text);
        category_title.setText(data[position]);

        category_image = (ImageView) v.findViewById(R.id.image);
        category_image.setImageResource(image[position]);

    }
    return v;
}

现在它可以解决我的问题..

谢谢

finally i have find solution..
i have wrong adapter while set image and text..

this my old getView() :

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        v = mInflater.inflate(R.layout.category, null);
        holder = new ViewHolder();
        holder.category_title = (TextView) v.findViewById(R.id.text);
        holder.category_image = (ImageView) v.findViewById(R.id.image);
        v.setTag(holder);
    } else{
        holder = (ViewHolder) v.getTag();
    }

    holder.category_title.setText(data[position]);
    holder.category_image.setImageResource(image[position]);

    return v;
}

i change with this one:

public View getView(int position, View convertView, ViewGroup parent) {

    v = convertView;
    if(convertView == null){
        LayoutInflater li = activity.getLayoutInflater();
        v = li.inflate(R.layout.category, null);

        category_title = (TextView) v.findViewById(R.id.text);
        category_title.setText(data[position]);

        category_image = (ImageView) v.findViewById(R.id.image);
        category_image.setImageResource(image[position]);

    }
    return v;
}

for now it's can solve my problem..

thanks

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