RecyclerView in the horizontal direction and recyclerview in the direction

Source: Internet
Author: User

RecyclerView in the horizontal direction and recyclerview in the direction

Recently, a project needs to achieve horizontal slide of a card type, but it cannot slide with gestures. After clicking the button on the card, it slides to the next card, so I want to use RecyclerView to implement it, remove its gesture slide, click the button, and then the code controls the slide to the next card.

The next step is step-by-step implementation.

1. Introduce RecyclerView.
compile'com.android.support:recyclerview-v7:25.+'
2. Use RecyclerView in the layout file.
<android.support.v7.widget.RecyclerView        android:id="@+id/recycler"        android:layout_width="match_parent"        android:layout_height="match_parent"/>
3. Create the item layout file for RecyclerView.
<? 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 = "next page" android: textSize = "24sp"/> </LinearLayout>
4. Implement 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. Set RecyclerView in the activity file.
    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. Set RecyclerView to horizontal.
mRecyclerViewLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
7. Do not move with gestures.
recyclerView.setOnTouchListener(new View.OnTouchListener() {    @Override         public boolean onTouch(View v, MotionEvent event) {              return true;         }});

However, there is a problem. When the item slides, click it and the item will be paused and no longer slide.
At this time, we need to deal with this problem. During the Sliding Process of items, we do not intercept click events, or intercept click events when they do not slide.

// Position is the ID of the item in RecyclerView. 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 ();}

 

Complete Project Code address: Https://github.com/ZhangMiao147/HorizontalRecyclerView

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.