2016-04-28 85 views
26

當我將RecycleView放置在嵌套滾動視圖中時,屏幕始終跳轉到recycleview的頂部而不是頁面的頂部。這是一個簡單的例子。RecycleView在NestedScrollView中竊取焦點

佈局的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> 

活動與虛擬適配器:

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

} 

在這個例子中,我有過recycleview一個350dp高大空的觀點,因爲你需要有在RecycleView一些內容這顯然很明顯。 RecycleView iteself包含100個虛擬文本視圖。

開始活動後,滾動位於RecycleView的頂部而不是頁面的頂部。它必須是LinearLayoutManager內的東西,但還沒有真正看過。

任何想法如何解決這個問題?

+0

[這裏同樣的問題,檢查解決方案(http://stackoverflow.com/a/41481237/1993001) –

回答

72

讓你的頂視圖可獲得焦點。 「RecyclerView將」focusableOnTouchMode「設置爲true,以在佈局過程中處理其兒童的焦點更改。」相關discussion of the issue

例子:

<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

謝謝,這確實解決了這個問題。 – breakline

+3

這也適用於我。在任何您的RecyclerView的父視圖中,只要將:android:focusable =「true」 android:focusableInTouchMode =「true」即可解決。 – Lancelot

+1

感謝lottttttt @ Amagi82。你救了我的一天 – MashukKhan

0

這樣來做:

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

不幸的是這並沒有爲我工作,同樣的結果 – breakline

+0

將此添加到您的recyclerView中XML:app:layout_behavior =「@ string/appbar_scrolling_view_behavior」 –

+0

做了這項工作,或有anymor問題? –

6

對於我接受的答案沒有工作。我解決這個問題通過增加該屬性對父:

android:descendantFocusability="blocksDescendants"

+0

它爲我工作。我在滾動視圖中使用回收站視圖。謝謝 –

+0

我的佈局和@GulnazGhanchi一樣。這對我很有用:-) –

+0

太好了,在垂直RecyclerView中使用水平RecyclerView(s)時也是有效的。當數據被綁定時,該項目將竊取焦點並導致列表執行小型「跳躍」,從而降低性能。像Horizo​​ntals ReyclerView佈局的根目錄中添加android:descendantFocusability =「blocksDescendants」的魅力。 – Slickelito

1

感謝@ Amagi82。你的回答幫助了我,但這還不夠。我添加了更多2個屬性。這爲我工作:

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