相关文章 Android 开发中不得不知道的 Tips 集合 (持续更新 ing)java
不少时候咱们须要给ImageView添加点击效果,例如title上的back按钮。android
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按压时 -->
<item android:drawable="@mipmap/btn_enter_pressed" android:state_pressed="true" />
<!-- 默认时 -->
<item android:drawable="@mipmap/btn_enter_normal" />
</selector>
复制代码
而后在布局文件中把Imageview的background属性设置成你写的Drawable文件。例如:git
<ImageView
android:id="@+id/tv_login"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/back_click" />
复制代码
这样固然没问题,毕竟都是你们熟悉的套路。不料,你忽然接到了一个需求,为了支持动态换肤,这个back的图片须要从网络上获取,而且仍然须要支持点击效果。顿时,无数程序猿心中众多那个啥在奔腾。github
解决方案: 继承ImageView,监听OnTouchListener的事件,动态设置setColorFilterbash
public class ClickImageView extends AppCompatImageView {
public ClickImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public ClickImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ClickImageView(Context context) {
super(context);
init();
}
private void init() {
setOnTouchListener(onTouchListener);
}
private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
setColorFilter(null);
break;
case MotionEvent.ACTION_DOWN:
changeLight();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_CANCEL:
setColorFilter(null);
break;
default:
break;
}
return false;
}
};
private void changeLight() {
int brightness = -80;
ColorMatrix matrix = new ColorMatrix();
matrix.set(new float[]{1, 0, 0, 0, brightness, 0, 1, 0, 0,
brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0});
setColorFilter(new ColorMatrixColorFilter(matrix));
}
}
复制代码
布局文件中这么搞妥了网络
<你的包名.ClickImageView
android:id="@+id/iv_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/act_ic_share" />
复制代码
如今APP里面怎么能少的了WebView的舞台呢?不过加载以下网页的时候会有坑ide
解决方案: 在WebActivity中控制一下WebView,亲测有效布局
@Override
protected void onResume() {
super.onResume();
wb_content.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
wb_content.destroy(); //手动销毁WebView
}
@Override
protected void onPause() {
super.onPause();
wb_content.onPause();
}
复制代码
还没玩过Rxjava的同窗们建议直接从Rxjava2学起,如今还在奋斗在Rxjava1的同窗们建议尽快转到Rxjava2的战线。Rxjava1很快就中止更新了。废话很少说,直接祭出官方wiki https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0post
不少场景下,产品须要咱们经过代码控制Recyclerview滑动到第几个position,例如:用户下拉刷新当天节目列表,咱们应该计算当前时间播放的是第几个节目,而后滑动到这个position,注:此时这个position应该居于屏幕的中间ui
解决方案: 这里只说一下LinearLayoutManager下的解决方式
public class CenterLayoutManager extends LinearLayoutManager {
public CenterLayoutManager(Context context) {
super(context);
}
public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CenterLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private static class CenterSmoothScroller extends LinearSmoothScroller {
CenterSmoothScroller(Context context) {
super(context);
}
@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
}
}
}
复制代码
而后使用Recyclerview的时候,设置LayoutManager为CenterLayoutManager。须要滚动到第几个item直接调用
recyclerview.smoothScrollToPosition(position);
复制代码
就妥啦。效果以下。
contact way | value |
---|---|
weixinjie1993@gmail.com | |
W2006292 | |
github | https://github.com/weixinjie |
blog | https://juejin.im/user/57673c83207703006bb92bf6 |