2017-07-10 2 views
-1

Ich habe ein nächstes Problem.Erste sichtbare Ansicht innerhalb von NestedScrollView

Ich versuche, das erste sichtbare Element innerhalb von NestedScrollView zu bekommen. Eigentlich ist die erste und einzige Ansicht innerhalb von LinearLayout (weil die Bildlaufansicht nur ein direktes Kind enthalten kann), aber ich versuche, das erste sichtbare Element darin zu bekommen. Ich habe viel gesucht, aber ohne Erfolg.

Mit diesem Ansatz möchte ich RecyclerView in einem anderen RecyclerView vermeiden. Ich fand es eine schlechte Übung.

P.S. Ich will nicht Listview oder RecyclerView verwenden

Vielen Dank im Voraus

Antwort

1

Okay, fand ich eine Lösung:

 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; 
         } 
        } 
       } 
      } 
     }); 
1

für dieses Layout

<?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> 

und verwenden diesen Code

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; 
        } 
       } 
      } 
     }); 
    } 
} 
+0

ich zum ersten Mal VISIBLE Kind Artikel erhalten möchten, nicht das erste Kind Artikel. –

+0

ändern Sie den Code, überprüfen Sie diese –

+0

Alle Kind Ansichten sind sichtbar (nicht gegangen), aber ich brauche zuerst sichtbar auf dem Bildschirm –

0

Wenn Sie die erste finden wollen sichtbares Element in der inneren LinearLayout von NestedScrollView, können Sie dies versuchen:

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; 

Wo NSV ist Ihre NestedScrollView.

+0

Alle Kinderansichten sind sichtbar (nicht GONE), aber ich brauche zuerst VISIBLE auf dem Bildschirm. –

Verwandte Themen