Android:检查ScrollView内部的View是否可见

时间:2014-07-11 09:15:45

标签: android scrollview

我在gridview的每个项目中都有一个ScrollView。 在此滚动视图中,有一系列视图。我希望能够确定当前是否可以看到视图。

我已经看到了这个:Android: how to check if a View inside of ScrollView is visible?

我在自定义适配器中尝试此代码,但它看起来不起作用,因为边界的值总是(0,0,0,0)..

你有什么想法吗?

 @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 个答案:

答案 0 :(得分:0)

在以下两个陈述中,您没有在其中指定滚动点值。 因此,ScrollBounds将保持值null ..

  

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

所以试试,

示例:

  

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

或者从该链接尝试此代码..

  

Rect scrollBounds = new Rect();
  scrollView.getHitRect(scrollBounds);
  if(imageView.getLocalVisibleRect(scrollBounds)){
      // imageView的任何部分,甚至是单个像素,都在可见窗口内   其他{
      //没有imageView在可见窗口内   }