Android疑难杂症-自定义RecyclerView的LayoutManager

禁用RecyclerView手动滑动功能;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class PlanDateRecyclerView extends RecyclerView {
public PlanDateRecyclerView(Context context) {
super(context);
init();
}

public PlanDateRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}

public PlanDateRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
}

Android疑难杂证-解决ViewPager和Fragment中水平滑动控件冲突问题

终极解决方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import zhouzhuo810.me.zzandframe.ui.widget.ZzViewPager;

/**
* Created by zhouzhuo810 on 2018/4/11.
*/
public class NoConflictViewPager extends ZzViewPager {
public NoConflictViewPager(Context context) {
super(context);
}

public NoConflictViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
//这里添加冲突的控件类型
if (v instanceof PickerView) {
return true;
}
return super.canScroll(v, checkV, dx, x, y);
}
}

Android常用代码-再按一次退出App

核心代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
public void onBackPressed() {
if (isExit) {
finish();
} else {
isExit = true;
ToastUtils.showCustomBgToast(getString(R.string.press_angin_exit_text));
new Timer().schedule(new TimerTask() {
@Override
public void run() {
isExit = false;
}
}, 2000);
}
}

Android-8.0新特性-服务启动问题

问题描述

  • Android 8.0 启动后台service 出错 IllegalStateException: Not allowed to start service Intent

原因

  • 极光推送老版本sdk启动推送服务未兼容8.0才出现的问题;

Android O对应用在后台运行时可以执行的操作施加了限制,称为后台执行限制(Background Execution Limits),这可以大大减少应用的内存使用和耗电量,提高用户体验。后台执行限制分为两个部分:后台服务限制(Background Service Limitations)、广播限> 制(BroadcastLimitations)。

Android常用代码-自定义View和ViewGroup

一、自定义View

自定义属性

  • 以ZzHorizontalProgressBar为例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//attrs.xml
<declare-styleable name="ZzHorizontalProgressBar">
<!--大小-->
<attr name="zpb_padding" format="dimension" />
<!--颜色-->
<attr name="zpb_bg_color" format="color|reference" />
<attr name="zpb_pb_color" format="color|reference" />
<attr name="zpb_second_pb_color" format="color|reference" />
<!--整数-->
<attr name="zpb_max" format="integer" />
<attr name="zpb_progress" format="integer" />
<attr name="zpb_second_progress" format="integer" />
<!--布尔-->
<attr name="zpb_show_second_progress" format="boolean" />
<!--枚举-->
<attr name="zpb_show_second_point_shape" format="enum">
<enum name="point" value="0"/>
<enum name="line" value="1"/>
</attr>
</declare-styleable>

Android-TV开发-RecyclerView刷新焦点丢失以及局部刷新问题

问题1

使用GridLayoutManager时,多选的情况下,点击item刷新后,焦点被抢问题;

  • 解决方式
1
2
3
4
5
6
7
8
9
holder.setText(R.id.tv_name, data.get(position).getcName())
.setChecked(R.id.cb_choose, data.get(position).isChoosed())
.setOnClickListener(R.id.ll_cb, new View.OnClickListener() {
@Override
public void onClick(View v) {
data.get(position).setChoosed(!data.get(position).isChoosed());
notifyItemChanged(position); //关键代码
}
});

Android常用代码-横竖屏切换

自动切换

  • Android Manifest在需要切换的Activity中添加
1
2
android:configChanges="orientation|keyboardHidden|layoutDirection|screenSize|screenLayout"
android:screenOrientation="sensor"
  • res添加layout-land和layout-port文件夹,分别放两套布局。

  • Activity代码中实现

1
2
3
4
5
6
7
8
9
10
11
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
//切换到竖屏
setContentView(R.layout.activity_main);
}else{
//切换到横屏
setContentView(R.layout.activity_main);
}
}