3

在相对布局内;我在 ScrollView 中有一个 TableLayout 和一个水平 LinearLayout。我的视图工作正常:随着表格在后台滚动,LinearLayout 在前台是静态的。我现在需要的是准确知道表格和 LinearLayout 在滚动期间相交的位置。

由于 LinearLayout 是静态的,所以我随时都知道它在哪里,比如 y=50pixel。假设我在 TableLayout 中有 TextView。因此,当表格滚动时,我想知道哪个 TextView(即 tableRow)与 LinearLayout 相交/隐藏。

4

2 回答 2

0

您是否尝试过View.getLocationOnScreen()在表格行上使用?

于 2013-08-13T02:08:44.180 回答
0

我在 y 偏移 = 310 像素处有一条水平线。当我滚动位于 ScrollView 内的 tableLayout 时,我想知道表格的哪一行与我的水平线相交。

您可以使用该View.getLocationInWindow()方法。最初,您需要查看哪一行与线相交,然后使用 custom ScrollView,按照滚动:

// in the onCreate:
scrollView.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    int[] loc = new int[2];
                    ll.getLocationInWindow(loc);
                    scrollView.setYIntersection(loc[1]);
                    ViewGroup table = (ViewGroup) scrollView.getChildAt(0);
                    // find out which row initially intersects our line so
                    // we can initialize mCrossedRow and position
                    for (int i = 0; i < table.getChildCount(); i++) {
                        final View row = table.getChildAt(i);
                        row.getLocationInWindow(loc);
                        if (loc[1] <= scrollView.getYIntersection()
                                && loc[1] + row.getHeight() >= scrollView
                                        .getYIntersection()) {
                            scrollView.setCurrIntersection(row, i);
                            row.setBackgroundColor(Color.RED);
                            break;
                        }
                    }
                    scrollView.getViewTreeObserver()
                            .removeOnGlobalLayoutListener(this);
                }
            });

习惯ScrollView是:

public class CustomScrollView extends ScrollView {

    /**
     * This will be the current intersected row.
     */
    private View mCrossedRow;
    /**
     * The row position of the intersected row, mCrossedRow.
     */
    private int mRowOrder;
    /**
     * The y value of the intersecting line.
     */
    private int mLine;
    /**
     * Used as a temporary holder for the location retrieval.
     */
    private int[] mLocHelper = new int[2];

    public CustomScrollView(Context context) {
        super(context);
    }

    public void setYIntersection(int y) {
        mLine = y;
    }

    public int getYIntersection() {
        return mLine;
    }

    public void setCurrIntersection(View row, int childPosition) {
        mCrossedRow = row;
        mRowOrder = childPosition;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        // this will be called every time the user scrolls so we need to
        // keep updating the position and see if the crossed row still
        // intersects the line otherwise move it to the next row.
        mCrossedRow.getLocationInWindow(mLocHelper);
        if (mLocHelper[1] <= mLine
                && mLocHelper[1] + mCrossedRow.getHeight() >= mLine) {
            // do nothing, we're still in the same row
        } else {
            if (t - oldt > 0) {
                // going down so increase the row position
                mRowOrder++;
            } else {
                // going up so decrease the row position
                mRowOrder--;
            }
            // visual effect, the intersecting row will have a red
            // background and all other rows will have a white background.
            // You could setup a listener here to get notified that a new
            // row intersects the line.
            mCrossedRow.setBackgroundColor(Color.WHITE);
            mCrossedRow = ((ViewGroup) getChildAt(0)).getChildAt(mRowOrder);
            mCrossedRow.setBackgroundColor(Color.RED);
        }
    }
}
于 2013-08-13T18:48:00.540 回答