相關文章 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 |