Android中RecyclerView设置边框并循环滚动
2017-04-01 00:41
501 查看
编写不易,如有转载,请声明出处: 梦回河口:http://blog.csdn.net/zxc514257857/article/details/68939809
注:RecyclerView,是放在support library v7包中,我们使用的时候需要导包。在build.gradle中的dependencies节点下面添加:compile ‘com.android.support:recyclerview-v7:+’
如图:
Demo下载请移步:http://download.csdn.net/detail/zxc514257857/9830679
———-因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!———-
1,Demo展示图片:
2,布局代码如下:
//(layout)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </RelativeLayout> ----------------------------------------------------------- //(layout)recyclerview_item.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/item_shape" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_letter" android:paddingTop="15dp" android:paddingBottom="15dp" android:text="A" android:textSize="30sp" android:textColor="#fff" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout> -------------------------------------------------------------- //(drawable)item_shape.xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10px"/> <solid android:color="@color/colorBlue"/> <stroke android:width="2px" android:color="#fff"/> </shape> <!--设置了圆角弧度 每个item的边框粗细及边框颜色和背景颜色-->
3,Activity代码如下:
//(activity)MainActivity import android.content.pm.ActivityInfo; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private static final String TAG = "MainActivity"; private static final int RECYCLERVIEWRollROLL = 1; private Handler mRecyclerViewHandler; private AtomicBoolean shouldContinue = new AtomicBoolean(false); private Thread thread = null; private List<String> mData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //强制设置为横屏 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); initView(); initData(); } private void initView(){ mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); //三列 mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3)); } private void initData(){ MyRollRecyclerViewAdapter mMyRollRecyclerViewAdapter = new MyRollRecyclerViewAdapter(); mRecyclerView.setAdapter(mMyRollRecyclerViewAdapter); mData = new ArrayList<>(); for (int i = 'A'; i <= 'Z'; i++){ mData.add("" + (char) i); } } class MyRollRecyclerViewAdapter extends RecyclerView.Adapter<MyRollRecyclerViewAdapter.MyRollRecyclerViewHolder> { @Override public MyRollRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { MyRollRecyclerViewHolder holder = new MyRollRecyclerViewHolder(LayoutInflater.from( MainActivity.this).inflate(R.layout.recyclerview_item, parent, false)); return holder; } @Override public void onBindViewHolder(MyRollRecyclerViewHolder holder, int position) { holder.mTv_letter.setText(mData.get(position)); } @Override public int getItemCount() { if (mData != null) { return mData.size(); } return 0; } class MyRollRecyclerViewHolder extends RecyclerView.ViewHolder { TextView mTv_letter; public MyRollRecyclerViewHolder(View view) { super(view); mTv_letter = (TextView) view.findViewById(R.id.tv_letter); } } } /**RecyclerView条目的滚动操作*/ private void initScroll() { /**主线程的handler,用于执行Marquee的滚动消息*/ mRecyclerViewHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case RECYCLERVIEWRollROLL: /**垂直移动偏移2个像素*/ mRecyclerView.scrollBy(0, 2); /**如果移动到底部*/ if (isSlideToBottom(mRecyclerView)) { Log.i(TAG, "滑动到了底部"); /**跳至顶部*/ mRecyclerView.scrollToPosition(0); /**如果没有移动到底部*/ } else { Log.i(TAG, "没有滑动到底部"); } break; } } }; if (thread == null) { thread = new Thread() { public void run() { while (shouldContinue.get()) { /**每次滚动睡眠0.2秒*/ SystemClock.sleep(200); mRecyclerViewHandler.sendEmptyMessage(RECYCLERVIEWRollROLL); } /**退出循环时清理handler*/ mRecyclerViewHandler = null; } }; } } @Override /**在附到窗口的时候开始滚动*/ public void onAttachedToWindow() { super.onAttachedToWindow(); shouldContinue.set(true); initScroll(); thread.start(); } /**判断Recycler是否滑动至最底部 是返回true 不是返回false*/ public static boolean isSlideToBottom(RecyclerView recyclerView) { if (recyclerView == null) return false; if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange()) return true; return false; } @Override /**在脱离窗口时处理相关内容*/ public void onDetachedFromWindow() { super.onDetachedFromWindow(); stopMarquee(); } /**停止滚动*/ public void stopMarquee() { shouldContinue.set(false); thread = null; } }
注:RecyclerView,是放在support library v7包中,我们使用的时候需要导包。在build.gradle中的dependencies节点下面添加:compile ‘com.android.support:recyclerview-v7:+’
如图:
Demo下载请移步:http://download.csdn.net/detail/zxc514257857/9830679
———-因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!———-
相关文章推荐
- Android使用Recyclerview实现图片水平自动循环滚动效果
- Android使用Recyclerview实现图片水平自动循环滚动效果
- Android RecyclerView 水平滚动+自动循环轮播
- android中TextView中如何设置水平滚动效果
- android ViewPager实现循环滚动效果
- Android给TextView设置透明背景、圆角边框
- Android自动滚动 轮播循环的ViewPager
- android中TextView中如何设置水平滚动效果
- Android给TextView,EditText等设置边框
- Android给TextView设置透明背景、圆角边框
- android如何写一个循环文字滚动的TextView
- Android给TextView,EditText等设置边框
- 关于Android使TextView可以滚动的设置
- android中TextView中如何设置水平滚动效果
- Android-RecyclerView-Item点击事件设置
- android中ImageView设置图片边框的方法
- Android设置TextView能够滚动
- android中TextView中如何设置水平滚动效果
- Android开发--ViewPager自动循环滚动,手动换页
- Android给TextView设置透明背景、圆角边框