NestedScrollView 内に RecyclerView を追加すると、奇妙なスクロール動作が発生します。
何が起こるかというと、スクロールビューの行数が画面に表示できる行数を超えている場合、アクティビティが起動されるとすぐに、NestedScrollView は上からオフセットされた位置から開始されます (画像 1)。スクロールビュー内の項目数が少なく、一度にすべて表示できる場合は、このようなことは起こりません (画像 2)。
サポート ライブラリのバージョン 23.2.0 を使用しています。
画像1: 間違い - 上からのオフセットで始まる
画像2: 正解 - リサイクラービューにアイテムが少ない
レイアウト コードを以下に貼り付けます:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title:"
style="@style/TextAppearance.AppCompat.Caption"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/bodyPadding"
style="@style/TextAppearance.AppCompat.Body1"
android:text="Neque porro quisquam est qui dolorem ipsum"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtitle:"
style="@style/TextAppearance.AppCompat.Caption"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Body1"
android:padding="@dimen/bodyPadding"
android:text="Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
何か見落としているのでしょうか? これを修正する方法を知っている人はいますか?
アップデート1
アクティビティを初期化するときに次のコードを配置すると、正しく動作します。
sv.post(new Runnable() {
@Override
public void run() {
sv.scrollTo(0,0);
}
});
sv は NestedScrollView への参照ですが、かなりハックのように見えます。
アップデート2
ご要望に応じて、アダプタ コードを以下に示します。
public abstract class ArrayAdapter<T, VH extends RecyclerView.ViewHolder>
extends RecyclerView.Adapter<VH> {
private List<T> mObjects;
public ArrayAdapter(final List<T> objects) {
mObjects = objects;
}
/**
* Adds the specified object at the end of the array.
*
* @param object The object to add at the end of the array.
*/
public void add(final T object) {
mObjects.add(object);
notifyItemInserted(getItemCount() - 1);
}
/**
* Remove all elements from the list.
*/
public void clear() {
final int size = getItemCount();
mObjects.clear();
notifyItemRangeRemoved(0, size);
}
@Override
public int getItemCount() {
return mObjects.size();
}
public T getItem(final int position) {
return mObjects.get(position);
}
public long getItemId(final int position) {
return position;
}
/**
* Returns the position of the specified item in the array.
*
* @param item The item to retrieve the position of.
* @return The position of the specified item.
*/
public int getPosition(final T item) {
return mObjects.indexOf(item);
}
/**
* Inserts the specified object at the specified index in the array.
*
* @param object The object to insert into the array.
* @param index The index at which the object must be inserted.
*/
public void insert(final T object, int index) {
mObjects.add(index, object);
notifyItemInserted(index);
}
/**
* Removes the specified object from the array.
*
* @param object The object to remove.
*/
public void remove(T object) {
final int position = getPosition(object);
mObjects.remove(object);
notifyItemRemoved(position);
}
/**
* Sorts the content of this adapter using the specified comparator.
*
* @param comparator The comparator used to sort the objects contained in this adapter.
*/
public void sort(Comparator<? super T> comparator) {
Collections.sort(mObjects, comparator);
notifyItemRangeChanged(0, getItemCount());
}
}
これが私の ViewHolder です:
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView txt;
public ViewHolder(View itemView) {
super(itemView);
txt = (TextView) itemView;
}
public void render(String text) {
txt.setText(text);
}
}
RecyclerView 内の各項目のレイアウトは次のとおりです (android.R.layout.simple_spinner_item
この画面は、このバグの例を示すためだけのものです)。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
ベストアンサー1
私は次のように設定することでこの問題を解決しました:
<ImageView ...
android:focusableInTouchMode="true"/>
RecyclerView の上のビュー (不要なスクロール後に非表示になった) にこのプロパティを設定してみてください。このプロパティを RecyclerView の上の LinearLayout または RecyclerView のコンテナーである LinearLayout に設定してみてください (別のケースで役立ちました)。
NestedScrollView のソースを見ると、onRequestFocusInDescendants で最初の可能な子にフォーカスしようとし、RecyclerView だけがフォーカス可能な場合はそれが優先されます。
編集(Waranさんに感謝):スムーズなスクロールのために設定を忘れないでくださいyourRecyclerView.setNestedScrollingEnabled(false);