Android: calculating view size inside MyAdapter :: getView

I have ListView, inside which I dynamically populate the list items in a method getView()with a combination of TextViewand ImageView.

I need to calculate the size of the Text and Image view inside the getView, so I can set visibility=goneif the View is too big. I am tring:

public View getView(int position, View convertView, ViewGroup parent) {
  ..
  View listItem=view.findViewById(R.id.listItem);
  ImageView imageView=(ImageView)view.findViewById(R.id.myImg);
  TextView textView=(TextView)view.findViewById(R.id.mytxt);
  if (imageView.getRight()>listItemText.getRight()) {
    imageView.setVisibility(View.GONE);   
  }
  if (textView.getRight()>listItemText.getRight()) {
    textView.setVisibility(View.GONE);    
  }
  ..
}

However, since I'm inside getView(), the layout values ​​have not yet been created, so I get false values ​​for the call View.getRight().

Any ideas how to do this?

+5
source share
1 answer

, onLayoutChangeListener , , , .

textView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                // TODO: Take care of visibility here. You can access the parent View by v.getParent() method.
            }
        });

getView. textView android:ellipsize="marquee" textView. : Android

0

All Articles