写单元测试无法运行
答: 方法需要添加test前缀才会被认为是单元测试方法,才会执行测试
为什么要写单元测试
答: 单元测试可以单独测试模块,单元测试提供上下文Context对象,可以使用getContext()方法获得
怎么写单元测试
答:
- 写个类继承ApplicationTestCase
,并且调用父类构造方法 - 需要依赖testCompile 'junit:junit:4.12'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
}
public class MyDAOTest extends ApplicationTestCase<Application> {
public MyDAOTest() {
//必须调用父类构造方法,且需要传递一个Application.class
super(Application.class);
}
//添加前缀test,才会识别测试方法
public void testFun() {
//可以使用this.getContext()方法获得上下文Context
//以前必须出View才能测试也是因为没有上下文的原因
}
}
RecycleView项点击在android5.0机子上没有水波纹效果
答: 如果点击的组件是ViewGroup那么默认是不带水波纹效果,因此需要设置背景android:background="?android:attr/selectableItemBackground",但是只能在API为21的或者以上添加。兼容办法为做出两个layout
api-21版本之前
res/layout/xxx.xml
<RelativeLayout
android:clickable="true"
android:longClickable="true"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin">
</RelativeLayout>
api-21版本或者之后,添加android:background="?android:attr/selectableItemBackground"背景属性
res/layout-v21/xxx.xml
<RelativeLayout
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:longClickable="true"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin">
</RelativeLayout>