在列表视图中平滑滚动,具有多个行布局

时间:2013-12-19 09:03:42

标签: android performance android-listview scroll

我有一个listview,根据位置(覆盖getViewTypeCount()和getItemViewType(int position)来实现它,膨胀4种不同类型的行布局。)每行都有一个Image和其他textview和按钮。我在一个seaparate线程中加载图像。我正在使用android视图模式来缓存每个行类型的视图。

当我从一种行布局滚动到另一行布局时,我观察到滚动时的急动。

任何改进的提示都将受到赞赏。

        public View getView(int arg0, View arg1, ViewGroup arg2) {

    int width = (Utils.getWidth(mCtx)-Utils.dpToPx(40, mCtx)) ;

    final FeedViewData mFeedViewData = getFeedViewData().get(arg0);

    if( getItemViewType(arg0) == Utils.SMALL_OBJECT_LEFT){
        ObjectViewHolder viewHolderLeft = new ObjectViewHolder() ;
        float imgWidth = (width-Utils.dpToPx(1, mCtx))*(0.6f);
        float descWidth = (width-Utils.dpToPx(1, mCtx))*(0.4f) ;
        float rowHeight = (width-Utils.dpToPx(1, mCtx))*(0.6f)*Utils.PRODUCT_PRI_ASPECT_RATIO;
        if(arg1==null){
            LayoutInflater inflater = ((Activity)mCtx).getLayoutInflater();
            arg1 = inflater.inflate(R.layout.view_multipane_left_image, arg2, false);
            viewHolderLeft.row = arg1.findViewById(R.id.rowLayout);

            viewHolderLeft.imagesLayout = (RelativeLayout)arg1.findViewById(R.id.layout_image);
            viewHolderLeft.objImg = (ImageView)arg1.findViewById(R.id.img_object);
            viewHolderLeft.price = (TextView)arg1.findViewById(R.id.text_price);

            viewHolderLeft.paneMargin = arg1.findViewById(R.id.pane_margin);
            viewHolderLeft.descriptionsLayout = (LinearLayout)arg1.findViewById(R.id.layout_description);

            viewHolderLeft.likeActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_like);
            viewHolderLeft.likeCount = (TextView)arg1.findViewById(R.id.text_like_count);

            viewHolderLeft.shareActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_share);
            viewHolderLeft.shareCount = (TextView)arg1.findViewById(R.id.text_share_count);

            viewHolderLeft.commentActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_comment);
            viewHolderLeft.commentCount = (TextView)arg1.findViewById(R.id.text_share_comment);

            viewHolderLeft.activityLayout = (LinearLayout)arg1.findViewById(R.id.layout_activity);
            viewHolderLeft.userImg = (ImageView)arg1.findViewById(R.id.img_user);
            viewHolderLeft.userActivity = (TextView)arg1.findViewById(R.id.text_activity);
            viewHolderLeft.timeline = (TextView)arg1.findViewById(R.id.text_timeline);

            RelativeLayout.LayoutParams imgLp = new RelativeLayout.LayoutParams(
                    new RelativeLayout.LayoutParams((int)imgWidth,(int)rowHeight));
            imgLp.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
            viewHolderLeft.objImg.setLayoutParams(imgLp);

            RelativeLayout.LayoutParams rlLp = new RelativeLayout.LayoutParams(
                    new RelativeLayout.LayoutParams((int)imgWidth,(int)rowHeight));
            rlLp.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
            rlLp.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
            viewHolderLeft.imagesLayout.setLayoutParams(rlLp);

            RelativeLayout.LayoutParams marginLp = new RelativeLayout.LayoutParams(
                    new RelativeLayout.LayoutParams(Utils.dpToPx(1,mCtx),(int)rowHeight)); 
            marginLp.addRule(RelativeLayout.RIGHT_OF,R.id.layout_image);
            viewHolderLeft.paneMargin.setLayoutParams(marginLp);

            RelativeLayout.LayoutParams descLp = new RelativeLayout.LayoutParams(
                    new RelativeLayout.LayoutParams((int)descWidth,(int)rowHeight)); 
            descLp.addRule(RelativeLayout.RIGHT_OF,R.id.pane_margin);
            descLp.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
            viewHolderLeft.descriptionsLayout.setLayoutParams(descLp);          

            ImageSizeUtils.defineTargetSizeForView(viewHolderLeft.objImg, (int)imgWidth,(int)rowHeight);

            arg1.setTag(viewHolderLeft);

        }
        else{
            viewHolderLeft = (ObjectViewHolder)arg1.getTag();               
            ImageSizeUtils.defineTargetSizeForView(viewHolderLeft.objImg, (int)imgWidth,(int)rowHeight);
        }       

        String mLeftImgUrl = null;

        mLeftImgUrl = Utils.m_img_base_url+
                mFeedViewData.getProductId() + "/pri-" + mFeedViewData.getFileidn()+".jpg";

        try{
            ImageLoader.getInstance().displayImage(mLeftImgUrl, viewHolderLeft.objImg, 
                    new SingleImageListener(mFeedViewData,viewHolderLeft.objImg));
        }
        catch(Exception e){             
            return null;
        } 

        viewHolderLeft.objImg.setOnClickListener(new ProdImageOnClickListener(mFeedViewData));

        if(mFeedViewData.getIsLoved())
            viewHolderLeft.likeActionLayout.setBackgroundColor(mCtx.getResources().getColor(R.color.auth_btn_color_normal));
        else
            viewHolderLeft.likeActionLayout.setBackgroundResource(R.drawable.selector_pink_button);

        viewHolderLeft.likeActionLayout.setOnClickListener(new LikeActionListener(mFeedViewData,viewHolderLeft.likeActionLayout));
        viewHolderLeft.shareActionLayout.setOnClickListener(new ShareClickListener(mLeftImgUrl));
        viewHolderLeft.commentActionLayout.setOnClickListener(new OnCommentClickListener(mFeedViewData));

        viewHolderLeft.price.setTypeface(Utils.getFontForRupeeSymb(mCtx));
        viewHolderLeft.price.setText(Html.fromHtml( "` " + "</font>" + "<b>" + mFeedViewData.getPrice() + "</b>"));
        if(!mFeedViewData.getLikeCount().equals("0"))
            viewHolderLeft.likeCount.setText(" (" + mFeedViewData.getLikeCount() + ")");
        if(!mFeedViewData.getShareCount().equals("0"))
            viewHolderLeft.shareCount.setText(" (" + mFeedViewData.getShareCount() + ")");
        if(!mFeedViewData.getRevCount().equals("0"))
            viewHolderLeft.commentCount.setText(" (" + mFeedViewData.getRevCount() + ")");

        int minutes=0;

        if(mFeedViewData.getTimestamp()!=""){
            long timeDiff = System.currentTimeMillis() - Long.parseLong(mFeedViewData.getTimestamp());
            minutes = (int)((int) (timeDiff/1000))/60;
        }

        if(mFeedViewData.getUsername().length()>0){
            ImageLoader.getInstance().displayImage(mFeedViewData.getTnPic(), viewHolderLeft.userImg,new UserImageListener(viewHolderLeft.userImg)); 
            String activityText = Html.fromHtml("<b>" + mFeedViewData.getUsername() + "</b>" + " "+ mFeedViewData.getActivity()).toString();
            SpannableString ss = new SpannableString(activityText);

            ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getDoer()), 0, mFeedViewData.getUsername().length(), Spanned.SPAN_COMPOSING);
            viewHolderLeft.userActivity.setText(ss);
            if(minutes!=0){
                viewHolderLeft.timeline.setText( minutes + " minutes ago");                 
            }               
            viewHolderLeft.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
        }
        else if(mFeedViewData.getReview()!=null){
            ImageLoader.getInstance().displayImage(mFeedViewData.getReview().getTnUrl(), viewHolderLeft.userImg,new UserImageListener(viewHolderLeft.userImg)); 
            String activityText = Html.fromHtml("<b>" + mFeedViewData.getReview().getReviewerName() + "</b>" + " "+ mFeedViewData.getReview().getReview()).toString();
            SpannableString ss = new SpannableString(activityText);

            ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getReview().getId()), 0, mFeedViewData.getReview().getReviewerName().length(), Spanned.SPAN_COMPOSING);
            viewHolderLeft.userActivity.setText(ss);
            if(minutes!=0){
                viewHolderLeft.timeline.setText( minutes + " minutes ago");                 
            }               
            viewHolderLeft.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
        }
        else if(mFeedViewData.getActivity().length()>0){
            String activityText;
            if(mFeedViewData.getActivity().equals("null"))
                activityText = Html.fromHtml("<b>" + "A user: "+ "</b>" + "" +" ").toString();
            else
                activityText = Html.fromHtml("<b>" + "A user: "+ "</b>" + "" +" "+ mFeedViewData.getActivity()).toString();
            viewHolderLeft.userActivity.setText(activityText);
            if(minutes!=0){
                viewHolderLeft.timeline.setText( minutes + " minutes ago");                 
            }   
            viewHolderLeft.userImg.setImageResource(R.drawable.ic_blank_profile);
        }

        if(feedType == Utils.ADAPTER_FEED_TUTORIAL){
            viewHolderLeft.userActivity.setClickable(false);
            viewHolderLeft.objImg.setClickable(false);
        }
    }
    else if(getItemViewType(arg0) == Utils.SMALL_OBJECT_RIGHT){
        ObjectViewHolder viewHolderRight = new ObjectViewHolder();
        float imgWidth = (width-Utils.dpToPx(1, mCtx))*(0.6f);
        float descWidth = (width-Utils.dpToPx(1, mCtx))*(0.4f) ;
        float rowHeight = (width-Utils.dpToPx(1, mCtx))*(0.6f)*Utils.PRODUCT_PRI_ASPECT_RATIO;

        if(arg1==null){
            LayoutInflater inflater = ((Activity)mCtx).getLayoutInflater();
            arg1 = inflater.inflate(R.layout.view_multipane_right_image, arg2, false);          
            viewHolderRight.row = arg1.findViewById(R.id.rowLayout);

            viewHolderRight.imagesLayout = (RelativeLayout)arg1.findViewById(R.id.layout_image);
            viewHolderRight.objImg = (ImageView)arg1.findViewById(R.id.img_object);
            viewHolderRight.price = (TextView)arg1.findViewById(R.id.text_price);

            viewHolderRight.paneMargin = arg1.findViewById(R.id.pane_margin);

            viewHolderRight.descriptionsLayout = (LinearLayout)arg1.findViewById(R.id.layout_description);

            viewHolderRight.likeActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_like);
            viewHolderRight.likeCount = (TextView)arg1.findViewById(R.id.text_like_count);

            viewHolderRight.shareActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_share);
            viewHolderRight.shareCount = (TextView)arg1.findViewById(R.id.text_share_count);

            viewHolderRight.commentActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_comment);
            viewHolderRight.commentCount = (TextView)arg1.findViewById(R.id.text_share_comment);

            viewHolderRight.activityLayout = (LinearLayout)arg1.findViewById(R.id.layout_activity);
            viewHolderRight.userImg = (ImageView)arg1.findViewById(R.id.img_user);
            viewHolderRight.userActivity = (TextView)arg1.findViewById(R.id.text_activity);
            viewHolderRight.timeline = (TextView)arg1.findViewById(R.id.text_timeline);

            RelativeLayout.LayoutParams descLp = new RelativeLayout.LayoutParams(
                    new RelativeLayout.LayoutParams((int)descWidth,(int)rowHeight)); 
            descLp.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
            descLp.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
            viewHolderRight.descriptionsLayout.setLayoutParams(descLp);

            RelativeLayout.LayoutParams marginLp = new RelativeLayout.LayoutParams(
                    new RelativeLayout.LayoutParams(Utils.dpToPx(1,mCtx),(int)rowHeight)); 
            marginLp.addRule(RelativeLayout.RIGHT_OF,R.id.layout_description);
            viewHolderRight.paneMargin.setLayoutParams(marginLp);

            RelativeLayout.LayoutParams imgLp = new RelativeLayout.LayoutParams(
                    new RelativeLayout.LayoutParams((int)imgWidth,(int)rowHeight));

            imgLp.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
            viewHolderRight.objImg.setLayoutParams(imgLp);

            RelativeLayout.LayoutParams rlLp = new RelativeLayout.LayoutParams(
                    new RelativeLayout.LayoutParams((int)imgWidth,(int)rowHeight));
            rlLp.addRule(RelativeLayout.RIGHT_OF,R.id.pane_margin);
            rlLp.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
            viewHolderRight.imagesLayout.setLayoutParams(rlLp);

            ImageSizeUtils.defineTargetSizeForView(viewHolderRight.objImg, (int)imgWidth,(int)rowHeight);

            arg1.setTag(viewHolderRight);
        }
        else{
            viewHolderRight = (ObjectViewHolder)arg1.getTag();
            ImageSizeUtils.defineTargetSizeForView(viewHolderRight.objImg, (int)imgWidth,(int)rowHeight);
        }

        String mRightImgUrl = null;

        mRightImgUrl = Utils.m_img_base_url+
                mFeedViewData.getProductId() + "/pri-" + mFeedViewData.getFileidn()+".jpg";

        try{

            ImageLoader.getInstance().displayImage(mRightImgUrl, viewHolderRight.objImg, 
                    new SingleImageListener(mFeedViewData,viewHolderRight.objImg));

        }
        catch(Exception e){             
            return null;
        } 

        viewHolderRight.objImg.setOnClickListener(new ProdImageOnClickListener(mFeedViewData));

        if(mFeedViewData.getIsLoved())
            viewHolderRight.likeActionLayout.setBackgroundColor(mCtx.getResources().getColor(R.color.auth_btn_color_normal));
        else
            viewHolderRight.likeActionLayout.setBackgroundResource(R.drawable.selector_pink_button);

        viewHolderRight.likeActionLayout.setOnClickListener(new LikeActionListener(mFeedViewData,viewHolderRight.likeActionLayout));
        viewHolderRight.shareActionLayout.setOnClickListener(new ShareClickListener(mRightImgUrl));
        viewHolderRight.commentActionLayout.setOnClickListener(new OnCommentClickListener(mFeedViewData));

        viewHolderRight.price.setTypeface(Utils.getFontForRupeeSymb(mCtx));
        viewHolderRight.price.setText(Html.fromHtml( "` " + "</font>" + "<b>" + mFeedViewData.getPrice() + "</b>"));
        if(!mFeedViewData.getLikeCount().equals("0"))
            viewHolderRight.likeCount.setText(" (" + mFeedViewData.getLikeCount() + ")");
        if(!mFeedViewData.getShareCount().equals("0"))
            viewHolderRight.shareCount.setText(" (" + mFeedViewData.getShareCount() + ")");
        if(!mFeedViewData.getRevCount().equals("0"))
            viewHolderRight.commentCount.setText(" (" + mFeedViewData.getRevCount() + ")");

        int minutes=0;

        if(mFeedViewData.getTimestamp()!=""){
            long timeDiff = System.currentTimeMillis() - Long.parseLong(mFeedViewData.getTimestamp());
            minutes = (int)((int) (timeDiff/1000))/60;
        }

        if(mFeedViewData.getUsername().length()>0){
            ImageLoader.getInstance().displayImage(mFeedViewData.getTnPic(), viewHolderRight.userImg,new UserImageListener(viewHolderRight.userImg));   
            String activityText = Html.fromHtml("<b>" + mFeedViewData.getUsername() + "</b>" + " "+ mFeedViewData.getActivity()).toString();
            SpannableString ss = new SpannableString(activityText);

            ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getDoer()), 0, mFeedViewData.getUsername().length(), Spanned.SPAN_COMPOSING);
            viewHolderRight.userActivity.setText(ss);
            if(minutes!=0){
                viewHolderRight.timeline.setText( minutes + " minutes ago");                    
            }               
            viewHolderRight.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
        }
        else if(mFeedViewData.getReview()!=null){
            ImageLoader.getInstance().displayImage(mFeedViewData.getReview().getTnUrl(), viewHolderRight.userImg,new UserImageListener(viewHolderRight.userImg));   
            String activityText = Html.fromHtml("<b>" + mFeedViewData.getReview().getReviewerName() + "</b>" + " "+ mFeedViewData.getReview().getReview()).toString();
            SpannableString ss = new SpannableString(activityText);

            ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getReview().getId()), 0, mFeedViewData.getReview().getReviewerName().length(), Spanned.SPAN_COMPOSING);
            viewHolderRight.userActivity.setText(ss);
            if(minutes!=0){
                viewHolderRight.timeline.setText( minutes + " minutes ago");                    
            }               
            viewHolderRight.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
        }
        else if(mFeedViewData.getActivity().length()>0){
            String activityText = Html.fromHtml("<b>" + "A user: "+ "</b>" + " "+ mFeedViewData.getActivity()).toString();
            viewHolderRight.userActivity.setText(activityText);
            if(minutes!=0){
                viewHolderRight.timeline.setText( minutes + " minutes ago");                    
            }   
            viewHolderRight.userImg.setImageResource(R.drawable.ic_blank_profile);
        }

        if(feedType == Utils.ADAPTER_FEED_TUTORIAL){
            viewHolderRight.userActivity.setClickable(false);
            viewHolderRight.objImg.setClickable(false);
        }
    }
    else if(getItemViewType(arg0) == Utils.MAGAZINE_OBJECT){
        MagazineViewHolder viewHolderMag = new MagazineViewHolder();
        float imgWidth = width/2;
        float descWidth = width;
        float rowHeight = ((width)/2)*Utils.MAG_ASPECT_RATIO;
        if(arg1==null){
            LayoutInflater inflater = ((Activity)mCtx).getLayoutInflater();
            arg1 = inflater.inflate(R.layout.view_magazine, arg2, false);
            viewHolderMag.row = arg1.findViewById(R.id.rowLayout);

            viewHolderMag.imagesLayout = (RelativeLayout)arg1.findViewById(R.id.layout_image);
            viewHolderMag.magImgLeft = (ImageView)arg1.findViewById(R.id.img_object_1);
            viewHolderMag.magImgRight = (ImageView)arg1.findViewById(R.id.img_object_2);
            viewHolderMag.price = (TextView)arg1.findViewById(R.id.text_price);

            viewHolderMag.likeActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_like);
            viewHolderMag.likeCount = (TextView)arg1.findViewById(R.id.text_like_count);

            viewHolderMag.shareActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_share);
            viewHolderMag.shareCount = (TextView)arg1.findViewById(R.id.text_share_count);

            viewHolderMag.commentActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_comment);
            viewHolderMag.commentCount = (TextView)arg1.findViewById(R.id.text_share_comment);

            viewHolderMag.activityLayout = (LinearLayout)arg1.findViewById(R.id.layout_activity);
            viewHolderMag.userImg = (ImageView)arg1.findViewById(R.id.img_user);
            viewHolderMag.userActivity = (TextView)arg1.findViewById(R.id.text_activity);
            viewHolderMag.timeline = (TextView)arg1.findViewById(R.id.text_timeline);

            LinearLayout.LayoutParams imgLp1 = new LinearLayout.LayoutParams(
                    new LinearLayout.LayoutParams((int)imgWidth,(int)rowHeight));
            imgLp1.gravity = Gravity.CENTER;
            viewHolderMag.magImgLeft.setLayoutParams(imgLp1);
            viewHolderMag.magImgRight.setLayoutParams(imgLp1);

            RelativeLayout.LayoutParams rlLp = new RelativeLayout.LayoutParams(
                    new RelativeLayout.LayoutParams((int)descWidth,(int)rowHeight));
            viewHolderMag.imagesLayout.setLayoutParams(rlLp);

            ImageSizeUtils.defineTargetSizeForView(viewHolderMag.magImgLeft,(int) imgWidth,(int)rowHeight);
            ImageSizeUtils.defineTargetSizeForView(viewHolderMag.magImgRight,(int) imgWidth,(int)rowHeight);

            arg1.setTag(viewHolderMag);
        }
        else{
            viewHolderMag = (MagazineViewHolder)arg1.getTag();
            ImageSizeUtils.defineTargetSizeForView(viewHolderMag.magImgLeft,(int) imgWidth,(int)rowHeight);
            ImageSizeUtils.defineTargetSizeForView(viewHolderMag.magImgRight,(int) imgWidth,(int)rowHeight);
        }

        String mImgUrlLeft = Utils.m_img_mag_base_url+mFeedViewData.getFileidn().split("-")[1]+".jpg";
        String mImgUrlRight =  Utils.m_img_mag_base_url+mFeedViewData.getFileidn().split("-")[2];

        try{
            ImageLoader.getInstance().displayImage(mImgUrlLeft, viewHolderMag.magImgLeft,
                    new MagazineImageListener(viewHolderMag.magImgLeft));
            ImageLoader.getInstance().displayImage(mImgUrlRight, viewHolderMag.magImgRight,
                    new MagazineImageListener(viewHolderMag.magImgRight));
        }
        catch(Exception e){
            return null;
        } 

        viewHolderMag.imagesLayout.setOnClickListener(new ProdImageOnClickListener(mFeedViewData));

        viewHolderMag.price.setTypeface(Utils.getFontForRupeeSymb(mCtx));
        viewHolderMag.price.setText(Html.fromHtml( "` " + "</font>" + "<b>" + mFeedViewData.getPrice() + "</b>"));

        //TODO: Remove once mag has price
        viewHolderMag.price.setVisibility(View.GONE);

        if(!mFeedViewData.getLikeCount().equals("0"))
            viewHolderMag.likeCount.setText(" (" + mFeedViewData.getLikeCount() + ")");
        if(!mFeedViewData.getShareCount().equals("0"))
            viewHolderMag.shareCount.setText(" (" + mFeedViewData.getShareCount() + ")");
        if(!mFeedViewData.getRevCount().equals("0"))
            viewHolderMag.commentCount.setText(" (" + mFeedViewData.getRevCount() + ")");

        if(mFeedViewData.getIsLoved())
            viewHolderMag.likeActionLayout.setBackgroundColor(mCtx.getResources().getColor(R.color.auth_btn_color_normal));
        else
            viewHolderMag.likeActionLayout.setBackgroundResource(R.drawable.selector_pink_button);

        viewHolderMag.shareActionLayout.setOnClickListener(new ShareClickListener(mFeedViewData.getMagImage()));
        viewHolderMag.likeActionLayout.setOnClickListener(new LikeActionListener(mFeedViewData,viewHolderMag.likeActionLayout));
        viewHolderMag.commentActionLayout.setOnClickListener(new OnCommentClickListener(mFeedViewData));

        int minutes=0;

        if(mFeedViewData.getTimestamp()!=""){
            long timeDiff = System.currentTimeMillis() - Long.parseLong(mFeedViewData.getTimestamp());
            minutes = (int)((int) (timeDiff/1000))/60;
        }

        if(mFeedViewData.getUsername().length()>0){
            ImageLoader.getInstance().displayImage(mFeedViewData.getTnPic(), viewHolderMag.userImg,new UserImageListener(viewHolderMag.userImg));   
            String activityText = Html.fromHtml("<b>" + mFeedViewData.getUsername() + "</b>" + " "+ mFeedViewData.getActivity()).toString();
            SpannableString ss = new SpannableString(activityText);

            ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getDoer()), 0, mFeedViewData.getUsername().length(), Spanned.SPAN_COMPOSING);
            viewHolderMag.userActivity.setText(ss);
            if(minutes!=0){
                viewHolderMag.timeline.setText( minutes + " minutes ago");                  
            }               
            viewHolderMag.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
        }
        else if(mFeedViewData.getReview()!=null){
            ImageLoader.getInstance().displayImage(mFeedViewData.getReview().getTnUrl(), viewHolderMag.userImg,new UserImageListener(viewHolderMag.userImg));   
            String activityText = Html.fromHtml("<b>" + mFeedViewData.getReview().getReviewerName() + "</b>" + " "+ mFeedViewData.getReview().getReview()).toString();
            SpannableString ss = new SpannableString(activityText);

            ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getReview().getId()), 0, mFeedViewData.getReview().getReviewerName().length(), Spanned.SPAN_COMPOSING);
            viewHolderMag.userActivity.setText(ss);
            if(minutes!=0){
                viewHolderMag.timeline.setText( minutes + " minutes ago");                  
            }               
            viewHolderMag.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
        }
        else if(mFeedViewData.getActivity().length()>0){
            String activityText = Html.fromHtml("<b>" + "A user: "+ "</b>" + " "+ mFeedViewData.getActivity()).toString();
            viewHolderMag.userActivity.setText(activityText);
            if(minutes!=0){
                viewHolderMag.timeline.setText( minutes + " minutes ago");                  
            }   
            viewHolderMag.userImg.setImageResource(R.drawable.ic_blank_profile);
        }

        if(feedType == Utils.ADAPTER_FEED_TUTORIAL){
            viewHolderMag.userActivity.setClickable(false);
            viewHolderMag.imagesLayout.setClickable(false);
        }
    }
    else if(getItemViewType(arg0) == Utils.SCRAP_OBJECT){
        ObjectViewHolder viewHolderScrap = new ObjectViewHolder();
        float imgWidth = width;
        float rowHeight = (width)*Utils.SCRAPBOOK_ASPECT_RATIO;
        if(arg1==null){
            LayoutInflater inflater = ((Activity)mCtx).getLayoutInflater();
            arg1 = inflater.inflate(R.layout.view_single_pane, arg2, false);
            viewHolderScrap.row = arg1.findViewById(R.id.rowLayout);

            viewHolderScrap.imagesLayout = (RelativeLayout)arg1.findViewById(R.id.layout_image);
            viewHolderScrap.objImg = (ImageView)arg1.findViewById(R.id.img_object);
            viewHolderScrap.price = (TextView)arg1.findViewById(R.id.text_price);           

            viewHolderScrap.likeActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_like);
            viewHolderScrap.likeCount = (TextView)arg1.findViewById(R.id.text_like_count);

            viewHolderScrap.shareActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_share);
            viewHolderScrap.shareCount = (TextView)arg1.findViewById(R.id.text_share_count);

            viewHolderScrap.commentActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_comment);
            viewHolderScrap.commentCount = (TextView)arg1.findViewById(R.id.text_share_comment);

            viewHolderScrap.activityLayout = (LinearLayout)arg1.findViewById(R.id.layout_activity);
            viewHolderScrap.userImg = (ImageView)arg1.findViewById(R.id.img_user);
            viewHolderScrap.userActivity = (TextView)arg1.findViewById(R.id.text_activity);
            viewHolderScrap.timeline = (TextView)arg1.findViewById(R.id.text_timeline);

            RelativeLayout.LayoutParams imgLp1 = new RelativeLayout.LayoutParams(
                    new RelativeLayout.LayoutParams((int)imgWidth,(int)rowHeight));
            viewHolderScrap.objImg.setLayoutParams(imgLp1);

            RelativeLayout.LayoutParams rlLp = new RelativeLayout.LayoutParams(
                    new RelativeLayout.LayoutParams((int)imgWidth,(int)rowHeight));
            viewHolderScrap.imagesLayout.setLayoutParams(rlLp);

            ImageSizeUtils.defineTargetSizeForView(viewHolderScrap.objImg,(int) imgWidth,(int)rowHeight);

            arg1.setTag(viewHolderScrap);
        }
        else{
            viewHolderScrap = (ObjectViewHolder)arg1.getTag();
            ImageSizeUtils.defineTargetSizeForView(viewHolderScrap.objImg,(int) imgWidth,(int)rowHeight);
        }

        String mNormalImgUrl = null;
        mNormalImgUrl = Utils.m_img_scrap_base_url + mFeedViewData.getScrapId() + ".png";

        try{
            ImageLoader.getInstance().displayImage(mNormalImgUrl, viewHolderScrap.objImg, 
                    new SingleImageListener(mFeedViewData,viewHolderScrap.objImg));
        }
        catch(Exception e){
            return null;
        } 

        viewHolderScrap.objImg.setOnClickListener(new ProdImageOnClickListener(mFeedViewData));

        if(mFeedViewData.getIsLoved())
            viewHolderScrap.likeActionLayout.setBackgroundColor(mCtx.getResources().getColor(R.color.auth_btn_color_normal));
        else
            viewHolderScrap.likeActionLayout.setBackgroundResource(R.drawable.selector_pink_button);

        viewHolderScrap.likeActionLayout.setOnClickListener(new LikeActionListener(mFeedViewData,viewHolderScrap.likeActionLayout));
        viewHolderScrap.shareActionLayout.setOnClickListener(new ShareClickListener(mNormalImgUrl));
        viewHolderScrap.commentActionLayout.setOnClickListener(new OnCommentClickListener(mFeedViewData));

        viewHolderScrap.price.setTypeface(Utils.getFontForRupeeSymb(mCtx));
        viewHolderScrap.price.setText(Html.fromHtml( "` " + "</font>" + "<b>" + mFeedViewData.getPrice() + "</b>"));

        //TODO:Remove once scrap has price
        viewHolderScrap.price.setVisibility(View.GONE);

        if(!mFeedViewData.getLikeCount().equals("0"))
            viewHolderScrap.likeCount.setText(" (" + mFeedViewData.getLikeCount() + ")");
        if(!mFeedViewData.getShareCount().equals("0"))
            viewHolderScrap.shareCount.setText(" (" + mFeedViewData.getShareCount() + ")");
        if(!mFeedViewData.getRevCount().equals("0"))
            viewHolderScrap.commentCount.setText(" (" + mFeedViewData.getRevCount() + ")");

        int minutes=0;

        if(mFeedViewData.getTimestamp()!=""){
            long timeDiff = System.currentTimeMillis() - Long.parseLong(mFeedViewData.getTimestamp());
            minutes = (int)((int) (timeDiff/1000))/60;
        }

        if(mFeedViewData.getUsername().length()>0 && (!mFeedViewData.getActivity().equals("null")) && mFeedViewData.getActivity().length()>0){
            ImageLoader.getInstance().displayImage(mFeedViewData.getTnPic(), viewHolderScrap.userImg,new UserImageListener(viewHolderScrap.userImg));   

            String activityText = Html.fromHtml("<b>" + mFeedViewData.getUsername() + "</b>" + " "+ mFeedViewData.getActivity()).toString();
            SpannableString ss = new SpannableString(activityText);

            ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getDoer()), 0, mFeedViewData.getUsername().length(), Spanned.SPAN_COMPOSING);
            viewHolderScrap.userActivity.setText(ss);
            if(minutes!=0){
                viewHolderScrap.timeline.setText( minutes + " minutes ago");                    
            }                           
            viewHolderScrap.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
        }
        else if(mFeedViewData.getReview()!=null){
            ImageLoader.getInstance().displayImage(mFeedViewData.getReview().getTnUrl(), viewHolderScrap.userImg,new UserImageListener(viewHolderScrap.userImg));   
            String activityText = Html.fromHtml("<b>" + mFeedViewData.getReview().getReviewerName() + "</b>" + " "+ mFeedViewData.getReview().getReview()).toString();
            SpannableString ss = new SpannableString(activityText);

            ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getReview().getId()), 0, mFeedViewData.getReview().getReviewerName().length(), Spanned.SPAN_COMPOSING);
            viewHolderScrap.userActivity.setText(ss);
            if(minutes!=0){
                viewHolderScrap.timeline.setText( minutes + " minutes ago");                    
            }               
            viewHolderScrap.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
        }
        else if(mFeedViewData.getActivity().length()>0){
            String activityText = Html.fromHtml("<b>" + "A user: "+ "</b>" + " "+ mFeedViewData.getActivity()).toString();
            viewHolderScrap.userActivity.setText(activityText);
            if(minutes!=0){
                viewHolderScrap.timeline.setText( minutes + " minutes ago");                    
            }   
            viewHolderScrap.userImg.setImageResource(R.drawable.ic_blank_profile);
        }

        if(feedType == Utils.ADAPTER_FEED_TUTORIAL){
            viewHolderScrap.userActivity.setClickable(false);
            viewHolderScrap.objImg.setClickable(false);
        }

    }

    return arg1;
}

2 个答案:

答案 0 :(得分:1)

确保您不使用Adapter.getView()方法加载或处理任何图像。如果您需要这样做,请在单独的AsyncTask中进行。如果您从Internet下载图像,最好使用一些辅助库,如VolleyPicasso

答案 1 :(得分:0)

我使用此库在列表视图中加载图像,我从未遇到过问题:Android Image Loader