0
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder helper = null;
    Log.i("StaggeredGridView--Adapter:", "position:" + position);
    if(convertView ==null){
        helper = new ViewHolder();
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_user_details_adapter, null);
        helper.tv_content = (EmojiconTextView) convertView.findViewById(R.id.txt_content);
        helper.tv_time = (TextView) convertView.findViewById(R.id.txt_time);
        helper.tv_zannum = (TextView) convertView.findViewById(R.id.tv_zan_num);
        helper.tv_plnum = (TextView) convertView.findViewById(R.id.tv_pl_num);
        helper.iv_show = (DynamicHeightImageView) convertView.findViewById(R.id.img_content);// 展示的图片
        helper.img_zan = (ImageView) convertView.findViewById(R.id.img_normal);// 已经赞过的改颜色。
        helper.rel_photo = (RelativeLayout) convertView.findViewById(R.id.rel_photo);
        convertView.setTag(helper);
    } else {
     helper = (ViewHolder) convertView.getTag();
    }

    double positionHeight = getPositionRatio(position);
    Log.d(TAG, "getView position:" + position + " h:" + positionHeight);
    helper.iv_show.setHeightRatio(positionHeight);
    String imgeurl = "";
    List<Map<String, String>> listget = mUserInfors.get(position).getmAttach();
    if (listget != null && listget.size() > 0) {
        for (int i = 0; i < listget.size(); i++) {
            Map<String, String> map = listget.get(i);
            if (map != null) {
                if (map.get("attach_middle") != null) {
                    imgeurl = map.get("attach_middle");
                    if (!TextUtils.isEmpty(imgeurl)) {
                        break;
                    }
                }

            }
        }
    }
    List<Map<String, String>> diggerlist = mUserInfors.get(position).getDigger_list();
    if (diggerlist.size() > 0) {
        helper.tv_zannum.setText(diggerlist.size() + "");
        boolean state = getCheckstate(diggerlist);
        if (state) {
            helper.img_zan.setImageDrawable(mContext.getResources().getDrawable(R.drawable.zan));
        } else {
            helper.img_zan.setImageDrawable(mContext.getResources().getDrawable(R.drawable.normalzan));
        }
    } else {
        helper.img_zan.setImageDrawable(mContext.getResources().getDrawable(R.drawable.normalzan));
    }
    if (!TextUtils.isEmpty(imgeurl)) {
        ImageLoader.getInstance().displayImage(imgeurl, helper.iv_show, mDisplayOption);
    } else {
        helper.iv_show.setImageDrawable(mContext.getResources().getDrawable(R.drawable.empty_activity_icon));
    }
    String content = mUserInfors.get(position).getContent();
    String time = mUserInfors.get(position).getCtime();
    helper.tv_time.setText(time.substring(5));
    helper.tv_zannum.setText(mUserInfors.get(position).getDigg_count());
    helper.tv_plnum.setText(mUserInfors.get(position).getComment_count());
    helper.tv_content.setText(content);
    if (mUserInfors.get(position).getType().equals("post")) {
        helper.rel_photo.setVisibility(View.GONE);
        helper.tv_content.setVisibility(View.VISIBLE);
    } else {
        if (TextUtils.isEmpty(content)) {
            helper.tv_content.setVisibility(View.GONE);
        } else {
            helper.tv_content.setVisibility(View.VISIBLE);
        }
        helper.rel_photo.setVisibility(View.VISIBLE);
    }

    return convertView;

}

上面是getview的代码,我是在使用staggeredgridview Etsy,当我滚动屏幕的时候出现这个问题,位置混乱的时候,好像没有记住位置的位置。

以下是位置getview的截图: 图片

4

2 回答 2

0

仅当您没有控制 getCount()getItem()方法时才会出现此问题。确保您返回列表大小,getCount()如下所示:

@Override
    public int getCount() {
    return list.size();
    }

getItem()作为:

@Override
    public SetterGetterClassName getItem(int position) {
        return list.get(position);
    }
于 2015-12-30T07:52:10.377 回答
0

这是我的完整代码:

  enter code here

public class UserDetailsAdapter2 extends BaseAdapter {
//private HashMap<Integer, View> viewMap;  
private DisplayImageOptions mDisplayOption = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true)
        .showStubImage(R.drawable.empty_activity_icon).showImageForEmptyUri(R.drawable.empty)
        .showImageOnFail(R.drawable.empty_activity_icon).imageScaleType(ImageScaleType.EXACTLY)
        .bitmapConfig(Bitmap.Config.RGB_565).displayer(new FadeInBitmapDisplayer(400)).considerExifParams(true)
        .build();
private Context mContext;
private List<CellQzones> mUserInfors;
private UserInfor mUser;
private String TAG = "UserDetailsAdapter2";
private final Random mRandom;
private static final SparseArray<Double> sPositionHeightRatios = new SparseArray<Double>();
//private ImageLoader2 mImageLoader2;
public UserDetailsAdapter2(Context context, List<CellQzones> mDatas, UserInfor user) {
    mContext = context;
    mUserInfors = mDatas;
    mUser = user;
    mRandom = new Random();
    //viewMap=new HashMap<Integer, View>();
}

@Override
public int getCount() {
    return mUserInfors.size();
}

@Override
public Object getItem(int position) {
    return mUserInfors.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder helper = null;
    Log.i("StaggeredGridView--Adapter:", "position:" + position);
//      if(!viewMap.containsKey(position) || viewMap.get(position) == null){ 
    if(convertView ==null){
        helper = new ViewHolder();
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_user_details_adapter, null);
        helper.tv_content = (EmojiconTextView) convertView.findViewById(R.id.txt_content);
        helper.tv_time = (TextView) convertView.findViewById(R.id.txt_time);
        helper.tv_zannum = (TextView) convertView.findViewById(R.id.tv_zan_num);
        helper.tv_plnum = (TextView) convertView.findViewById(R.id.tv_pl_num);
        helper.iv_show = (DynamicHeightImageView) convertView.findViewById(R.id.img_content);// 展示的图片
        helper.img_zan = (ImageView) convertView.findViewById(R.id.img_normal);// 已经赞过的改颜色。
        helper.rel_photo = (RelativeLayout) convertView.findViewById(R.id.rel_photo);
        convertView.setTag(helper);
    } else {
        //convertView = viewMap.get(position);  
        helper = (ViewHolder) convertView.getTag();
    }

    double positionHeight = getPositionRatio(position);
    Log.d(TAG, "getView position:" + position + " h:" + positionHeight);
    helper.iv_show.setHeightRatio(positionHeight);
    String imgeurl = "";
    List<Map<String, String>> listget = mUserInfors.get(position).getmAttach();
    if (listget != null && listget.size() > 0) {
        for (int i = 0; i < listget.size(); i++) {
            Map<String, String> map = listget.get(i);
            if (map != null) {
                if (map.get("attach_middle") != null) {
                    imgeurl = map.get("attach_middle");
                    if (!TextUtils.isEmpty(imgeurl)) {
                        break;
                    }
                }

            }
        }
    }
    List<Map<String, String>> diggerlist = mUserInfors.get(position).getDigger_list();
    if (diggerlist.size() > 0) {
        helper.tv_zannum.setText(diggerlist.size() + "");
        boolean state = getCheckstate(diggerlist);
        if (state) {
            helper.img_zan.setImageDrawable(mContext.getResources().getDrawable(R.drawable.zan));
        } else {
            helper.img_zan.setImageDrawable(mContext.getResources().getDrawable(R.drawable.normalzan));
        }
    } else {
        helper.img_zan.setImageDrawable(mContext.getResources().getDrawable(R.drawable.normalzan));
    }
    if (!TextUtils.isEmpty(imgeurl)) {
        //mImageLoader2.loadImage(imgeurl,helper.iv_show, true);
        ImageLoader.getInstance().displayImage(imgeurl, helper.iv_show, mDisplayOption);
    } else {
        helper.iv_show.setImageDrawable(mContext.getResources().getDrawable(R.drawable.empty_activity_icon));
    }
    String content = mUserInfors.get(position).getContent();
    String time = mUserInfors.get(position).getCtime();
    helper.tv_time.setText(time.substring(5));
    helper.tv_zannum.setText(mUserInfors.get(position).getDigg_count());
    helper.tv_plnum.setText(mUserInfors.get(position).getComment_count());
    helper.tv_content.setText(content);
    if (mUserInfors.get(position).getType().equals("post")) {
        helper.rel_photo.setVisibility(View.GONE);
        helper.tv_content.setVisibility(View.VISIBLE);
    } else {
        if (TextUtils.isEmpty(content)) {
            helper.tv_content.setVisibility(View.GONE);
        } else {
            helper.tv_content.setVisibility(View.VISIBLE);
        }
        helper.rel_photo.setVisibility(View.VISIBLE);
    }

    return convertView;
}

public class ViewHolder {
    EmojiconTextView tv_content;
    TextView tv_time;
    TextView tv_zannum;
    TextView tv_plnum;
    DynamicHeightImageView iv_show;
    ImageView img_zan;
    RelativeLayout rel_photo;
}

private double getPositionRatio(final int position) {
    double ratio = sPositionHeightRatios.get(position, 0.0);
    // if not yet done generate and stash the columns height
    // in our real world scenario this will be determined by
    // some match based on the known height and width of the image
    // and maybe a helpful way to get the column height!
    if (ratio == 0) {
        ratio = getRandomHeightRatio();
        sPositionHeightRatios.append(position, ratio);
        Log.d(TAG, "getPositionRatio:" + position + " ratio:" + ratio);
    }
    return ratio;
}

private double getRandomHeightRatio() {
    return (mRandom.nextDouble() / 2.0) + 1.0; // height will be 1.0 - 1.5
                                                // the width
}

/** 得到checkbox的赞的状态 **/
private boolean getCheckstate(List<Map<String, String>> diggerlist) {
    for (int i = 0; i < diggerlist.size(); i++) {
        Map<String, String> mp = diggerlist.get(i);
        String uid = mUser.getUid();
        String mUid = mp.get("uid");
        // 如果有就设置true。
        if (uid.equals(mUid)) {
            return true;
        }
    }
    return false;
}

}
于 2015-12-30T08:32:17.543 回答