Android疑难杂症-ViewPager+分组列表+指示器悬停

系统自带方案

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<ImageView
android:id="@+id/main.backdrop"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:scaleType="centerCrop"
android:src="@drawable/material_img"
app:layout_collapseMode="parallax" />

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:layout_collapseMode="pin" />

</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="50dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/my_txt"
android:textSize="20sp" />

</android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

此方案不利于屏幕适配,故不采用。

Androidd开源控件-Glide

Glide 4.3.1 (4.x用起来更麻烦了)

集成

1
2
3
4
5
6
7
8
9
repositories {
mavenCentral()
maven { url 'https://maven.google.com' }
}

dependencies {
compile 'com.github.bumptech.glide:glide:4.3.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'
}

Android常用代码-App更新

APP更新逻辑

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
private void checkUpdate() {
String key = SpUtil.getKey();

Api.getApi0()
.CheckUpdate(key, SystemUtil.getPackageInfo(this).versionCode)
.compose(RxHelper.<CheckUpdateResult>io_main())
.subscribe(new Subscriber<CheckUpdateResult>() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(CheckUpdateResult checkUpdateResult) {
if (checkUpdateResult.getCode() == 1) {
CheckUpdateResult.DataEntity data = checkUpdateResult.getData();
showUpdate(data);
}
}
});
}

private void showUpdate(final CheckUpdateResult.DataEntity data) {
if (data != null) {
showTwoBtnDialog("更新", "v" + data.getVersionName() + "\n" + data.getUpdateInfo(), false, new IBaseActivity.OnTwoBtnClick() {
@Override
public void onOk() {
final TextView[] tv = new TextView[1];
final ProgressBar[] pb = new ProgressBar[1];
showUpdateDialog("更新", "即将开始下载...", false, new IBaseActivity.OnOneBtnClickListener() {
@Override
public void onProgress(TextView textView, ProgressBar progressBar) {
tv[0] = textView;
pb[0] = progressBar;
}

@Override
public void onOK() {
}
});
String url = Constants.API + File.separator + data.getUrl();
final String name = "HighNetFix_" + data.getVersionName() + "_" + data.getVersionCode() + ".apk";
OkGo.<File>get(url)
.tag(this)
.execute(new FileCallback(Constants.APK_DOWNLOAD_PATH, name) {
@Override
public void onSuccess(Response<File> response) {
hideUpdateDialog();
ApkUtils.installApk(HomePageActivity.this, BuildConfig.APPLICATION_ID, Constants.APK_DOWNLOAD_PATH, name);
}

@Override
public void onError(Response<File> response) {
hideUpdateDialog();
ToastUtils.showCustomBgToast("下载失败,请检查您的网络设置。");
}

@Override
public void downloadProgress(Progress progress) {
long currentSize = progress.currentSize;
long totalSize = progress.totalSize;
int pro = (int) (currentSize * 100.0 / totalSize + 0.5f);
tv[0].setText("已下载:" + pro + "%");
pb[0].setProgress(pro);
}
});
}

@Override
public void onCancel() {
}
});
}
}

AndroidStudio技巧-mac上导入导出SVN项目

Android Studio -> SVN

  • 1.打开项目
  • 2.Preferences -> Version Control -> Ignored Files
    (1)app/build
    (2)build
    (3)*.iml
  • 3.VCS -> import into version control -> import into subversion
  • 4.Submit

SVN -> Android Studio

  • 1.Check out project from version control
  • 2.选择Subversion
  • 3.选中项目文件夹根目录
  • 4.选择保存路径(非中文路径,新建一个项目文件夹)
  • 5.等待完成(如果打开会报错,大多是找不到gradle,别急)。
  • 6.复制其他项目的local.properties文件到项目文件夹。
  • 7.open a new Android Studio project打开项目

Android疑难杂症-ViewPager禁止左右滑动

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.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
* Created by zhouzhuo810 on 2017/10/31.
*/
public class NoScrollViewPager extends ViewPager {
public NoScrollViewPager(Context context) {
super(context);
}

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

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
return true;
}
}

Java工具类-二维码生成和解析

zxing生成和解析二维码

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>

Android常用代码-微信QQ分享文件

不使用SDK实现QQ和微信文件与文字的分享。

QQ分享文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void shareFileToQQ(File file) throws Exception{
Intent share = new Intent(Intent.ACTION_SEND);
ComponentName component = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
share.setComponent(component);
Uri uri = null;
if (Build.VERSION.SDK_INT > 23) {
uri = FileProvider.getUriForFile(getActivity(),
BuildConfig.APPLICATION_ID + ".provider",
file);
} else {
uri = Uri.fromFile(file);
}
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("*/*");
startActivity(Intent.createChooser(share, "发送"));
}