C
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 = 0; super.onDestroy();
本文转自xmgdc51CTO博客,原文链接:http://blog.51cto.com/12953214/1940445
,如需转载请自行联系原作者