2016-04-28 13 views
26

Po umieszczeniu RecycleView wewnątrz zagnieżdżonego przewijania, ekran zawsze przeskakuje na szczyt widoku recycle zamiast na górę strony. Oto prosty przykład.RecycleView przechwytuje fokus, gdy wewnątrz NestedScrollView

układ xml:

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="350dp" 
      android:background="@android:color/holo_blue_dark"/> 
     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycleView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 
</android.support.v4.widget.NestedScrollView> 
</layout> 

aktywny z manekina adaptera:

public class RecycleViewTestActivity extends AppCompatActivity { 

public static class ExampleAdapter extends RecyclerView.Adapter<ExampleViewHolder> { 

    private Context context; 

    public ExampleAdapter(Context context) { 
     this.context = context; 
    } 

    @Override 
    public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     TextView view = new TextView(context); 
     view.setText("Test"); 
     return new ExampleViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(ExampleViewHolder holder, int position) { 

    } 

    @Override 
    public int getItemCount() { 
     return 100; 
    } 
} 

public static class ExampleViewHolder extends RecyclerView.ViewHolder { 

    public ExampleViewHolder(View itemView) { 
     super(itemView); 
    } 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_rectest); 
    RecyclerView view = (RecyclerView) findViewById(R.id.recycleView); 
    view.setNestedScrollingEnabled(false); 
    view.setLayoutManager(new LinearLayoutManager(this)); 
    ExampleAdapter adapter = new ExampleAdapter(this); 
    view.setAdapter(adapter); 
} 

} 

W tym przykładzie mam 350dp wysoki pusty widok na recycleview ponieważ trzeba mieć jakąś treść nad RecycleView dla to oczywiste. RecycleView sam zawiera 100 fałszywych odsyłaczy tekstowych.

Po uruchomieniu czynności przewijanie znajduje się u góry okna RecycleView, a nie u góry strony. To musi być coś wewnątrz LinearLayoutManager, ale naprawdę jeszcze nie wyglądało.

Wszelkie pomysły na rozwiązanie tego problemu?

+0

[ten sam problem rozwiązanie Sprawdź tutaj] (http://stackoverflow.com/a/41481237/1993001) –

Odpowiedz

72

Dokonaj widok z góry aktywowana. "RecyclerView ma" focusableOnTouchMode "ustawiony na wartość true, aby obsłużyć zmiany fokusów swoich dzieci podczas układania." Odpowiednie discussion of the issue.

Przykład:

<android.support.v4.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent""> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:focusableInTouchMode="true" 
     android:orientation="vertical"> 

     <View 
      android:id="@+id/someView" 
      android:layout_width="wrap_content" 
      android:layout_height="350dp"/> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recyclerView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

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

Dzięki, to rzeczywiście rozwiązuje ten problem. – breakline

+3

To też zadziałało dla mnie. Po prostu: android: focusable = "true" android: focusableInTouchMode = "true" w dowolnym widoku, który jest rodzicem twojego RecyclerView i który powinien go rozwiązać. – Lancelot

+1

Bardzo dziękuję @ Amagi82. Uratowałeś mój dzień – MashukKhan

0

zrobić to w ten sposób:

LinearLayoutManager lm = new LinearLayoutManager(this); 
lm.setAutoMeasureEnabled(true); 
view.setLayoutManager(lm) 
+0

Niestety to nie działa dla mnie, sam wynik – breakline

+0

dodaj to do twojego recyclerView w XML: app: layout_behavior = "@ string/appbar_scrolling_view_behavior" –

+0

czy to działa, czy są jakieś problemy? –

6

Dla mnie zaakceptowane odpowiedź nie działa. I rozwiązać ten problem poprzez dodanie tego atrybutu dla rodzica:

android:descendantFocusability="blocksDescendants"

+0

to zadziałało dla mnie. Korzystałem z widoku recyklera w widoku przewijania. dzięki –

+0

Miałem taki sam układ jak @GulnazGhanchi. To zadziałało dla mnie :-) –

+0

Świetnie, działa to również wtedy, gdy ma poziomy widok RecyclerView w pionowym widoku RecyclerView.Przedmiot wykrada fokus, gdy dane były powiązane i powoduje, że lista robi małe "przeskoki", które obniżały wydajność. Działa jak urok dodając android: descendantFocusability = "blocksDescendants" do katalogu głównego układu Horizonals ReyclerView. – Slickelito

1

Dzięki @ Amagi82. Twoja odpowiedź mi pomogła, ale to nie wystarczyło. Dodałem więcej 2 atrybutów. Że pracował dla mnie:

<android.support.v4.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent""> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:focusableInTouchMode="true" 
    android:focusable="true" 
    android:descendantFocusability="blocksDescendants" 
    android:orientation="vertical"> 

    <View 
     android:id="@+id/someView" 
     android:layout_width="wrap_content" 
     android:layout_height="350dp"/> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 

Powiązane problemy