private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.mRecyclerView);
mRecyclerView.setAdapter(new SplashAdapter(MainActivity.this));
mRecyclerView.setLayoutManager(new ScollLinearLayoutManager(MainActivity.this));
//smoothScrollToPosition滚动到某个位置(有滚动效果)
mRecyclerView.smoothScrollToPosition(Integer.MAX_VALUE / 2);
}
}
1. 无限循环
将RecyclerView的Item数量设置成很大的值,用smoothScrollToPosition方法滚到很远的位置,就能实现这样的效果,很多banner轮播图的实现也是如此;
public class SplashAdapter extends RecyclerView.Adapter<SplashAdapter.ViewHolder> {
private int imgWidth;
public SplashAdapter(Context context) {
imgWidth = EasyUtil.getScreenWidth(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_splash, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
/* ViewGroup.LayoutParams lp = holder.item_bg.getLayoutParams();
lp.width = imgWidth;
lp.height =imgWidth5;
holder.item_bg.setLayoutParams(lp);/
}
@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView item_bg;
public ViewHolder(final View itemView) {
super(itemView);
item_bg = itemView.findViewById(R.id.item_bg);
}
}
}
2.控制smoothScrollToPosition的滑动速度
参考RecyclerView调用smoothScrollToPosition() 控制滑动速度,修改MILLISECONDS_PER_INCH的值即可
/**
- 更改RecyclerView滚动的速度
*/
public class ScollLinearLayoutManager extends LinearLayoutManager {
private float MILLISECONDS_PER_INCH = 25f; //修改可以改变数据,越大速度越慢
private Context contxt;
public ScollLinearLayoutManager(Context context) {
super(context);
this.contxt = context;
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
LinearSmoothScroller linearSmoothScroller =
new LinearSmoothScroller(recyclerView.getContext()) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return ScollLinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel
(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.density;
//返回滑动一个pixel需要多少毫秒
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
//可以用来设置速度
public void setSpeedSlow(float x) {
//自己在这里用density去乘,希望不同分辨率设备上滑动速度相同
//0.3f是自己估摸的一个值,可以根据不同需求自己修改
MILLISECONDS_PER_INCH = contxt.getResources().getDisplayMetrics().density * 0.3f + (x);
}
}
3.图片宽度充满屏幕、高度按图片原始宽高比例自适应
@SuppressLint(“AppCompatCustomView”)
public class FitImageView extends ImageView {
public FitImageView(Context context) {
super(context);
}
public FitImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
Drawable drawable = getDrawable();
if(drawable!=null){
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) Math.ceil((float) width * (float) drawable.getIntrinsicHeight() / (float) drawable.getIntrinsicWidth());
setMeasuredDimension(width, height);
}else{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
4.这里需要注意的是、Item的根布局android:layout_height=“wrap_content”,否则图片高度会受限
<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”>
架构师筑基包括哪些内容
我花了将近半个月时间将:深入 Java 泛型.、注解深入浅出、并发编程.、数据传输与序列化、Java 虚拟机原理、反射与类加载、高效 IO、Kotlin项目实战等等Android架构师筑基必备技能整合成了一套系统知识笔记PDF,相信看完这份文档,你将会对这些Android架构师筑基必备技能有着更深入、更系统的理解。
由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容
注:资料与上面思维导图一起看会更容易学习哦!每个点每个细节分支,都有对应的目录内容与知识点!
这份资料就包含了所有Android初级架构师所需的所有知识!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
图片转存中…(img-OKRTNNNz-1715256918097)]
这份资料就包含了所有Android初级架构师所需的所有知识!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!