水平方向的RecyclerView
最近做了一个项目需要实现一个卡片式的水平滑动,但是不能手势滑动,点击卡片上的按钮之后滑动到下一个卡片,所以想到用RecyclerView实现,去掉它的手势滑动,点击按钮之后再代码控制滑动到下一个卡片。
接下来就是一步一步的实现。
1.引入RecyclerView。
compile'com.android.support:recyclerview-v7:25.+'
2.在布局文件中使用RecyclerView。
<android.support.v7.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent"/>
3.创建RecyclerView的item布局文件。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/item_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test" android:textColor="@android:color/black" android:textSize="36sp" /> <Button android:id="@+id/item_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="36dp" android:text="下一页" android:textSize="24sp" /> </LinearLayout> </LinearLayout>
4.实现RecyclerViewAdapter。
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerHolder> { private static final String TAG = RecyclerViewAdapter.class.getSimpleName(); private List<String> dataList; private Context mContext; private RecyclerView recyclerView; public RecyclerViewAdapter(Context context, List<String> dataList) { mContext = context; this.dataList = dataList; } @Override public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_view, parent, false); RecyclerHolder holder = new RecyclerHolder(view); return holder; } @Override public void onBindViewHolder(RecyclerHolder holder, final int position) { holder.textView.setText(dataList.get(position)); if (position == dataList.size() - 1) { holder.button.setVisibility(View.GONE); } holder.button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { recyclerView.smoothScrollToPosition(position + 1); } }); } @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); this.recyclerView = recyclerView; } @Override public int getItemCount() { return dataList.size(); } public class RecyclerHolder extends RecyclerView.ViewHolder { TextView textView; Button button; public RecyclerHolder(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.item_text); button = (Button) itemView.findViewById(R.id.item_button); } } }
5.在activity文件中设置RecyclerView。
private RecyclerView recyclerView; private LinearLayoutManager mRecyclerViewLayoutManager; private RecyclerViewAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recycler); List<String> datas = new ArrayList<>(); datas.add("LiMing"); datas.add("XiaoMing"); datas.add("HanMeiMei"); mRecyclerViewLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(mRecyclerViewLayoutManager); adapter = new RecyclerViewAdapter(this, datas); recyclerView.setAdapter(adapter); }
6.设置RecyclerView为水平方向。
mRecyclerViewLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
7.禁止手势滑动。
recyclerView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } });
但是这样是有一个问题的,在item滑动的时候点击一下,item就会暂停住,不再滑动。
这个时候就需要处理一下这个问题,在item滑动的过程,不拦截点击事件,不滑动的时候拦截点击事件。
//position是RecyclerView的item的编号。 recyclerView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.d(TAG,"position = "+RecyclerViewAdapter.position); if (isScroll) { recyclerView.smoothScrollToPosition(RecyclerViewAdapter.position); return false; } else { return true; } } }); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == 2) { isScroll = false; } if (newState == 1) { isScroll = true; } if (newState == 0) { isScroll = false; } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); } }); @Override protected void onDestroy() { Log.d(TAG,"onDestroy()"); position = 0; super.onDestroy(); }
项目的完整代码地址:https://github.com/ZhangMiao147/HorizontalRecyclerView