我有一个充满Square ImageViews的GridView。我想检测屏幕上当前可见的那个。我使用的代码虽然不起作用,但似乎应该这样:
Rect scrollBounds = new Rect();
this.container.getHitRect(scrollBounds);
if(imageContainer.getLocalVisibleRect(scrollBounds)) {
Log.d(TAG, "MyPhoto with position " + position + " is visible.");
} else if(!imageView.getLocalVisibleRect(scrollBounds)) {
Log.d(TAG, "MyPhoto with position " + position + " out of sight.");
}
这是一个调试日志,包含我的活动指标:
11-30 20:27:11.080 (Loading) D/MyArrayAdaptor: MyPhoto with position 0 out of sight.
11-30 20:27:11.080 (Loading) D/MyArrayAdaptor: MyPhoto with position 0 out of sight.
11-30 20:27:11.081 (Loading) D/MyArrayAdaptor: MyPhoto with position 0 out of sight.
11-30 20:27:11.082 (Loading) D/MyArrayAdaptor: MyPhoto with position 1 out of sight.
11-30 20:27:11.083 (Loading) D/MyArrayAdaptor: MyPhoto with position 2 out of sight.
11-30 20:27:11.084 (Loading) D/MyArrayAdaptor: MyPhoto with position 3 out of sight.
11-30 20:27:11.084 (Loading) D/MyArrayAdaptor: MyPhoto with position 4 out of sight.
11-30 20:27:11.085 (Loading) D/MyArrayAdaptor: MyPhoto with position 5 out of sight.
11-30 20:27:11.086 (Loading) D/MyArrayAdaptor: MyPhoto with position 6 out of sight.
11-30 20:27:11.087 (Loading) D/MyArrayAdaptor: MyPhoto with position 7 out of sight.
11-30 20:27:11.093 (Loading) D/MyArrayAdaptor: MyPhoto with position 8 out of sight.
11-30 20:27:11.094 (Loading) D/MyArrayAdaptor: MyPhoto with position 9 out of sight.
11-30 20:27:11.095 (Loading) D/MyArrayAdaptor: MyPhoto with position 10 out of sight.
11-30 20:27:11.095 (Loading) D/MyArrayAdaptor: MyPhoto with position 11 out of sight.
11-30 20:27:11.096 (Loading) D/MyArrayAdaptor: MyPhoto with position 12 out of sight.
11-30 20:27:11.097 (Loading) D/MyArrayAdaptor: MyPhoto with position 13 out of sight.
11-30 20:27:11.097 (Loading) D/MyArrayAdaptor: MyPhoto with position 14 out of sight.
11-30 20:27:11.119 (Loading) D/MyArrayAdaptor: MyPhoto with position 0 out of sight.
11-30 20:27:11.119 (Loading) D/MyArrayAdaptor: MyPhoto with position 0 out of sight.
11-30 20:27:23.525 (Scroll to the bottom) D/MyArrayAdaptor: MyPhoto with position 15 out of sight.
11-30 20:27:23.526 (Scroll to the bottom) D/MyArrayAdaptor: MyPhoto with position 16 out of sight.
11-30 20:27:23.526 (Scroll to the bottom) D/MyArrayAdaptor: MyPhoto with position 17 out of sight.
11-30 20:27:26.019 (Scroll to the bottom) D/MyArrayAdaptor: MyPhoto with position 18 is visible.
11-30 20:27:29.888 (Scroll up) D/MyArrayAdaptor: MyPhoto with position 3 is visible.
11-30 20:27:29.888 (Scroll up) D/MyArrayAdaptor: MyPhoto with position 4 is visible.
11-30 20:27:29.888 (Scroll up) D/MyArrayAdaptor: MyPhoto with position 5 is visible.
11-30 20:27:30.992 (Scroll up) D/MyArrayAdaptor: MyPhoto with position 0 is visible.
11-30 20:27:30.992 (Scroll up) D/MyArrayAdaptor: MyPhoto with position 1 is visible.
11-30 20:27:30.992 (Scroll up) D/MyArrayAdaptor: MyPhoto with position 2 is visible.
11-30 20:27:35.220 (Scroll back down) D/MyArrayAdaptor: MyPhoto with position 15 is visible.
11-30 20:27:35.220 (Scroll back down) D/MyArrayAdaptor: MyPhoto with position 16 is visible.
11-30 20:27:35.220 (Scroll back down) D/MyArrayAdaptor: MyPhoto with position 17 is visible.
11-30 20:27:36.171 (Scroll back down) D/MyArrayAdaptor: MyPhoto with position 18 is visible.
这是一个简单的测试设置,没有任何图像,可以在一个完全干净的android项目中重现问题。
MainActivity.java
package scroll.test.com.scrolltest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
MyArrayAdaptor arrayAdaptor;
GridView gv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GridView) findViewById(R.id.list_list_photos);
arrayAdaptor = new MyArrayAdaptor(this, gv, getData());
gv.setAdapter(arrayAdaptor);
}
private List<MyPhoto> getData() {
List<MyPhoto> d = new ArrayList<>();
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
d.add(new MyPhoto());
return d;
}
}
MyArrayAdaptor.java:
package scroll.test.com.scrolltest;
import android.content.Context;
import android.graphics.Rect;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import java.util.List;
public class MyArrayAdaptor extends ArrayAdapter<MyPhoto> {
private final static String TAG = "MyArrayAdaptor";
private final Context context;
private final GridView container;
public MyArrayAdaptor (Context context, GridView container, List<MyPhoto> values) {
super(context, -1, values);
this.context = context;
this.container = container;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.photo, parent, false);
}
RelativeLayout imageContainer = (RelativeLayout) convertView.findViewById(R.id.list_container);
ImageView imageView = (ImageView) convertView.findViewById(R.id.list_image);
Rect scrollBounds = new Rect();
this.container.getHitRect(scrollBounds);
if(imageContainer.getLocalVisibleRect(scrollBounds)) {
Log.d(TAG, "MyPhoto with position " + position + " is visible.");
} else if(!imageView.getLocalVisibleRect(scrollBounds)) {
Log.d(TAG, "MyPhoto with position " + position + " out of sight.");
}
return convertView;
}
}
SquareImageView.java:
package scroll.test.com.scrolltest;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class SquareImageView extends ImageView
{
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}
MyPhoto.java:
package scroll.test.com.scrolltest;
public class MyPhoto { }
ActivityMain.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="scroll.test.com.scrolltest.MainActivity">
<GridView
android:id="@+id/list_list_photos"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:columnWidth="100dp"
android:numColumns="auto_fit" />
</RelativeLayout>
Photo.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_container">
<scroll.test.com.scrolltest.SquareImageView
android:id="@+id/list_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</RelativeLayout>