7
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RecyclerViewメモ

Last updated at Posted at 2018-01-29

NestedScrollView の中にある RecyclerView がスムーズにスクロールしない

RecyclerViewのandroid:layout_height="wrap_content"android:nestedScrollingEnabled="false" が重要
※たぶん4系だとダメ

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <!-- その他のView -->
                ・・・・・・・・

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:nestedScrollingEnabled="false" />
            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>

参考:Recyclerview inside ScrollView not scrolling smoothly

スクロールを無効にする

RecyclerViewをaddView()する場合はrecyclerView.setNestedScrollingEnabled(false);としてもスムーズにスクロールできない。
解決策として、スクロールを無効にする

解決策1:LayoutManagerを継承する

public class CustomGridLayoutManager extends LinearLayoutManager {
 private boolean isScrollEnabled = true;

 public CustomGridLayoutManager(Context context) {
  super(context);
 }

 public void setScrollEnabled(boolean flag) {
  this.isScrollEnabled = flag;
 }

 @Override
 public boolean canScrollVertically() {
  //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
  return isScrollEnabled && super.canScrollVertically();
 }
}

解決策2:匿名クラスでオーバーライド

 linearLayoutManager = new LinearLayoutManager(context) {
 @Override
 public boolean canScrollVertically() {
  return false;
 }
};

レイアウトでLayoutManagerを設定

LayoutManager のクラスを直接設定する

<android.support.v7.widget.RecyclerView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layoutManager="android.support.v7.widget.GridLayoutManager" >
android.support.v7.widget.GridLayoutManager
android.support.v7.widget.LinearLayoutManager

リップル対応

アイテムタップ時にリップルを表示させる。
リストアイテムのルートビューに以下を設定。

        android:clickable="true"
        android:focusable="true"
        android:foreground="?android:attr/selectableItemBackground"
(例)
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:background="#ffd7f6"
        android:foreground="?android:attr/selectableItemBackground">

レイアウトのプレビューにListItemのレイアウトを表示する

<android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:listitem="@android:layout/simple_list_item_checked" />

アイテム間のマージンを設定

参考:https://stackoverflow.com/questions/37507937/margin-between-items-in-recycler-view-android

RecyclerView.ItemDecorationクラス継承して作って

public class RecyclerViewMargin extends RecyclerView.ItemDecoration {
private final int columns;
private int margin;

/**
 * constructor
 * @param margin desirable margin size in px between the views in the recyclerView
 * @param columns number of columns of the RecyclerView
 */
public RecyclerViewMargin(@IntRange(from=0)int margin ,@IntRange(from=0) int columns ) {
    this.margin = margin;
    this.columns=columns;

}

/**
 * Set different margins for the items inside the recyclerView: no top margin for the first row
 * and no left margin for the first column.
 */
@Override
public void getItemOffsets(Rect outRect, View view,
                           RecyclerView parent, RecyclerView.State state) {

    int position = parent.getChildLayoutPosition(view);
    //set right margin to all
    outRect.right = margin;
    //set bottom margin to all
    outRect.bottom = margin;
    //we only add top margin to the first row
    if (position <columns) {
        outRect.top = margin;
    }
    //add left margin only to the first column
    if(position%columns==0){
        outRect.left = margin;
        }
    }
}

RecyclerViewに設定

 RecyclerViewMargin decoration = new RecyclerViewMargin(itemMargin, numColumns);
 recyclerView.addItemDecoration(decoration);
7
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?