Android 获取NestedScrollView内部的第一个可见视图

Android 获取NestedScrollView内部的第一个可见视图,android,android-nestedscrollview,Android,Android Nestedscrollview,我还有一个问题 我正在尝试获取NestedScrollView中的第一个可见项。实际上,内部的第一个也是唯一的视图是LinearLayout(因为滚动视图只能容纳一个直接子视图),但我正在尝试获取其中的第一个可见项。我搜索了很多,但没有成功 通过这种方法,我希望避免在另一个RecyclerView中使用RecyclerView。我发现这是一个坏习惯 另外,我不想使用ListView或RecyclerView 提前感谢您提供此布局 <?xml version="1.0" encoding="

我还有一个问题

我正在尝试获取NestedScrollView中的第一个可见项。实际上,内部的第一个也是唯一的视图是LinearLayout(因为滚动视图只能容纳一个直接子视图),但我正在尝试获取其中的第一个可见项。我搜索了很多,但没有成功

通过这种方法,我希望避免在另一个RecyclerView中使用RecyclerView。我发现这是一个坏习惯

另外,我不想使用ListView或RecyclerView

提前感谢您提供此布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/nestedScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ly"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_1"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_2" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_3" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_4" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_5" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_6" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_7" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_8" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_9" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_10" />


    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

然后使用这个代码

  public class MainActivity extends AppCompatActivity {

    NestedScrollView nestedScrollView;
    LinearLayout linearLayout;

    ArrayList<Integer> childrenY = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //find view
        nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);
        linearLayout = (LinearLayout) nestedScrollView.getChildAt(0);


        //get Y Children and save
        for (int i = 0; i < linearLayout.getChildCount(); i++) {

            int childrenY = 0;
            for (int j = 0; j < i; j++) {
                TextView tv = (TextView) linearLayout.getChildAt(j);
                Log.i("--------", tv.getText().toString());
                childrenY += tv.getLayoutParams().height;
            }
            this.childrenY.add(childrenY);
        }

        //add Scroll Change Listener
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

                for (int i = 0; i < childrenY.size(); i++) {
                    if (scrollY < childrenY.get(i)) {
                        Log.i("++++++++++", ((TextView) linearLayout.getChildAt(i-1)).getText().toString());
                        return;
                    }
                }
            }
        });
    }
}
public类MainActivity扩展了AppCompatActivity{
NestedScrollView NestedScrollView;
线性布局线性布局;
ArrayList childrenY=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查找视图
nestedScrollView=(nestedScrollView)findViewById(R.id.nestedScrollView);
linearLayout=(linearLayout)nestedScrollView.getChildAt(0);
//带上孩子,拯救他们
对于(int i=0;i
用于此布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/nestedScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ly"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_1"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_2" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_3" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_4" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_5" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_6" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_7" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_8" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_9" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="text_10" />


    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

然后使用这个代码

  public class MainActivity extends AppCompatActivity {

    NestedScrollView nestedScrollView;
    LinearLayout linearLayout;

    ArrayList<Integer> childrenY = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //find view
        nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);
        linearLayout = (LinearLayout) nestedScrollView.getChildAt(0);


        //get Y Children and save
        for (int i = 0; i < linearLayout.getChildCount(); i++) {

            int childrenY = 0;
            for (int j = 0; j < i; j++) {
                TextView tv = (TextView) linearLayout.getChildAt(j);
                Log.i("--------", tv.getText().toString());
                childrenY += tv.getLayoutParams().height;
            }
            this.childrenY.add(childrenY);
        }

        //add Scroll Change Listener
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

                for (int i = 0; i < childrenY.size(); i++) {
                    if (scrollY < childrenY.get(i)) {
                        Log.i("++++++++++", ((TextView) linearLayout.getChildAt(i-1)).getText().toString());
                        return;
                    }
                }
            }
        });
    }
}
public类MainActivity扩展了AppCompatActivity{
NestedScrollView NestedScrollView;
线性布局线性布局;
ArrayList childrenY=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查找视图
nestedScrollView=(nestedScrollView)findViewById(R.id.nestedScrollView);
linearLayout=(linearLayout)nestedScrollView.getChildAt(0);
//带上孩子,拯救他们
对于(int i=0;i
如果要在NestedScrollView的内部LinearLayout中找到第一个可见项,可以尝试以下操作:

    if(nsv.getChildCount() > 0) {
        ViewGroup vg = (ViewGroup) nsv.getChildAt(0);
        for (int i = 0; i < vg.getChildCount(); i++) {
            if(vg.getChildAt(i).getVisibility()==View.VISIBLE)
                return vg.getChildAt(i);
        }
    }

    return null;
if(nsv.getChildCount()>0){
ViewGroup vg=(ViewGroup)nsv.getChildAt(0);
对于(int i=0;i

其中nsv是您的NestedScrollView。

如果您想在NestedScrollView的内部LinearLayout中找到第一个可见项,可以尝试以下方法:

    if(nsv.getChildCount() > 0) {
        ViewGroup vg = (ViewGroup) nsv.getChildAt(0);
        for (int i = 0; i < vg.getChildCount(); i++) {
            if(vg.getChildAt(i).getVisibility()==View.VISIBLE)
                return vg.getChildAt(i);
        }
    }

    return null;
if(nsv.getChildCount()>0){
ViewGroup vg=(ViewGroup)nsv.getChildAt(0);
对于(int i=0;i

nsv是您的嵌套滚动视图。

好的,我找到了一个解决方案:

        final Rect scrollBounds = new Rect();
        questionAndAnswerScroll.getHitRect(scrollBounds);
        questionAndAnswerScroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                for (int i = 0; i < questionAndAnswerHolder.getChildCount(); i++) {
                    View childView = questionAndAnswerHolder.getChildAt(i);
                    if (childView != null) {
                        if (childView.getLocalVisibleRect(scrollBounds)) {
                            //Here is the position of first visible view
                            positionToScroll = i;
                            break;
                        }
                    }
                }
            }
        });
final Rect scrollBounds=new Rect();
questionAndAnswerScroll.getHitRect(scrollBounds);
questionAndAnswerScroll.setOnScrollChangeListener(新嵌套的ScrollView.OnScrollChangeListener()文件){
@凌驾
public void onScrollChange(嵌套ScrollView v、int scrollX、int scrollY、int oldScrollX、int oldScrollY){
对于(int i=0;i
好的,我找到了一个解决方案:

        final Rect scrollBounds = new Rect();
        questionAndAnswerScroll.getHitRect(scrollBounds);
        questionAndAnswerScroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                for (int i = 0; i < questionAndAnswerHolder.getChildCount(); i++) {
                    View childView = questionAndAnswerHolder.getChildAt(i);
                    if (childView != null) {
                        if (childView.getLocalVisibleRect(scrollBounds)) {
                            //Here is the position of first visible view
                            positionToScroll = i;
                            break;
                        }
                    }
                }
            }
        });
final Rect scrollBounds=new Rect();
questionAndAnswerScroll.getHitRect(scrollBounds);
问题与回答