1
votes

I have a ScrollView in each item of a gridview. In this scrollview there is a series of Views. I would like to be able to determine if a view is currently visible.

I already seen this : Android: how to check if a View inside of ScrollView is visible?

I try this code in my custom adapter but it's looks like that it doesn't work because the value of bounds is always (0,0,0,0) ..

Did you have an idea ?

 @Override
public View getView(int position, View convertView, final ViewGroup parent) {
    final ViewHolder holder;
    String reference_sequence = "DIRECT";

    if(convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        //On recupère le layout
        convertView= mInflater.inflate(R.layout.listitemaboyeur, parent, false);
        holder = new ViewHolder();
        // On place les widgets de notre layout dans le holder
        holder.Couverts = (TextView) convertView.findViewById(R.id.Couverts);
        holder.Table = (TextView) convertView.findViewById(R.id.Table);
        holder.Time = (TextView) convertView.findViewById(R.id.Time);
        holder.Block=(LinearLayout) convertView.findViewById(R.id.BlocLayout);
        holder.scrollview = (ScrollView) convertView.findViewById(R.id.scrollView);
        //On insere le holder en tant que tag dans le layout
        convertView.setTag(holder);
    } else
        holder = (ViewHolder)convertView.getTag();

    //On recupere la commande en cours
    final Commande order = getItem(position);
    main_layout = new RelativeLayout(context);
    main_layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,getPixels(55)));
    Rect bounds = new Rect();
    main_layout.getDrawingRect(bounds);

    Rect scrollBounds = new Rect();
    holder.scrollview.getHitRect(scrollBounds);

    if(Rect.intersects(scrollBounds, bounds))
    {
      Log.d("ok","visible");
      //is  visible
    }
1

1 Answers

0
votes

In the below two statements, you didn't assign the scrolling point value in it.. hence, the ScrollBounds will hold the value null..

Rect scrollBounds = new Rect();
holder.scrollview.getHitRect(scrollBounds);

So try,

Example :

Rect scrollBounds = new Rect(scroll.getScrollX(), scroll.getScrollY(), scroll.getScrollX() + scroll.getWidth(), scroll.getScrollY() + scroll.getHeight());

Or try this code from that link..

Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (imageView.getLocalVisibleRect(scrollBounds)) {
// Any part of the imageView, even a single pixel, is within the visible window
} else {
// NONE of the imageView is within the visible window
}