1. 程式人生 > >Android 利用RecyclerView 的SnapHelper 實現滾輪效果

Android 利用RecyclerView 的SnapHelper 實現滾輪效果

在support V4的最新包中,谷歌加入了一個叫做SnapHelper的輔助類。這個類的作用是可以讓RecyclerView實現一些類似ViewPager的效果。我大致試了下,可以利用這個類實現滾輪選擇效果。如果所示:


核心程式碼如下:

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
        linearSnapHelper.attachToRecyclerView(mRecyclerView);
        mAdapter = new RecyclerViewAdapter(this);
        mRecyclerView.setAdapter(mAdapter);
其中Adapter程式碼如下:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
    private List<String> mDataList = new ArrayList<>();
    private Context mContext;

    public RecyclerViewAdapter(Context context) {
        mContext = context;

        for (int i = 0; i < 200; i++) {
            mDataList.add("" + i);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View headerView = View.inflate(mContext, R.layout.item, null);
        MyViewHolder headerHolder = new MyViewHolder(headerView);
        return headerHolder;

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textView.setText(mDataList.get(position));
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public MyViewHolder(View view) {
            super(view);
            textView = (TextView) view.findViewById(R.id.id_info);
        }
    }
}
item 的佈局檔案程式碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/left"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true" />

    <TextView
        android:id="@+id/id_info"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_toLeftOf="@+id/right"
        android:layout_toRightOf="@+id/left"
        android:background="#ffcccc"
        android:gravity="center"
        android:text="北京"
        android:textColor="#333333"
        android:textSize="18dp" />

    <View
        android:id="@+id/right"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true" />
</RelativeLayout>

補記:

系統還幫我們實現了一個PagerSnapHelper,可以用RecyclerView實現跟ViewPager一模一樣的效果:

PagerSnapHelper linearSnapHelper = new PagerSnapHelper();
具體的item 的XML 程式碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffffff">

    <TextView
        android:id="@+id/id_info"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="#ffcccc"
        android:gravity="center"
        android:text="北京"
        android:textColor="#333333"
        android:textSize="18dp" />

</RelativeLayout>