【问题标题】:Android: check if a View inside of ScrollView is visibleAndroid:检查 ScrollView 内的 View 是否可见
【发布时间】:2014-09-01 20:52:16
【问题描述】:

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

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

我在我的自定义适配器中尝试了这段代码,但它看起来不起作用,因为 bounds 的值总是 (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
    }

【问题讨论】:

标签: android scrollview


【解决方案1】:

在以下两个语句中,您没有在其中分配滚动点值.. 因此,ScrollBounds 将保持值为 null..

矩形滚动边界 = new Rect();
holder.scrollview.getHitRect(scrollBounds);

那就试试吧,

示例:

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

或者从那个链接试试这个代码..

矩形滚动边界 = new Rect();
scrollView.getHitRect(scrollBounds);
if (imageView.getLocalVisibleRect(scrollBounds)) {
// imageView 的任何部分,即使是单个像素,都在可见窗口内
} 其他 {
// 没有一个 imageView 在可见窗口内
}

【讨论】:

  • 并更改 main_layout.getDrawingRect(bounds);到 main_layout.getHitRect(bounds);
  • 在 onResume 之后调用它时我也得到 0,但如果在 onScroll 之后调用它就设置好了
猜你喜欢
  • 2011-06-05
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多