最近做了一個項目需要實現一個卡片式的水平滑動,但是不能手勢滑動,點擊卡片上的按鈕之後滑動到下一個卡片,所以想到用RecyclerView實現,去掉它的手勢滑動,點擊按鈕之後再代碼控制滑動到下一個卡片。 接下來就是一步一步的實現。 1.引入RecyclerView。 2.在佈局文件中使用Recycle ...
最近做了一個項目需要實現一個卡片式的水平滑動,但是不能手勢滑動,點擊卡片上的按鈕之後滑動到下一個卡片,所以想到用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