在适配器中重新加载视图时,图像消失了

时间:2015-02-09 08:47:12

标签: android

我有一个图像按钮和一个图像视图。我正在尝试获得悬停主题,例如当用户点击某个项目时,项目上会有悬停图像,这意味着他检查了该项目。我的xml:

布局1:

<?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"
    android:orientation="horizontal" >


    <ImageButton 
        android:id="@+id/fetchedImageObj"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:scaleType="centerInside"
        android:src="@drawable/test"
        android:background="@drawable/image_selector"
        android:clickable="true"
        android:padding="0dp"
        />
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView 
            android:id="@+id/hoverImage"
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:scaleType="centerInside"
            android:src="@drawable/correct"
            android:visibility="gone"/>
    </RelativeLayout>

这是我的适配器,我正在尝试完成任务。但是,当我选择一个项目并向下滚动它时,问题就会消失或切换到其他对象

适配器:

public class FetchItemAdapter extends BaseAdapter {

    private Activity myContext;
    private LayoutInflater inflater;
    public FetchModel fetchModel;
    private boolean check_click=false;

    public FetchItemAdapter(Activity context,FetchModel fetchModel)
    {
        this.myContext=context;
        inflater = context.getLayoutInflater();
        this.fetchModel = fetchModel;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return this.fetchModel.images.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
private static class ViewHolder {


        ImageButton fetchedImageObj;
        ImageView selection;

}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        System.out.println("get View");



        final ViewHolder viewHolder = new ViewHolder();


        convertView = inflater.inflate(com.yolove.R.layout.fetched_images_row,null);

        //viewHolder = new ViewHolder();
        viewHolder.fetchedImageObj = (ImageButton) convertView.findViewById(com.yolove.R.id.fetchedImageObj);
        viewHolder.selection=(ImageView)convertView.findViewById(com.yolove.R.id.hoverImage);


        convertView.setTag(viewHolder);


       ImageLoader.getInstance().displayImage(fetchModel.images.get(position).imageurl,viewHolder.fetchedImageObj);


       viewHolder.fetchedImageObj.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(check_click==false)
                {
                    viewHolder.selection.setVisibility(View.VISIBLE);
                    check_click=true;


                }
                else
                {
                    viewHolder.selection.setVisibility(View.GONE);
                    check_click=false;
                }

            }
        });

            return convertView;
    }

}

我让我的持有者最终获得了viewholder.selection对象。

1 个答案:

答案 0 :(得分:1)

在我看来,你需要为你的图像背景制作一个选择器xml,并在xml文件中设置图像视图。