0

I have a listview backed by an adapter. Each row has a number of views, two of which are textviews. Because I need to call view.getMeasuredWidth() on the textviews, I call and assign data to the textviews from inside onGlobalLayout() as

textview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @SuppressLint("NewApi")
        @Override
        public void onGlobalLayout() {
            int length = Utils.getTextLengthToFitTextview(textview, strValue);
            textview.setText(strValue.substring(0, length));
            if (length < strValue.length()) {
                textview2.setText(strValue.substring(length));
            } else {
                textview2.setVisibility(View.GONE);
            }
            if (Utils.hasJellyBean()) {
                textview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                textview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        }
    });

Now for some reason, the content of textview and textview1 do not always match the appropriate row. When I scroll, the contents jump rows. Does anyone know how I might fix this? I create my adapter by extending the BaseAdapter.

Also I am using holder pattern for my views.

learner
  • 11,490
  • 26
  • 97
  • 169
  • why do you have to assign data to the textview in the onGlobalTreeViewObserver? – tyczj Jun 15 '14 at 04:08
  • @tyczj I am using two textviews to create a so called `FlowTextView`, which gives the impression that the text flows around the images around it. – learner Jun 15 '14 at 05:09
  • To know how much text the first view can take, I have to do `view.getMeasuredWidth()` – learner Jun 15 '14 at 05:14
  • smells like XY problem, explain what is your primary goal – pskink Jun 15 '14 at 07:02
  • @pskink my primary goal is to do something like this: http://stackoverflow.com/questions/3626750/textview-wrap-around-view or ashttps://code.google.com/p/android-flowtextview/. But for some reason they are not working well from within an adapter. First they don't load when the views first appear; then after I scroll a view offscreen and then back, at which point the padding finally shows, the padding is three times what it's supposed to be. – learner Jun 15 '14 at 14:56

0 Answers0