在屏幕上获取位置,以便在查看权持有人

时间:2015-10-05 13:16:14

标签: android android-recyclerview android-viewholder

我有一个由适配器管理的Recyclerview。对于回收器中的每个项目,我都会为一个复杂的视图进行充气,该视图还包含一个水平进度条,该进度条占据整个屏幕宽度。

我必须将气球TextView定位为指向进度条的百分比值(20%,60%等),作为进度量的指标。我尝试使用此代码

int[] startPosition =  new int[2];
Integer progresswidth;
WindowManager wm = (WindowManager) contextthis.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
progresswidth = size.x;
holder.horiz_progress.getLocationOnScreen(startPosition);
float exactvalue = (progresswidth * currentitem.getMatchPercent()) / 100;
startPosition[0] = startPosition[0] + Math.round(exactvalue);
startPosition[0] = startPosition[0] - holder.baloon_txt.getWidth() / 3 ;
startPosition[1] = startPosition[1] + 10;
holder.baloon_txt.setX(startPosition[0]);
holder.baloon_txt.setY(startPosition[1]);

但问题是holder.horiz_progress.getLocationOnScreen总是返回0,所以我无法定位balloon_txt。 我在一个活动中遇到了类似的问题,我解决了它覆盖OnWindowFocusChanged,但这是在适配器内,所以我不知道如何完成它。

编辑

我当前的适配器代码:

public class ResultsAdapter extends RecyclerView.Adapter<ResultsAdapter.ViewHolder>{

private List<ResultsItem> mResults;
private View mSelectedView;
private int mSelectedPosition;
Context contextthis;
android.os.Handler handler;
int[] startPosition =  new int[2];

public ResultsAdapter(Context context,List<ResultsItem> resultsItemList) {
    this.mResults = resultsItemList;
    contextthis = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.results_item, null);
    final ViewHolder viewHolder = new ViewHolder(v);

    viewHolder.results_likeimg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        }
    });

    return viewHolder;
}

@Override
public void onBindViewHolder( final ViewHolder holder, int position) {
    final ResultsItem currentitem = mResults.get(position);
    //set Image
    if(currentitem.getImageslist().get(0).getPicture() != null)
        ImageLoader.getInstance().displayImage(currentitem.getImageslist().get(0).getPicture(), holder.results_img);

    //baloon set
    holder.baloon_txt.setText(currentitem.getMatchPercent() + "% " + "Match");
    holder.horiz_progress.setProgress(currentitem.getMatchPercent());
    final View view = holder.horiz_progress;
            view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            view.getLocationOnScreen(startPosition);
            Integer progresswidth;
            WindowManager wm = (WindowManager) contextthis.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            progresswidth = size.x;
            float exactvalue = (progresswidth * currentitem.getMatchPercent()) / 100;
            startPosition[0] = startPosition[0] + Math.round(exactvalue);
            startPosition[0] = startPosition[0] - holder.baloon_txt.getWidth() / 3 ;
            startPosition[1] = startPosition[1] + 10;
            holder.baloon_txt.setX(startPosition[0]);
            holder.baloon_txt.setY(startPosition[1]);
        }
    });


    //logo
    if(currentitem.getPriceslist().get(0).getSource() != null)
        ImageLoader.getInstance().displayImage(currentitem.getPriceslist().get(0).getSource(), holder.results_logo_img);

    //description
    holder.description_txt.setText(currentitem.getDescription());

    //price
    holder.price_curr.setText(currentitem.getPriceslist().get(0).getCurrency());
    holder.price_txt.setText(String.valueOf(currentitem.getPriceslist().get(0).getPrice()));
}

@Override
public int getItemCount() {
    return mResults.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {

    ImageView results_img, results_dislikeimg, results_likeimg, results_logo_img;
    ProgressBar horiz_progress;
    TextView baloon_txt, price_txt, description_txt, buybtn, sharebtn, price_curr;

    public ViewHolder(View view) {
        super(view);

        this.results_img = (ImageView) view.findViewById(R.id.resultsitem_img);
        this.results_dislikeimg = (ImageView) view.findViewById(R.id.results_item_dislike);
        this.results_likeimg = (ImageView) view.findViewById(R.id.resultsitem_like);
        this.results_logo_img = (ImageView) view.findViewById(R.id.logoimg);
        this.horiz_progress = (ProgressBar) view.findViewById(R.id.progressBar_horizontal);
        this.baloon_txt = (TextView) view.findViewById(R.id.baloonMatch_txt);
        this.price_txt = (TextView) view.findViewById(R.id.price_txt);
        this.description_txt = (TextView) view.findViewById(R.id.description_txt);
        this.buybtn = (TextView) view.findViewById(R.id.buybtn);
        this.sharebtn = (TextView) view.findViewById(R.id.sharebtn);
        this.price_curr = (TextView) view.findViewById(R.id.price_curr);

    }
}

}

1 个答案:

答案 0 :(得分:3)

getLocation()返回0,因为视图尚未布局。您需要设置布局侦听器:

final View view = holder.horiz_progress;
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
         // Do what you need to do here.
         // Then remove the listener:
         view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }

});