├── .gitignore ├── README.md ├── apk └── gank_1.0.apk ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── github │ │ └── weiss │ │ └── example │ │ ├── App.java │ │ ├── BaseActivity.java │ │ ├── C.java │ │ ├── HttpResult.java │ │ ├── adapter │ │ └── GankViewProvider.java │ │ ├── api │ │ ├── Api.java │ │ └── ApiService.java │ │ ├── entity │ │ ├── Gank.java │ │ └── UserModel.java │ │ ├── ui │ │ ├── EasyPermissionsActivity.java │ │ ├── GankDetailsActivity.java │ │ ├── MainActivity.java │ │ ├── MainActivityFragment.java │ │ ├── PictureActivity.java │ │ ├── SplashActivity.java │ │ ├── ToolbarActivity.java │ │ └── WebActivity.java │ │ ├── util │ │ ├── JsHandler.java │ │ └── SnackbarUtil.java │ │ └── view │ │ ├── RatioImageView.java │ │ ├── behavior │ │ ├── helper │ │ │ ├── HeaderScrollingViewBehavior.java │ │ │ ├── MathUtils.java │ │ │ ├── ViewOffsetBehavior.java │ │ │ └── ViewOffsetHelper.java │ │ └── uc │ │ │ ├── UcNewsContentBehavior.java │ │ │ ├── UcNewsHeaderPagerBehavior.java │ │ │ ├── UcNewsTabBehavior.java │ │ │ └── UcNewsTitleBehavior.java │ │ └── webview │ │ ├── CommonWebChromeClient.java │ │ ├── CommonWebView.java │ │ ├── CommonWebViewClient.java │ │ └── LoveVideoView.java │ └── res │ ├── drawable │ ├── black_bg.xml │ ├── ic_copy.png │ └── ic_share.png │ ├── layout │ ├── activity_gank_details.xml │ ├── activity_main.xml │ ├── activity_picture.xml │ ├── activity_splash.xml │ ├── activity_web.xml │ ├── content_main.xml │ ├── fragment_main.xml │ ├── item_gank.xml │ ├── navigation_content.xml │ ├── recyclerview_base.xml │ ├── view_load_more.xml │ └── view_toolbar.xml │ ├── menu │ ├── menu_main.xml │ └── menu_web.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── bg.jpg │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── core ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── publish.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── github │ │ └── weiss │ │ └── core │ │ ├── ContainerActivity.java │ │ ├── UserManager.java │ │ ├── api │ │ ├── BasicParamsInterceptor.java │ │ ├── HttpsUtils.java │ │ ├── NobodyConverterFactory.java │ │ ├── NullableResult.java │ │ ├── ProgressRequestBody.java │ │ ├── RetryIntercepter.java │ │ ├── cookie │ │ │ ├── CookieJarImpl.java │ │ │ └── store │ │ │ │ ├── CookieStore.java │ │ │ │ ├── MemoryCookieStore.java │ │ │ │ ├── PersistentCookieStore.java │ │ │ │ └── SerializableHttpCookie.java │ │ ├── download │ │ │ ├── DownLoadStateBean.java │ │ │ ├── DownLoadSubscriber.java │ │ │ ├── ProgressCallBack.java │ │ │ └── ProgressResponseBody.java │ │ └── interceptor │ │ │ ├── BaseInterceptor.java │ │ │ ├── CacheInterceptor.java │ │ │ ├── ProgressInterceptor.java │ │ │ └── logging │ │ │ ├── I.java │ │ │ ├── Level.java │ │ │ ├── Logger.java │ │ │ ├── LoggingInterceptor.java │ │ │ └── Printer.java │ │ ├── base │ │ ├── AppManager.java │ │ ├── BackHandledFragment.java │ │ ├── BaseApp.java │ │ ├── BaseCoreActivity.java │ │ ├── BaseFragment.java │ │ ├── BaseLazyFragment.java │ │ ├── BaseRxActivity.java │ │ └── BaseRxFragment.java │ │ ├── bus │ │ ├── RxBus.java │ │ ├── RxBusSubscriber.java │ │ ├── RxManager.java │ │ ├── RxSubscriptions.java │ │ └── event │ │ │ ├── SingleLiveEvent.java │ │ │ └── SnackbarMessage.java │ │ ├── cache │ │ └── ICache.java │ │ ├── crash │ │ ├── CaocConfig.java │ │ ├── CaocInitProvider.java │ │ ├── CustomActivityOnCrash.java │ │ └── DefaultErrorActivity.java │ │ ├── entity │ │ ├── BaseHttpResult.java │ │ ├── Entity.java │ │ ├── IListBean.java │ │ ├── ListEntity.java │ │ ├── LoadMore.java │ │ └── NoBodyEntity.java │ │ ├── utils │ │ ├── ImageLoaderUtil.java │ │ └── helper │ │ │ ├── BackHandlerHelper.java │ │ │ ├── CircleTransform.java │ │ │ ├── FragmentAdapter.java │ │ │ ├── FragmentBackHandler.java │ │ │ ├── GlideCircleTransform.java │ │ │ ├── RxException.java │ │ │ ├── RxHandle.java │ │ │ ├── RxSchedulers.java │ │ │ └── RxSubscriber.java │ │ └── view │ │ ├── ButterKnifeViewHolder.java │ │ ├── LoadMoreDelegate.java │ │ ├── LoadMoreViewBinder.java │ │ ├── MultiStateView.java │ │ ├── OnRcvScrollListener.java │ │ └── PtrRecyclerView.java │ └── res │ ├── drawable-hdpi │ └── customactivityoncrash_error_image.png │ ├── drawable-mdpi │ └── customactivityoncrash_error_image.png │ ├── drawable-xhdpi │ └── customactivityoncrash_error_image.png │ ├── drawable-xxhdpi │ └── customactivityoncrash_error_image.png │ ├── drawable-xxxhdpi │ └── customactivityoncrash_error_image.png │ ├── layout │ ├── customactivityoncrash_default_error_activity.xml │ ├── item_load_more.xml │ └── recyclerview_base.xml │ ├── values │ ├── attrs.xml │ ├── colors.xml │ ├── dimens.xml │ ├── ids.xml │ └── strings.xml │ └── xml │ └── network_security_config.xml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── screenshot └── core.jpg └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | .DS_Store 4 | /captures 5 | .externalNativeBuild 6 | **.iml 7 | .idea 8 | /bintray.properties 9 | **/build 10 | .idea/ 11 | -------------------------------------------------------------------------------- /apk/gank_1.0.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xm1nam0/RxCore/595f08ee480e17d7b4ec93bed3e5e8929434c8e2/apk/gank_1.0.apk -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'com.jakewharton.butterknife' 3 | //apply plugin: 'me.tatarka.retrolambda' 4 | 5 | android { 6 | def globalConfiguration = rootProject.extensions.getByName("ext") 7 | 8 | compileSdkVersion globalConfiguration["androidCompileSdkVersion"] 9 | defaultConfig { 10 | applicationId "com.github.weiss.example" 11 | minSdkVersion globalConfiguration["androidMinSdkVersion"] 12 | targetSdkVersion globalConfiguration["androidTargetSdkVersion"] 13 | versionCode globalConfiguration["androidVersionCode"] 14 | versionName globalConfiguration["androidVersionName"] 15 | /* externalNativeBuild { 16 | cmake { 17 | cppFlags "-std=c++11 -frtti -fexceptions" 18 | } 19 | }*/ 20 | /* jackOptions { 21 | enabled true 22 | } 23 | useJack(true)*/ 24 | } 25 | 26 | buildTypes { 27 | release { 28 | minifyEnabled false 29 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 30 | } 31 | } 32 | /* externalNativeBuild { 33 | cmake { 34 | path "CMakeLists.txt" 35 | } 36 | }*/ 37 | 38 | compileOptions { 39 | sourceCompatibility JavaVersion.VERSION_1_8 40 | targetCompatibility JavaVersion.VERSION_1_8 41 | } 42 | 43 | lintOptions { 44 | abortOnError false 45 | } 46 | } 47 | 48 | dependencies { 49 | implementation fileTree(include: ['*.jar'], dir: 'libs') 50 | implementation 'com.tencent.bugly:crashreport:2.4.0' 51 | implementation 'in.srain.cube:ultra-ptr:1.0.11' 52 | implementation 'com.github.chrisbanes.photoview:library:1.2.3' 53 | implementation 'pub.devrel:easypermissions:2.0.0' 54 | 55 | implementation "androidx.appcompat:appcompat:$rootProject.ext.supportXVersion" 56 | implementation "com.google.android.material:material:$rootProject.ext.supportXVersion" 57 | implementation "androidx.recyclerview:recyclerview:$rootProject.ext.supportXVersion" 58 | implementation "androidx.legacy:legacy-support-v4:$rootProject.ext.supportXVersion" 59 | 60 | annotationProcessor "com.jakewharton:butterknife-compiler:$rootProject.ext.butterknifeVersion" 61 | implementation "com.jakewharton:butterknife:$rootProject.ext.butterknifeVersion" 62 | implementation 'com.github.bumptech.glide:glide:4.10.0' 63 | 64 | implementation "io.reactivex.rxjava2:rxjava:$rootProject.ext.rxjava2Version" 65 | implementation "com.squareup.retrofit2:retrofit:$rootProject.ext.retrofit2Version" 66 | implementation "com.squareup.retrofit2:adapter-rxjava2:$rootProject.ext.retrofit2Version" 67 | implementation "com.squareup.retrofit2:converter-gson:$rootProject.ext.retrofit2Version" 68 | // api 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' 69 | implementation 'com.squareup.okhttp3:logging-interceptor:3.6.0' 70 | implementation "io.reactivex.rxjava2:rxandroid:$rootProject.ext.rxAndroidVersion" 71 | implementation 'in.srain.cube:ultra-ptr:1.0.11' 72 | implementation 'com.drakeet.multitype:multitype:4.1.1' 73 | debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5' 74 | releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5' 75 | implementation "com.google.code.gson:gson:$rootProject.ext.gsonVersion" 76 | implementation "com.flyco.systembar:FlycoSystemBar_Lib:1.0.0@aar" 77 | implementation project(':core') 78 | implementation "com.github.0xm1nam0:Common:$rootProject.ext.commonVersion" 79 | } 80 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in F:\SDK\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 22 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 36 | 37 | 38 | 40 | 42 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/App.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example; 2 | 3 | import com.github.weiss.core.base.BaseApp; 4 | import com.github.weiss.core.UserManager; 5 | import com.github.weiss.core.crash.CaocConfig; 6 | import com.github.weiss.example.entity.UserModel; 7 | import com.github.weiss.example.ui.MainActivity; 8 | import com.squareup.leakcanary.LeakCanary; 9 | 10 | import butterknife.ButterKnife; 11 | 12 | /** 13 | * Created by Weiss on 2017/1/10. 14 | */ 15 | 16 | public class App extends BaseApp { 17 | 18 | public static UserManager userManager; 19 | 20 | @Override 21 | public void onCreate() { 22 | super.onCreate(); 23 | ButterKnife.setDebug(true); 24 | // CrashReport.initCrashReport(getApplicationContext(), "", false); 25 | userManager = new UserManager<>(UserModel.class); 26 | initCrash(); 27 | if (LeakCanary.isInAnalyzerProcess(this)) { 28 | // This process is dedicated to LeakCanary for heap analysis. 29 | // You should not init your app in this process. 30 | return; 31 | } 32 | LeakCanary.install(this); 33 | } 34 | 35 | private void initCrash() { 36 | CaocConfig.Builder.create() 37 | .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //背景模式,开启沉浸式 38 | .enabled(true) //是否启动全局异常捕获 39 | .showErrorDetails(true) //是否显示错误详细信息 40 | .showRestartButton(true) //是否显示重启按钮 41 | .trackActivities(true) //是否跟踪Activity 42 | .minTimeBetweenCrashesMs(2000) //崩溃的间隔时间(毫秒) 43 | .errorDrawable(R.mipmap.ic_launcher) //错误图标 44 | .restartActivity(MainActivity.class) //重新启动后的activity 45 | // .errorActivity(YourCustomErrorActivity.class) //崩溃后的错误activity 46 | // .eventListener(new YourCustomEventListener()) //崩溃后的错误监听 47 | .apply(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example; 2 | 3 | import com.blankj.utilcode.util.LogUtils; 4 | import com.blankj.utilcode.util.ToastUtils; 5 | import com.github.weiss.core.base.BaseRxActivity; 6 | import com.github.weiss.core.entity.BaseHttpResult; 7 | 8 | /** 9 | * Created by Weiss on 2017/1/17. 10 | */ 11 | 12 | public abstract class BaseActivity extends BaseRxActivity { 13 | 14 | //token失效处理 15 | public void tokenInvalid() { 16 | LogUtils.d("tokenInvalid"); 17 | App.userManager.logout(); 18 | ToastUtils.showShort("请安全退出,重新登录账号"); 19 | 20 | } 21 | 22 | //是否登录 23 | protected boolean isLogin() { 24 | return false; 25 | } 26 | 27 | //是否HandleResult 28 | protected boolean needHandleResult(BaseHttpResult result) { 29 | if (result.isTokenInvalid()) { 30 | tokenInvalid(); 31 | return true; 32 | } else if (!isLogin()) { 33 | //执行登录操作 34 | return true; 35 | } else { 36 | return false; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/C.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example; 2 | 3 | /** 4 | * 常量 5 | * Created by Weiss on 2017/1/10. 6 | */ 7 | 8 | public class C { 9 | //base 10 | public static final int PAGE_COUNT = 10; 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/HttpResult.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example; 2 | 3 | import com.github.weiss.core.entity.BaseHttpResult; 4 | 5 | /** 6 | * Created by Weiss on 2017/1/11. 7 | */ 8 | 9 | public class HttpResult extends BaseHttpResult { 10 | public String code; 11 | public String msg; 12 | public boolean hasmore; 13 | public T results; 14 | public boolean error; 15 | 16 | public static String SUCCESS = "000"; 17 | public static String SIGN_OUT = "101";//token验证失败 18 | public static String SHOW_TOAST = "102";//显示Toast 19 | 20 | // public boolean isSuccess() { 21 | // return SUCCESS.equals(code); 22 | // } 23 | 24 | public boolean isSuccess() { 25 | return !error; 26 | } 27 | 28 | public boolean isTokenInvalid() { 29 | return SIGN_OUT.equals(code); 30 | } 31 | 32 | public boolean isShowToast() { 33 | return SHOW_TOAST.equals(code); 34 | } 35 | 36 | @Override 37 | public String getCode() { 38 | return code; 39 | } 40 | 41 | @Override 42 | public String getMsg() { 43 | return msg; 44 | } 45 | 46 | @Override 47 | public T getData() { 48 | return results; 49 | } 50 | 51 | public boolean hasMore() { 52 | return hasmore; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/adapter/GankViewProvider.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.adapter; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Build; 6 | import androidx.annotation.NonNull; 7 | import androidx.core.app.ActivityCompat; 8 | import androidx.core.app.ActivityOptionsCompat; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.TextView; 13 | 14 | import com.drakeet.multitype.ItemViewBinder; 15 | import com.github.weiss.core.utils.ImageLoaderUtil; 16 | import com.github.weiss.core.view.ButterKnifeViewHolder; 17 | import com.github.weiss.example.R; 18 | import com.github.weiss.example.entity.Gank; 19 | import com.github.weiss.example.ui.GankDetailsActivity; 20 | import com.github.weiss.example.ui.PictureActivity; 21 | import com.github.weiss.example.view.RatioImageView; 22 | 23 | import butterknife.BindView; 24 | 25 | /** 26 | * Created by Weiss on 2017/2/8. 27 | */ 28 | public class GankViewProvider extends ItemViewBinder { 29 | 30 | private String type; 31 | 32 | public GankViewProvider(String type) { 33 | this.type = type; 34 | } 35 | 36 | @NonNull 37 | @Override 38 | public ViewHolder onCreateViewHolder( 39 | @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) { 40 | View root = inflater.inflate(R.layout.item_gank, parent, false); 41 | return new ViewHolder(root); 42 | } 43 | 44 | @Override 45 | public void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Gank gank) { 46 | holder.title.setText(gank.desc); 47 | holder.image.setOriginalSize(50, 50); 48 | ImageLoaderUtil.loadGifImg(holder.image, gank.imageUrl); 49 | holder.image.setOnClickListener(view -> startPictureActivity(gank, view)); 50 | holder.itemView.setOnClickListener(view -> startWebActivity(gank, holder.itemView)); 51 | } 52 | 53 | private void startPictureActivity(Gank gank, View transitView) { 54 | Intent intent = PictureActivity.newIntent(transitView.getContext(), gank.imageUrl, gank.desc); 55 | ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation( 56 | (Activity) transitView.getContext(), transitView, PictureActivity.TRANSIT_PIC); 57 | try { 58 | ActivityCompat.startActivity((Activity) transitView.getContext(), intent, optionsCompat.toBundle()); 59 | } catch (IllegalArgumentException e) { 60 | e.printStackTrace(); 61 | transitView.getContext().startActivity(intent); 62 | } 63 | } 64 | 65 | 66 | private void startWebActivity(Gank gankItem, View itemView) { 67 | if (type.equals("休息视频")) { 68 | // VideoWebActivity.launch(getActivity(), gankItem.getUrl()); 69 | } else if (type.equals("福利")) { 70 | 71 | } else { 72 | Intent intent = GankDetailsActivity.start((Activity) itemView.getContext(), gankItem.url, gankItem.desc, gankItem.imageUrl, gankItem.who); 73 | ActivityOptionsCompat mActivityOptionsCompat; 74 | if (Build.VERSION.SDK_INT >= 21) { 75 | mActivityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation( 76 | (Activity) itemView.getContext(), itemView.findViewById(R.id.image), GankDetailsActivity.TRANSIT_PIC); 77 | } else { 78 | mActivityOptionsCompat = ActivityOptionsCompat.makeScaleUpAnimation( 79 | itemView.findViewById(R.id.image), 0, 0, 80 | itemView.findViewById(R.id.image).getWidth(), 81 | itemView.findViewById(R.id.image).getHeight()); 82 | } 83 | ActivityCompat.startActivity((Activity) itemView.getContext(), intent, mActivityOptionsCompat.toBundle()); 84 | } 85 | } 86 | 87 | 88 | static class ViewHolder extends ButterKnifeViewHolder { 89 | @BindView(R.id.title) 90 | TextView title; 91 | @BindView(R.id.image) 92 | RatioImageView image; 93 | 94 | ViewHolder(View itemView) { 95 | super(itemView); 96 | } 97 | } 98 | } -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/api/Api.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.api; 2 | 3 | 4 | import com.blankj.utilcode.util.LogUtils; 5 | import com.blankj.utilcode.util.NetworkUtils; 6 | import com.github.weiss.example.App; 7 | import com.google.gson.Gson; 8 | import com.google.gson.GsonBuilder; 9 | 10 | import java.io.File; 11 | import java.io.IOException; 12 | import java.util.concurrent.TimeUnit; 13 | 14 | import okhttp3.Cache; 15 | import okhttp3.CacheControl; 16 | import okhttp3.Interceptor; 17 | import okhttp3.OkHttpClient; 18 | import okhttp3.Request; 19 | import okhttp3.Response; 20 | import okhttp3.logging.HttpLoggingInterceptor; 21 | import retrofit2.Retrofit; 22 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; 23 | import retrofit2.converter.gson.GsonConverterFactory; 24 | 25 | /** 26 | * Created by weiss on 2016/12/23. 27 | */ 28 | public class Api { 29 | 30 | public static final String BASE_URL = "http://gank.io/api/"; 31 | public static final int DEFAULT_TIMEOUT = 7676; 32 | 33 | public Retrofit retrofit; 34 | public ApiService service; 35 | 36 | //构造方法私有 37 | private Api() { 38 | HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 39 | interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 40 | 41 | File cacheFile = new File(App.getAppContext().getCacheDir(), "cache"); 42 | Cache cache = new Cache(cacheFile, 1024 * 1024 * 20); //20Mb 43 | 44 | OkHttpClient okHttpClient = new OkHttpClient.Builder() 45 | .readTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS) 46 | .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS) 47 | .addInterceptor(interceptor) 48 | .addNetworkInterceptor(new HttpCacheInterceptor()) 49 | .cache(cache) 50 | .build(); 51 | 52 | 53 | Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").serializeNulls().create(); 54 | 55 | retrofit = new Retrofit.Builder() 56 | .client(okHttpClient) 57 | .addConverterFactory(GsonConverterFactory.create(gson)) 58 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 59 | .baseUrl(BASE_URL) 60 | .build(); 61 | service = retrofit.create(ApiService.class); 62 | } 63 | 64 | //在访问HttpMethods时创建单例 65 | private static class SingletonHolder { 66 | private static final Api INSTANCE = new Api(); 67 | } 68 | 69 | //获取单例 70 | public static Api getInstance() { 71 | return SingletonHolder.INSTANCE; 72 | } 73 | 74 | 75 | class HttpCacheInterceptor implements Interceptor { 76 | 77 | @Override 78 | public Response intercept(Chain chain) throws IOException { 79 | Request request = chain.request(); 80 | if (!NetworkUtils.isConnected()) { 81 | request = request.newBuilder() 82 | .cacheControl(CacheControl.FORCE_CACHE) 83 | .build(); 84 | LogUtils.d("Okhttp", "no network"); 85 | } 86 | 87 | Response originalResponse = chain.proceed(request); 88 | if (NetworkUtils.isConnected()) { 89 | //有网的时候读接口上的@Headers里的配置,你可以在这里进行统一的设置 90 | String cacheControl = request.cacheControl().toString(); 91 | return originalResponse.newBuilder() 92 | .header("Cache-Control", cacheControl) 93 | .removeHeader("Pragma") 94 | .build(); 95 | } else { 96 | return originalResponse.newBuilder() 97 | .header("Cache-Control", "public, only-if-cached, max-stale=2419200") 98 | .removeHeader("Pragma") 99 | .build(); 100 | } 101 | } 102 | } 103 | } -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/api/ApiService.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.api; 2 | 3 | import com.github.weiss.example.HttpResult; 4 | import com.github.weiss.example.entity.Gank; 5 | 6 | import java.util.List; 7 | 8 | import io.reactivex.Flowable; 9 | import io.reactivex.Observable; 10 | import retrofit2.http.GET; 11 | import retrofit2.http.Path; 12 | 13 | public interface ApiService { 14 | 15 | @GET("data/福利/10/{page}") 16 | Flowable>> getMeizhiData( 17 | @Path("page") int page); 18 | 19 | @GET("random/data/福利/10") 20 | Flowable>> getRandMeizhiData(); 21 | 22 | @GET("data/{gank}/10/{page}") 23 | Observable>> getGankData(@Path("gank") String gank, 24 | @Path("page") int page); 25 | 26 | @GET("search/query/{query}/category/{category}/count/10/page/{page}") 27 | Flowable>> getSearch(@Path("query") String query, @Path("category") String category, 28 | @Path("page") int page); 29 | 30 | @GET("data/福利/10/{page}") 31 | Flowable>> getMeizhiList(@Path("page") int page); 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/entity/Gank.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.entity; 2 | 3 | import com.github.weiss.core.entity.ListEntity; 4 | import com.github.weiss.core.utils.helper.RxSchedulers; 5 | import com.github.weiss.example.HttpResult; 6 | import com.github.weiss.example.api.Api; 7 | import com.minamo.utils.CollectionUtils; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | import io.reactivex.Observable; 13 | import io.reactivex.functions.BiFunction; 14 | 15 | /** 16 | * Created by Weiss on 2017/1/20. 17 | */ 18 | 19 | public class Gank extends ListEntity { 20 | 21 | public String _id; 22 | 23 | public String createdAt; 24 | 25 | public String desc; 26 | 27 | public String publishedAt; 28 | 29 | public String source; 30 | 31 | public String type; 32 | 33 | public String url; 34 | 35 | public String imageUrl; 36 | 37 | public boolean used; 38 | 39 | public String who; 40 | 41 | public List images; 42 | 43 | @Override 44 | public Observable>> getPage(int page) { 45 | return Api.getInstance().service.getGankData(param.get("gank"), page) 46 | .zipWith(Api.getInstance().service.getGankData("福利", page), 47 | (BiFunction>, HttpResult>, HttpResult>>) (listHttpResult, listHttpResult2) -> { 48 | HttpResult zipItems = new HttpResult(); 49 | Gank zipItem; 50 | List zipResults = new ArrayList(); 51 | 52 | for (int i = 0; i < listHttpResult2.results.size(); i++) { 53 | zipItem = new Gank(); 54 | Gank item = listHttpResult2.results.get(i); 55 | Gank gankInfo = listHttpResult.results.get(i); 56 | if (CollectionUtils.isEmpty(gankInfo.images)) { 57 | zipItem.imageUrl = item.url; 58 | } else { 59 | zipItem.imageUrl = gankInfo.images.get(0); 60 | } 61 | zipItem.url = gankInfo.url; 62 | zipItem.desc = gankInfo.desc; 63 | zipItem.who = gankInfo.who; 64 | zipResults.add(zipItem); 65 | } 66 | zipItems.results = zipResults; 67 | return zipItems; 68 | }) 69 | .compose(RxSchedulers.io_main()); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/entity/UserModel.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.entity; 2 | 3 | /** 4 | * author weiss 5 | * email kleinminamo@gmail.com 6 | * created 2018/1/30. 7 | */ 8 | public class UserModel { 9 | 10 | public int id; 11 | 12 | public UserModel(int id) { 13 | this.id = id; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/ui/EasyPermissionsActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.ui; 2 | 3 | import android.Manifest; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import androidx.annotation.NonNull; 7 | import androidx.appcompat.app.AppCompatActivity; 8 | import android.util.Log; 9 | 10 | import com.github.weiss.example.BaseActivity; 11 | 12 | import java.util.List; 13 | 14 | import pub.devrel.easypermissions.AfterPermissionGranted; 15 | import pub.devrel.easypermissions.AppSettingsDialog; 16 | import pub.devrel.easypermissions.EasyPermissions; 17 | 18 | public abstract class EasyPermissionsActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks, 19 | EasyPermissions.RationaleCallbacks { 20 | 21 | private static final String TAG = "EasyPermissionsActivity"; 22 | private static final String[] PERMISSION = { 23 | Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA 24 | }; 25 | 26 | private static final int RC_PERM = 124; 27 | private boolean isFirst = false; 28 | 29 | 30 | @Override 31 | protected void onCreate(Bundle savedInstanceState) { 32 | super.onCreate(savedInstanceState); 33 | isFirst = true; 34 | } 35 | 36 | @Override 37 | protected void onResume() { 38 | super.onResume(); 39 | if (isFirst) { 40 | //因为要通过一个Fragment来弹出弹出框,所以activity这里的onResume执行了两次,这里进行判断 41 | isFirst = false; 42 | permissionsTask(); 43 | 44 | } 45 | } 46 | 47 | private boolean hasPermissions() { 48 | return EasyPermissions.hasPermissions(this, PERMISSION); 49 | } 50 | 51 | private boolean hasStoragePermission() { 52 | return EasyPermissions.hasPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); 53 | } 54 | 55 | 56 | @AfterPermissionGranted(RC_PERM) 57 | public void permissionsTask() { 58 | if (hasPermissions()) { 59 | startMainActivity(); 60 | // Have permissions, do the thing! 61 | } else { 62 | // Ask for both permissions 63 | EasyPermissions.requestPermissions( 64 | this, 65 | "需要获取以下权限,才能正常使用APP", 66 | RC_PERM, 67 | PERMISSION); 68 | } 69 | } 70 | 71 | public abstract void startMainActivity(); 72 | 73 | @Override 74 | public void onRequestPermissionsResult(int requestCode, 75 | @NonNull String[] permissions, 76 | @NonNull int[] grantResults) { 77 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 78 | 79 | // EasyPermissions handles the request result. 80 | EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); 81 | } 82 | 83 | //接受的权限List 84 | @Override 85 | public void onPermissionsGranted(int requestCode, @NonNull List perms) { 86 | Log.d(TAG, "onPermissionsGranted:" + requestCode + ":" + perms.size()); 87 | } 88 | 89 | //拒绝的权限List 90 | @Override 91 | public void onPermissionsDenied(int requestCode, @NonNull List perms) { 92 | Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size()); 93 | // (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN." 94 | // This will display a dialog directing them to enable the permission in app settings. 95 | if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) { 96 | //这个方法有个前提是,用户点击了“不再询问”后,才判断权限没有被获取到 97 | new AppSettingsDialog.Builder(this) 98 | .setRationale("没有该权限,此应用程序可能无法正常工作。打开应用设置界面以修改应用权限") 99 | .setTitle("必需权限") 100 | .build() 101 | .show(); 102 | isFirst = true; 103 | } else if (!hasPermissions()) { 104 | //这里响应的是除了AppSettingsDialog这个弹出框,剩下的两个弹出框被拒绝或者取消的效果 105 | finish(); 106 | } 107 | } 108 | 109 | @Override 110 | public void onActivityResult(int requestCode, int resultCode, Intent data) { 111 | super.onActivityResult(requestCode, resultCode, data); 112 | if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) { 113 | if (!hasPermissions()) { 114 | finish(); 115 | } 116 | } 117 | } 118 | 119 | @Override 120 | public void onRationaleAccepted(int requestCode) { 121 | Log.d(TAG, "onRationaleAccepted:" + requestCode); 122 | } 123 | 124 | @Override 125 | public void onRationaleDenied(int requestCode) { 126 | Log.d(TAG, "onRationaleDenied:" + requestCode); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/ui/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.ui; 2 | 3 | import com.google.android.material.floatingactionbutton.FloatingActionButton; 4 | import com.google.android.material.snackbar.Snackbar; 5 | import androidx.fragment.app.FragmentManager; 6 | import androidx.fragment.app.FragmentTransaction; 7 | import androidx.appcompat.widget.Toolbar; 8 | import android.view.Menu; 9 | import android.view.MenuItem; 10 | import android.view.View; 11 | 12 | import com.github.weiss.example.BaseActivity; 13 | import com.github.weiss.example.R; 14 | 15 | public class MainActivity extends BaseActivity { 16 | private MainActivityFragment fragment; 17 | 18 | @Override 19 | protected int getLayoutId() { 20 | return R.layout.activity_main; 21 | } 22 | 23 | @Override 24 | protected void initView() { 25 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 26 | // Example of a call to a native method 27 | toolbar.setTitle("RxCore"); 28 | setSupportActionBar(toolbar); 29 | 30 | FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 31 | fab.setOnClickListener(new View.OnClickListener() { 32 | @Override 33 | public void onClick(View view) { 34 | Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 35 | .setAction("Action", null).show(); 36 | } 37 | }); 38 | 39 | FragmentManager fm = getSupportFragmentManager(); 40 | FragmentTransaction transaction = fm.beginTransaction(); 41 | fragment = MainActivityFragment.newInstance("Android"); 42 | transaction.replace(R.id.fragment, fragment); 43 | transaction.commit(); 44 | } 45 | 46 | @Override 47 | public boolean onCreateOptionsMenu(Menu menu) { 48 | // Inflate the menu; this adds items to the action bar if it is present. 49 | getMenuInflater().inflate(R.menu.menu_main, menu); 50 | return true; 51 | } 52 | 53 | @Override 54 | public boolean onOptionsItemSelected(MenuItem item) { 55 | // Handle action bar item clicks here. The action bar will 56 | // automatically handle clicks on the Home/Up button, so long 57 | // as you specify a parent activity in AndroidManifest.xml. 58 | int id = item.getItemId(); 59 | 60 | //noinspection SimplifiableIfStatement 61 | if (id == R.id.action_settings) { 62 | return true; 63 | } 64 | 65 | return super.onOptionsItemSelected(item); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/ui/MainActivityFragment.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.ui; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import androidx.recyclerview.widget.RecyclerView; 6 | import androidx.recyclerview.widget.StaggeredGridLayoutManager; 7 | 8 | import com.drakeet.multitype.MultiTypeAdapter; 9 | import com.github.weiss.core.ContainerActivity; 10 | import com.github.weiss.core.base.BackHandledFragment; 11 | import com.github.weiss.core.view.PtrRecyclerView; 12 | import com.github.weiss.example.R; 13 | import com.github.weiss.example.adapter.GankViewProvider; 14 | import com.github.weiss.example.entity.Gank; 15 | 16 | import butterknife.BindView; 17 | 18 | /** 19 | * A placeholder fragment containing a simple view. 20 | */ 21 | public class MainActivityFragment extends BackHandledFragment { 22 | 23 | private static String TYPE = "type"; 24 | 25 | @BindView(R.id.baseRecyclerView) 26 | PtrRecyclerView ptrRecyclerView; 27 | 28 | private MultiTypeAdapter adapter; 29 | private String type; 30 | 31 | 32 | public static void startContainerActivity(Context context,String type) { 33 | Bundle args = new Bundle(); 34 | args.putString(TYPE, type); 35 | ContainerActivity.startContainerActivity(context,MainActivityFragment.class.getCanonicalName(),args); 36 | } 37 | 38 | public static MainActivityFragment newInstance(String type) { 39 | MainActivityFragment fragment = new MainActivityFragment(); 40 | Bundle args = new Bundle(); 41 | args.putString(TYPE, type); 42 | fragment.setArguments(args); 43 | return fragment; 44 | } 45 | 46 | 47 | @Override 48 | protected int getLayoutId() { 49 | return R.layout.fragment_main; 50 | } 51 | 52 | protected void parseArguments(Bundle bundle) { 53 | type = bundle.getString(TYPE); 54 | } 55 | 56 | @Override 57 | protected void initView() { 58 | parseArguments(getArguments()); 59 | ptrRecyclerView.setParam("gank", type); 60 | StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); 61 | //RecyclerView滑动过程中不断请求layout的Request,不断调整item见的间隙,并且是在item尺寸显示前预处理,因此解决RecyclerView滑动到顶部时仍会出现移动问题 62 | layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE); 63 | ptrRecyclerView.setLayoutManager(layoutManager); 64 | ptrRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 65 | @Override 66 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 67 | super.onScrollStateChanged(recyclerView, newState); 68 | layoutManager.invalidateSpanAssignments(); 69 | } 70 | }); 71 | adapter = new MultiTypeAdapter(); 72 | adapter.register(Gank.class, new GankViewProvider(type)); 73 | ptrRecyclerView.setAdapter(adapter, new Gank()); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/ui/PictureActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Drakeet 3 | * 4 | * This file is part of Meizhi 5 | * 6 | * Meizhi is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Meizhi is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Meizhi. If not, see . 18 | */ 19 | 20 | package com.github.weiss.example.ui; 21 | 22 | import android.content.Context; 23 | import android.content.Intent; 24 | import androidx.core.view.ViewCompat; 25 | import androidx.appcompat.app.AlertDialog; 26 | import android.view.Menu; 27 | import android.view.MenuItem; 28 | 29 | import com.github.weiss.core.utils.ImageLoaderUtil; 30 | import com.github.weiss.example.R; 31 | 32 | import butterknife.BindView; 33 | import uk.co.senab.photoview.PhotoView; 34 | import uk.co.senab.photoview.PhotoViewAttacher; 35 | 36 | public class PictureActivity extends ToolbarActivity { 37 | 38 | public static final String EXTRA_IMAGE_URL = "image_url"; 39 | public static final String EXTRA_IMAGE_TITLE = "image_title"; 40 | public static final String TRANSIT_PIC = "picture"; 41 | 42 | @BindView(R.id.picture) 43 | PhotoView mImageView; 44 | 45 | PhotoViewAttacher mPhotoViewAttacher; 46 | String mImageUrl, mImageTitle; 47 | 48 | 49 | @Override 50 | public int getLayoutId() { 51 | return R.layout.activity_picture; 52 | } 53 | 54 | @Override 55 | public boolean canBack() { 56 | return true; 57 | } 58 | 59 | 60 | public static Intent newIntent(Context context, String url, String desc) { 61 | Intent intent = new Intent(context, PictureActivity.class); 62 | intent.putExtra(PictureActivity.EXTRA_IMAGE_URL, url); 63 | intent.putExtra(PictureActivity.EXTRA_IMAGE_TITLE, desc); 64 | return intent; 65 | } 66 | 67 | 68 | private void parseIntent() { 69 | mImageUrl = getIntent().getStringExtra(EXTRA_IMAGE_URL); 70 | mImageTitle = getIntent().getStringExtra(EXTRA_IMAGE_TITLE); 71 | } 72 | 73 | 74 | @Override 75 | public void initView() { 76 | super.initView(); 77 | parseIntent(); 78 | ViewCompat.setTransitionName(mImageView, TRANSIT_PIC); 79 | ImageLoaderUtil.loadGifImg(mImageView, mImageUrl); 80 | setAppBarAlpha(0.7f); 81 | setTitle(mImageTitle); 82 | setupPhotoAttacher(); 83 | } 84 | 85 | /* @Override 86 | public void initPresenter() { 87 | 88 | }*/ 89 | 90 | 91 | private void setupPhotoAttacher() { 92 | mPhotoViewAttacher = new PhotoViewAttacher(mImageView); 93 | mPhotoViewAttacher.setOnViewTapListener((view, v, v1) -> hideOrShowToolbar()); 94 | // @formatter:off 95 | mPhotoViewAttacher.setOnLongClickListener(v -> { 96 | new AlertDialog.Builder(PictureActivity.this) 97 | .setMessage(getString(R.string.ask_saving_picture)) 98 | .setNegativeButton(android.R.string.cancel, 99 | (dialog, which) -> dialog.dismiss()) 100 | .setPositiveButton(android.R.string.ok, 101 | (dialog, which) -> { 102 | saveImageToGallery(); 103 | dialog.dismiss(); 104 | }) 105 | .show(); 106 | return true; 107 | }); 108 | } 109 | 110 | 111 | private void saveImageToGallery() { 112 | } 113 | 114 | 115 | @Override 116 | public boolean onCreateOptionsMenu(Menu menu) { 117 | return true; 118 | } 119 | 120 | 121 | @Override 122 | public boolean onOptionsItemSelected(MenuItem item) { 123 | int id = item.getItemId(); 124 | switch (id) { 125 | } 126 | 127 | return super.onOptionsItemSelected(item); 128 | } 129 | 130 | 131 | @Override 132 | public void onResume() { 133 | super.onResume(); 134 | } 135 | 136 | 137 | @Override 138 | public void onPause() { 139 | super.onPause(); 140 | } 141 | 142 | 143 | @Override 144 | public void onDestroy() { 145 | super.onDestroy(); 146 | mPhotoViewAttacher.cleanup(); 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/ui/SplashActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.ui; 2 | 3 | import android.content.Intent; 4 | import android.os.Handler; 5 | import android.os.Message; 6 | 7 | import com.flyco.systembar.SystemBarHelper; 8 | import com.github.weiss.example.BaseActivity; 9 | import com.github.weiss.example.R; 10 | 11 | /** 12 | * author weiss 13 | * email kleinminamo@gmail.com 14 | * created 2017/12/15. 15 | */ 16 | public class SplashActivity extends EasyPermissionsActivity { 17 | 18 | @Override 19 | protected int getLayoutId() { 20 | //隐藏标题栏以及状态栏 21 | /* getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 22 | WindowManager.LayoutParams.FLAG_FULLSCREEN); 23 | *//**标题是属于View的,所以窗口所有的修饰部分被隐藏后标题依然有效,需要去掉标题**//* 24 | requestWindowFeature(Window.FEATURE_NO_TITLE);*/ 25 | return R.layout.activity_splash; 26 | } 27 | 28 | @Override 29 | protected void initView() { 30 | SystemBarHelper.immersiveStatusBar(this); 31 | } 32 | 33 | @Override 34 | protected void onDestroy() { 35 | handler.removeMessages(0); 36 | super.onDestroy(); 37 | } 38 | 39 | private Handler handler = new Handler() { 40 | @Override 41 | public void handleMessage(Message msg) { 42 | getHome(); 43 | super.handleMessage(msg); 44 | } 45 | }; 46 | 47 | public void getHome() { 48 | Intent intent = new Intent(SplashActivity.this, MainActivity.class); 49 | startActivity(intent); 50 | finish(); 51 | } 52 | 53 | @Override 54 | public void startMainActivity() { 55 | handler.sendEmptyMessageDelayed(0, 1500); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/ui/ToolbarActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Drakeet 3 | * 4 | * This file is part of Meizhi 5 | * 6 | * Meizhi is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Meizhi is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Meizhi. If not, see . 18 | */ 19 | 20 | package com.github.weiss.example.ui; 21 | 22 | import android.os.Build; 23 | import com.google.android.material.appbar.AppBarLayout; 24 | import androidx.appcompat.app.ActionBar; 25 | import androidx.appcompat.widget.Toolbar; 26 | import android.view.MenuItem; 27 | import android.view.animation.DecelerateInterpolator; 28 | 29 | import com.github.weiss.example.BaseActivity; 30 | import com.github.weiss.example.R; 31 | 32 | import butterknife.BindView; 33 | 34 | 35 | public abstract class ToolbarActivity extends BaseActivity { 36 | 37 | public void onToolbarClick() { 38 | } 39 | 40 | @BindView(R.id.app_bar_layout) 41 | protected AppBarLayout mAppBar; 42 | @BindView(R.id.toolbar) 43 | protected Toolbar mToolbar; 44 | protected boolean mIsHidden = false; 45 | 46 | 47 | /* @Override protected void onCreate(Bundle savedInstanceState) { 48 | super.onCreate(savedInstanceState); 49 | setContentView(provideContentViewId()); 50 | mAppBar = (AppBarLayout) findViewById(R.id.app_bar_layout); 51 | mToolbar = (Toolbar) findViewById(R.id.toolbar); 52 | if (mToolbar == null || mAppBar == null) { 53 | throw new IllegalStateException( 54 | "The subclass of ToolbarActivity must contain a toolbar."); 55 | } 56 | mToolbar.setOnClickListener(v -> onToolbarClick()); 57 | setSupportActionBar(mToolbar); 58 | 59 | if (canBack()) { 60 | ActionBar actionBar = getSupportActionBar(); 61 | if (actionBar != null) actionBar.setDisplayHomeAsUpEnabled(true); 62 | } 63 | if (Build.VERSION.SDK_INT >= 21) { 64 | mAppBar.setElevation(10.6f); 65 | } 66 | }*/ 67 | 68 | @Override 69 | public void initView() { 70 | if (mToolbar == null || mAppBar == null) { 71 | throw new IllegalStateException( 72 | "The subclass of ToolbarActivity must contain a toolbar."); 73 | } 74 | mToolbar.setOnClickListener(v -> onToolbarClick()); 75 | setSupportActionBar(mToolbar); 76 | 77 | if (canBack()) { 78 | ActionBar actionBar = getSupportActionBar(); 79 | if (actionBar != null) actionBar.setDisplayHomeAsUpEnabled(true); 80 | } 81 | if (Build.VERSION.SDK_INT >= 21) { 82 | mAppBar.setElevation(10.6f); 83 | } 84 | } 85 | 86 | public boolean canBack() { 87 | return false; 88 | } 89 | 90 | 91 | @Override 92 | public boolean onOptionsItemSelected(MenuItem item) { 93 | if (item.getItemId() == android.R.id.home) { 94 | onBackPressed(); 95 | return true; 96 | } else { 97 | return super.onOptionsItemSelected(item); 98 | } 99 | } 100 | 101 | 102 | protected void setAppBarAlpha(float alpha) { 103 | mAppBar.setAlpha(alpha); 104 | } 105 | 106 | 107 | protected void hideOrShowToolbar() { 108 | mAppBar.animate() 109 | .translationY(mIsHidden ? 0 : -mAppBar.getHeight()) 110 | .setInterpolator(new DecelerateInterpolator(2)) 111 | .start(); 112 | mIsHidden = !mIsHidden; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/ui/WebActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.ui; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import androidx.appcompat.app.ActionBar; 6 | import androidx.appcompat.widget.Toolbar; 7 | import android.text.TextUtils; 8 | import android.view.Menu; 9 | import android.view.MenuItem; 10 | import android.widget.ProgressBar; 11 | 12 | import com.github.weiss.example.BaseActivity; 13 | import com.github.weiss.example.R; 14 | import com.github.weiss.example.util.JsHandler; 15 | import com.github.weiss.example.util.SnackbarUtil; 16 | import com.github.weiss.example.view.webview.CommonWebChromeClient; 17 | import com.github.weiss.example.view.webview.CommonWebView; 18 | import com.github.weiss.example.view.webview.CommonWebViewClient; 19 | import com.minamo.utils.ClipboardUtils; 20 | 21 | import butterknife.BindView; 22 | 23 | /** 24 | * gank.IO详情web页面 25 | */ 26 | public class WebActivity extends BaseActivity { 27 | 28 | @BindView(R.id.progress_bar) 29 | ProgressBar mBar; 30 | 31 | @BindView(R.id.web_view) 32 | CommonWebView mCommonWebView; 33 | 34 | @BindView(R.id.toolbar) 35 | Toolbar mToolbar; 36 | 37 | private static final String KEY_URL = "key_url"; 38 | 39 | private static final String KEY_TITLE = "key_title"; 40 | 41 | private String url, title; 42 | 43 | 44 | @Override 45 | public int getLayoutId() { 46 | return R.layout.activity_web; 47 | } 48 | 49 | 50 | @Override 51 | public void initView() { 52 | 53 | Intent intent = getIntent(); 54 | if (intent != null) 55 | parseIntent(intent); 56 | 57 | initWebSetting(); 58 | initToolBar(); 59 | // hideProgress(); 60 | mCommonWebView.setWebChromeClient(new CommonWebChromeClient(mBar, mBar)); 61 | mCommonWebView.setWebViewClient(new CommonWebViewClient(WebActivity.this)); 62 | mCommonWebView.loadUrl(url); 63 | } 64 | 65 | 66 | public void initToolBar() { 67 | 68 | setSupportActionBar(mToolbar); 69 | ActionBar actionBar = getSupportActionBar(); 70 | if (actionBar != null) { 71 | actionBar.setDisplayHomeAsUpEnabled(true); 72 | actionBar.setTitle(title); 73 | } 74 | } 75 | 76 | @Override 77 | public boolean onCreateOptionsMenu(Menu menu) { 78 | 79 | getMenuInflater().inflate(R.menu.menu_web, menu); 80 | return true; 81 | } 82 | 83 | @Override 84 | public boolean onOptionsItemSelected(MenuItem item) { 85 | 86 | int itemId = item.getItemId(); 87 | switch (itemId) { 88 | case android.R.id.home: 89 | onBackPressed(); 90 | return true; 91 | case R.id.action_share: 92 | share(); 93 | return true; 94 | 95 | case R.id.action_copy: 96 | ClipboardUtils.copyText(url); 97 | SnackbarUtil.showMessage(mCommonWebView, "已复制到剪贴板"); 98 | return true; 99 | } 100 | 101 | 102 | return super.onOptionsItemSelected(item); 103 | } 104 | 105 | @Override 106 | protected void onNewIntent(Intent intent) { 107 | 108 | super.onNewIntent(intent); 109 | parseIntent(intent); 110 | } 111 | 112 | private void parseIntent(Intent intent) { 113 | 114 | url = intent.getStringExtra(KEY_URL); 115 | title = intent.getStringExtra(KEY_TITLE); 116 | 117 | if (TextUtils.isEmpty(url)) { 118 | finish(); 119 | } 120 | } 121 | 122 | private void initWebSetting() { 123 | 124 | JsHandler jsHandler = new JsHandler(this, mCommonWebView); 125 | mCommonWebView.addJavascriptInterface(jsHandler, "JsHandler"); 126 | } 127 | 128 | public static boolean start(Activity activity, String url, String title) { 129 | 130 | Intent intent = new Intent(); 131 | intent.setClass(activity, WebActivity.class); 132 | intent.putExtra(KEY_URL, url); 133 | intent.putExtra(KEY_TITLE, title); 134 | activity.startActivity(intent); 135 | 136 | return true; 137 | } 138 | 139 | public static boolean start(Activity activity, String url) { 140 | 141 | return start(activity, url, null); 142 | } 143 | 144 | 145 | /* public void hideProgress() 146 | { 147 | 148 | mCircleProgressView.setVisibility(View.GONE); 149 | mCircleProgressView.stopSpinning(); 150 | }*/ 151 | 152 | 153 | private void share() { 154 | 155 | Intent intent = new Intent(Intent.ACTION_SEND); 156 | intent.setType("text/plain"); 157 | intent.putExtra(Intent.EXTRA_SUBJECT, "分享"); 158 | intent.putExtra(Intent.EXTRA_TEXT, "来自「Gank.IO」的分享:" + url); 159 | startActivity(Intent.createChooser(intent, title)); 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/util/JsHandler.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.util; 2 | 3 | import android.app.Activity; 4 | import android.app.AlertDialog; 5 | import android.content.DialogInterface; 6 | import android.webkit.JavascriptInterface; 7 | import android.webkit.WebView; 8 | 9 | import com.github.weiss.example.R; 10 | 11 | 12 | public class JsHandler { 13 | 14 | Activity activity; 15 | 16 | String TAG = "JsHandler"; 17 | 18 | WebView webView; 19 | 20 | 21 | public JsHandler(Activity activity, WebView webView) { 22 | 23 | this.activity = activity; 24 | this.webView = webView; 25 | } 26 | 27 | /** 28 | * This function handles call from JS 29 | */ 30 | public void jsFnCall(String jsString) { 31 | 32 | showDialog(jsString); 33 | } 34 | 35 | /** 36 | * This function handles call from Android-Java 37 | */ 38 | @JavascriptInterface 39 | public void javaFnCall(String jsString) { 40 | 41 | final String webUrl = "javascript:diplayJavaMsg('" + jsString + "')"; 42 | // Add this to avoid android.view.windowmanager$badtokenexception unable to add window 43 | if (!activity.isFinishing()) 44 | // load url on UI main thread 45 | activity.runOnUiThread(new Runnable() { 46 | 47 | @Override 48 | public void run() { 49 | 50 | webView.loadUrl(webUrl); 51 | } 52 | }); 53 | } 54 | 55 | /** 56 | * function shows Android-Native Alert Dialog 57 | */ 58 | public void showDialog(String msg) { 59 | 60 | AlertDialog dialog = new AlertDialog.Builder(activity).create(); 61 | dialog.setTitle(activity.getString(R.string.app_name)); 62 | dialog.setMessage(msg); 63 | dialog.setButton(DialogInterface.BUTTON_POSITIVE, activity.getString(android.R.string.ok), 64 | new DialogInterface.OnClickListener() { 65 | 66 | public void onClick(DialogInterface dialog, int which) { 67 | 68 | dialog.dismiss(); 69 | } 70 | }); 71 | dialog.setButton(DialogInterface.BUTTON_NEGATIVE, activity.getString(android.R.string.cancel), 72 | new DialogInterface.OnClickListener() { 73 | 74 | @Override 75 | public void onClick(DialogInterface dialog, int which) { 76 | 77 | dialog.dismiss(); 78 | } 79 | }); 80 | dialog.show(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/util/SnackbarUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.util; 2 | 3 | import com.google.android.material.snackbar.Snackbar; 4 | import android.view.View; 5 | 6 | public class SnackbarUtil { 7 | 8 | public static void showMessage(View view, String text) { 9 | 10 | Snackbar.make(view, text, Snackbar.LENGTH_SHORT).show(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/view/RatioImageView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Drakeet 3 | * 4 | * This file is part of Meizhi 5 | * 6 | * Meizhi is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Meizhi is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Meizhi. If not, see . 18 | */ 19 | 20 | package com.github.weiss.example.view; 21 | 22 | import android.content.Context; 23 | import android.util.AttributeSet; 24 | import android.widget.ImageView; 25 | 26 | public class RatioImageView extends ImageView { 27 | 28 | private int originalWidth; 29 | private int originalHeight; 30 | 31 | 32 | public RatioImageView(Context context) { 33 | super(context); 34 | } 35 | 36 | 37 | public RatioImageView(Context context, AttributeSet attrs) { 38 | super(context, attrs); 39 | } 40 | 41 | 42 | public RatioImageView(Context context, AttributeSet attrs, int defStyleAttr) { 43 | super(context, attrs, defStyleAttr); 44 | } 45 | 46 | 47 | public void setOriginalSize(int originalWidth, int originalHeight) { 48 | this.originalWidth = originalWidth; 49 | this.originalHeight = originalHeight; 50 | } 51 | 52 | 53 | @Override 54 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 55 | if (originalWidth > 0 && originalHeight > 0) { 56 | float ratio = (float) originalWidth / (float) originalHeight; 57 | 58 | int width = MeasureSpec.getSize(widthMeasureSpec); 59 | int height = MeasureSpec.getSize(heightMeasureSpec); 60 | // TODO: 现在只支持固定宽度 61 | if (width > 0) { 62 | height = (int) ((float) width / ratio); 63 | } 64 | 65 | setMeasuredDimension(width, height); 66 | } else { 67 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/view/behavior/helper/MathUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.view.behavior.helper; 2 | 3 | /** 4 | * Copy from Android design library 5 | * Created by chensuilun on 16/7/24. 6 | */ 7 | class MathUtils { 8 | 9 | static int constrain(int amount, int low, int high) { 10 | return amount < low ? low : (amount > high ? high : amount); 11 | } 12 | 13 | static float constrain(float amount, float low, float high) { 14 | return amount < low ? low : (amount > high ? high : amount); 15 | } 16 | 17 | } 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/view/behavior/helper/ViewOffsetBehavior.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.view.behavior.helper; 2 | 3 | import android.content.Context; 4 | import androidx.coordinatorlayout.widget.CoordinatorLayout; 5 | import android.util.AttributeSet; 6 | import android.view.View; 7 | 8 | /** 9 | * Copy from Android design library 10 | *

11 | * Behavior will automatically sets up a {@link com.google.android.material.appbar.ViewOffsetHelper} on a {@link View}. 12 | */ 13 | public class ViewOffsetBehavior extends CoordinatorLayout.Behavior { 14 | 15 | private ViewOffsetHelper mViewOffsetHelper; 16 | 17 | private int mTempTopBottomOffset = 0; 18 | private int mTempLeftRightOffset = 0; 19 | 20 | public ViewOffsetBehavior() { 21 | } 22 | 23 | public ViewOffsetBehavior(Context context, AttributeSet attrs) { 24 | super(context, attrs); 25 | } 26 | 27 | @Override 28 | public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection) { 29 | // First let lay the child out 30 | layoutChild(parent, child, layoutDirection); 31 | 32 | if (mViewOffsetHelper == null) { 33 | mViewOffsetHelper = new ViewOffsetHelper(child); 34 | } 35 | mViewOffsetHelper.onViewLayout(); 36 | 37 | if (mTempTopBottomOffset != 0) { 38 | mViewOffsetHelper.setTopAndBottomOffset(mTempTopBottomOffset); 39 | mTempTopBottomOffset = 0; 40 | } 41 | if (mTempLeftRightOffset != 0) { 42 | mViewOffsetHelper.setLeftAndRightOffset(mTempLeftRightOffset); 43 | mTempLeftRightOffset = 0; 44 | } 45 | 46 | return true; 47 | } 48 | 49 | protected void layoutChild(CoordinatorLayout parent, V child, int layoutDirection) { 50 | // Let the parent lay it out by default 51 | parent.onLayoutChild(child, layoutDirection); 52 | } 53 | 54 | public boolean setTopAndBottomOffset(int offset) { 55 | if (mViewOffsetHelper != null) { 56 | return mViewOffsetHelper.setTopAndBottomOffset(offset); 57 | } else { 58 | mTempTopBottomOffset = offset; 59 | } 60 | return false; 61 | } 62 | 63 | public boolean setLeftAndRightOffset(int offset) { 64 | if (mViewOffsetHelper != null) { 65 | return mViewOffsetHelper.setLeftAndRightOffset(offset); 66 | } else { 67 | mTempLeftRightOffset = offset; 68 | } 69 | return false; 70 | } 71 | 72 | public int getTopAndBottomOffset() { 73 | return mViewOffsetHelper != null ? mViewOffsetHelper.getTopAndBottomOffset() : 0; 74 | } 75 | 76 | public int getLeftAndRightOffset() { 77 | return mViewOffsetHelper != null ? mViewOffsetHelper.getLeftAndRightOffset() : 0; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/view/behavior/helper/ViewOffsetHelper.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.view.behavior.helper; 2 | 3 | 4 | import android.os.Build; 5 | import androidx.core.view.ViewCompat; 6 | import android.view.View; 7 | import android.view.ViewParent; 8 | 9 | /** 10 | * Copy from Android design library 11 | *

12 | * Utility helper for moving a {@link View} around using 13 | * {@link View#offsetLeftAndRight(int)} and 14 | * {@link View#offsetTopAndBottom(int)}. 15 | *

16 | * Also the setting of absolute offsets (similar to translationX/Y), rather than additive 17 | * offsets. 18 | */ 19 | public class ViewOffsetHelper { 20 | 21 | private final View mView; 22 | 23 | private int mLayoutTop; 24 | private int mLayoutLeft; 25 | private int mOffsetTop; 26 | private int mOffsetLeft; 27 | 28 | public ViewOffsetHelper(View view) { 29 | mView = view; 30 | } 31 | 32 | public void onViewLayout() { 33 | // Now grab the intended top 34 | mLayoutTop = mView.getTop(); 35 | mLayoutLeft = mView.getLeft(); 36 | 37 | // And offset it as needed 38 | updateOffsets(); 39 | } 40 | 41 | private void updateOffsets() { 42 | ViewCompat.offsetTopAndBottom(mView, mOffsetTop - (mView.getTop() - mLayoutTop)); 43 | ViewCompat.offsetLeftAndRight(mView, mOffsetLeft - (mView.getLeft() - mLayoutLeft)); 44 | 45 | // Manually invalidate the view and parent to make sure we get drawn pre-M 46 | if (Build.VERSION.SDK_INT < 23) { 47 | tickleInvalidationFlag(mView); 48 | final ViewParent vp = mView.getParent(); 49 | if (vp instanceof View) { 50 | tickleInvalidationFlag((View) vp); 51 | } 52 | } 53 | } 54 | 55 | private static void tickleInvalidationFlag(View view) { 56 | final float y = ViewCompat.getTranslationY(view); 57 | ViewCompat.setTranslationY(view, y + 1); 58 | ViewCompat.setTranslationY(view, y); 59 | } 60 | 61 | /** 62 | * Set the top and bottom offset for this {@link ViewOffsetHelper}'s view. 63 | * 64 | * @param offset the offset in px. 65 | * @return true if the offset has changed 66 | */ 67 | public boolean setTopAndBottomOffset(int offset) { 68 | if (mOffsetTop != offset) { 69 | mOffsetTop = offset; 70 | updateOffsets(); 71 | return true; 72 | } 73 | return false; 74 | } 75 | 76 | /** 77 | * Set the left and right offset for this {@link ViewOffsetHelper}'s view. 78 | * 79 | * @param offset the offset in px. 80 | * @return true if the offset has changed 81 | */ 82 | public boolean setLeftAndRightOffset(int offset) { 83 | if (mOffsetLeft != offset) { 84 | mOffsetLeft = offset; 85 | updateOffsets(); 86 | return true; 87 | } 88 | return false; 89 | } 90 | 91 | public int getTopAndBottomOffset() { 92 | return mOffsetTop; 93 | } 94 | 95 | public int getLeftAndRightOffset() { 96 | return mOffsetLeft; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/view/behavior/uc/UcNewsContentBehavior.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.view.behavior.uc; 2 | 3 | import android.content.Context; 4 | import androidx.coordinatorlayout.widget.CoordinatorLayout; 5 | import android.util.AttributeSet; 6 | import android.util.Log; 7 | import android.view.View; 8 | 9 | import com.github.weiss.example.App; 10 | import com.github.weiss.example.BuildConfig; 11 | import com.github.weiss.example.R; 12 | import com.github.weiss.example.view.behavior.helper.HeaderScrollingViewBehavior; 13 | 14 | import java.util.List; 15 | 16 | 17 | /** 18 | * 可滚动的新闻列表Behavior 19 | *

20 | * Created by chensuilun on 16/7/24. 21 | */ 22 | public class UcNewsContentBehavior extends HeaderScrollingViewBehavior { 23 | private static final String TAG = "UcNewsContentBehavior"; 24 | 25 | public UcNewsContentBehavior() { 26 | } 27 | 28 | public UcNewsContentBehavior(Context context, AttributeSet attrs) { 29 | super(context, attrs); 30 | } 31 | 32 | @Override 33 | public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) { 34 | return isDependOn(dependency); 35 | } 36 | 37 | @Override 38 | public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { 39 | if (BuildConfig.DEBUG) { 40 | Log.d(TAG, "onDependentViewChanged"); 41 | } 42 | offsetChildAsNeeded(parent, child, dependency); 43 | return false; 44 | } 45 | 46 | private void offsetChildAsNeeded(CoordinatorLayout parent, View child, View dependency) { 47 | child.setTranslationY((int) (-dependency.getTranslationY() / (getHeaderOffsetRange() * 1.0f) * getScrollRange(dependency))); 48 | 49 | } 50 | 51 | 52 | @Override 53 | protected View findFirstDependency(List views) { 54 | for (int i = 0, z = views.size(); i < z; i++) { 55 | View view = views.get(i); 56 | if (isDependOn(view)) 57 | return view; 58 | } 59 | return null; 60 | } 61 | 62 | @Override 63 | protected int getScrollRange(View v) { 64 | if (isDependOn(v)) { 65 | return Math.max(0, v.getMeasuredHeight() - getFinalHeight()); 66 | } else { 67 | return super.getScrollRange(v); 68 | } 69 | } 70 | 71 | private int getHeaderOffsetRange() { 72 | return App.getAppContext().getResources().getDimensionPixelOffset(R.dimen.uc_news_header_pager_offset); 73 | } 74 | 75 | private int getFinalHeight() { 76 | return App.getAppContext().getResources().getDimensionPixelOffset(R.dimen.uc_news_tabs_height) + App.getAppContext().getResources().getDimensionPixelOffset(R.dimen.uc_news_header_title_height); 77 | } 78 | 79 | 80 | private boolean isDependOn(View dependency) { 81 | return dependency != null && dependency.getId() == R.id.id_uc_news_header_pager; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/view/behavior/uc/UcNewsTabBehavior.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.view.behavior.uc; 2 | 3 | import android.content.Context; 4 | import androidx.coordinatorlayout.widget.CoordinatorLayout; 5 | import android.util.AttributeSet; 6 | import android.util.Log; 7 | import android.view.View; 8 | 9 | import com.github.weiss.example.App; 10 | import com.github.weiss.example.BuildConfig; 11 | import com.github.weiss.example.R; 12 | import com.github.weiss.example.view.behavior.helper.HeaderScrollingViewBehavior; 13 | 14 | import java.util.List; 15 | 16 | 17 | /** 18 | * 新闻tab behavior 19 | *

20 | * Created by chensuilun on 16/7/25. 21 | */ 22 | public class UcNewsTabBehavior extends HeaderScrollingViewBehavior { 23 | private static final String TAG = "UcNewsTabBehavior"; 24 | 25 | public UcNewsTabBehavior() { 26 | } 27 | 28 | public UcNewsTabBehavior(Context context, AttributeSet attrs) { 29 | super(context, attrs); 30 | } 31 | 32 | 33 | @Override 34 | protected void layoutChild(CoordinatorLayout parent, View child, int layoutDirection) { 35 | super.layoutChild(parent, child, layoutDirection); 36 | if (BuildConfig.DEBUG) { 37 | Log.d(TAG, "layoutChild:top" + child.getTop() + ",height" + child.getHeight()); 38 | } 39 | } 40 | 41 | @Override 42 | public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) { 43 | return isDependOn(dependency); 44 | } 45 | 46 | 47 | @Override 48 | public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { 49 | if (BuildConfig.DEBUG) { 50 | Log.d(TAG, "onDependentViewChanged: dependency.getTranslationY():" + dependency.getTranslationY()); 51 | } 52 | offsetChildAsNeeded(parent, child, dependency); 53 | return false; 54 | } 55 | 56 | private void offsetChildAsNeeded(CoordinatorLayout parent, View child, View dependency) { 57 | float offsetRange = dependency.getTop() + getFinalHeight() - child.getTop(); 58 | int headerOffsetRange = getHeaderOffsetRange(); 59 | if (dependency.getTranslationY() == headerOffsetRange) { 60 | child.setTranslationY(offsetRange); 61 | } else if (dependency.getTranslationY() == 0) { 62 | child.setTranslationY(0); 63 | } else { 64 | child.setTranslationY((int) (dependency.getTranslationY() / (getHeaderOffsetRange() * 1.0f) * offsetRange)); 65 | } 66 | } 67 | 68 | 69 | @Override 70 | protected View findFirstDependency(List views) { 71 | for (int i = 0, z = views.size(); i < z; i++) { 72 | View view = views.get(i); 73 | if (isDependOn(view)) 74 | return view; 75 | } 76 | return null; 77 | } 78 | 79 | private int getHeaderOffsetRange() { 80 | return App.getAppContext().getResources().getDimensionPixelOffset(R.dimen.uc_news_header_pager_offset); 81 | } 82 | 83 | private int getFinalHeight() { 84 | return App.getAppContext().getResources().getDimensionPixelOffset(R.dimen.uc_news_header_title_height); 85 | } 86 | 87 | 88 | private boolean isDependOn(View dependency) { 89 | return dependency != null && dependency.getId() == R.id.id_uc_news_header_pager; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/view/behavior/uc/UcNewsTitleBehavior.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.view.behavior.uc; 2 | 3 | import android.content.Context; 4 | import androidx.coordinatorlayout.widget.CoordinatorLayout; 5 | import android.util.AttributeSet; 6 | import android.util.Log; 7 | import android.view.View; 8 | 9 | import com.github.weiss.example.App; 10 | import com.github.weiss.example.BuildConfig; 11 | import com.github.weiss.example.R; 12 | 13 | /** 14 | * 新闻标题 15 | *

16 | * Created by chensuilun on 16/7/25. 17 | */ 18 | public class UcNewsTitleBehavior extends CoordinatorLayout.Behavior { 19 | private static final String TAG = "UcNewsTitleBehavior"; 20 | 21 | public UcNewsTitleBehavior() { 22 | } 23 | 24 | public UcNewsTitleBehavior(Context context, AttributeSet attrs) { 25 | super(context, attrs); 26 | } 27 | 28 | 29 | @Override 30 | public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) { 31 | // FIXME: 16/7/27 不知道为啥在XML设置-45dip,解析出来的topMargin少了1个px,所以这里用代码设置一遍 32 | ((CoordinatorLayout.LayoutParams) child.getLayoutParams()).topMargin = -getTitleHeight(); 33 | parent.onLayoutChild(child, layoutDirection); 34 | if (BuildConfig.DEBUG) { 35 | Log.d(TAG, "layoutChild:top" + child.getTop() + ",height" + child.getHeight()); 36 | } 37 | return true; 38 | } 39 | 40 | @Override 41 | public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) { 42 | return isDependOn(dependency); 43 | } 44 | 45 | 46 | @Override 47 | public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { 48 | offsetChildAsNeeded(parent, child, dependency); 49 | return false; 50 | } 51 | 52 | private void offsetChildAsNeeded(CoordinatorLayout parent, View child, View dependency) { 53 | int headerOffsetRange = getHeaderOffsetRange(); 54 | int titleOffsetRange = getTitleHeight(); 55 | if (BuildConfig.DEBUG) { 56 | Log.d(TAG, "offsetChildAsNeeded:" + dependency.getTranslationY()); 57 | } 58 | if (dependency.getTranslationY() == headerOffsetRange) { 59 | child.setTranslationY(titleOffsetRange); 60 | } else if (dependency.getTranslationY() == 0) { 61 | child.setTranslationY(0); 62 | } else { 63 | child.setTranslationY((int) (dependency.getTranslationY() / (headerOffsetRange * 1.0f) * titleOffsetRange)); 64 | } 65 | 66 | } 67 | 68 | private int getHeaderOffsetRange() { 69 | return App.getAppContext().getResources().getDimensionPixelOffset(R.dimen.uc_news_header_pager_offset); 70 | } 71 | 72 | private int getTitleHeight() { 73 | return App.getAppContext().getResources().getDimensionPixelOffset(R.dimen.uc_news_header_title_height); 74 | } 75 | 76 | 77 | private boolean isDependOn(View dependency) { 78 | return dependency != null && dependency.getId() == R.id.id_uc_news_header_pager; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/view/webview/CommonWebChromeClient.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.view.webview; 2 | 3 | import androidx.appcompat.app.ActionBar; 4 | import android.view.View; 5 | import android.webkit.JsPromptResult; 6 | import android.webkit.JsResult; 7 | import android.webkit.WebChromeClient; 8 | import android.webkit.WebView; 9 | import android.widget.ProgressBar; 10 | 11 | public class CommonWebChromeClient extends WebChromeClient { 12 | 13 | private ProgressBar mBar; 14 | 15 | private View mLoadingView; 16 | 17 | private ActionBar mActionBar; 18 | 19 | public CommonWebChromeClient(ProgressBar bar, View loadingView) { 20 | 21 | this(bar, loadingView, null); 22 | } 23 | 24 | public CommonWebChromeClient(ProgressBar bar, View loadingView, ActionBar actionBar) { 25 | 26 | mBar = bar; 27 | mLoadingView = loadingView; 28 | mActionBar = actionBar; 29 | } 30 | 31 | @Override 32 | public void onReceivedTitle(WebView view, String title) { 33 | 34 | if (mActionBar != null) { 35 | mActionBar.setTitle(title); 36 | } 37 | } 38 | 39 | @Override 40 | public void onProgressChanged(WebView view, int newProgress) { 41 | 42 | if (mBar != null) { 43 | mBar.setProgress(newProgress); 44 | if (newProgress >= 100) { 45 | mBar.setVisibility(View.GONE); 46 | mLoadingView.setVisibility(View.GONE); 47 | } 48 | } 49 | } 50 | 51 | @Override 52 | public boolean onJsAlert(WebView view, String url, String message, JsResult result) { 53 | 54 | return super.onJsAlert(view, url, message, result); 55 | } 56 | 57 | @Override 58 | public boolean onJsConfirm(WebView view, String url, String message, JsResult result) { 59 | 60 | return super.onJsConfirm(view, url, message, result); 61 | } 62 | 63 | @Override 64 | public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) { 65 | 66 | return super.onJsPrompt(view, url, message, defaultValue, result); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/view/webview/CommonWebView.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.view.webview; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.Context; 5 | import android.os.Build; 6 | import android.util.AttributeSet; 7 | import android.webkit.WebSettings; 8 | import android.webkit.WebView; 9 | 10 | 11 | /** 12 | * 通用的WebView 13 | */ 14 | public class CommonWebView extends WebView { 15 | 16 | public static final String ENCODING_UTF_8 = "UTF-8"; 17 | 18 | public static final String MIME_TYPE = "text/html"; 19 | 20 | public CommonWebView(Context context) { 21 | 22 | super(context); 23 | init(); 24 | } 25 | 26 | public CommonWebView(Context context, AttributeSet attrs) { 27 | 28 | super(context, attrs); 29 | init(); 30 | } 31 | 32 | public CommonWebView(Context context, AttributeSet attrs, int defStyleAttr) { 33 | 34 | super(context, attrs, defStyleAttr); 35 | init(); 36 | } 37 | 38 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) 39 | public CommonWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 40 | 41 | super(context, attrs, defStyleAttr, defStyleRes); 42 | init(); 43 | } 44 | 45 | private void init() { 46 | 47 | if (isInEditMode()) { 48 | return; 49 | } 50 | WebSettings settings = getSettings(); 51 | settings.setJavaScriptEnabled(true); 52 | settings.setBuiltInZoomControls(false); 53 | //设置缓存模式 54 | settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); 55 | //开启DOM storage API功能 56 | settings.setDomStorageEnabled(true); 57 | //开启database storage 功能 58 | settings.setDatabaseEnabled(true); 59 | 60 | String cacheDir = getContext().getFilesDir().getAbsolutePath() + "web_cache"; 61 | settings.setAppCachePath(cacheDir); 62 | settings.setAppCacheEnabled(true); 63 | 64 | settings.setLoadsImagesAutomatically(true); 65 | settings.setDefaultTextEncodingName(ENCODING_UTF_8); 66 | settings.setBlockNetworkImage(false); 67 | settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); 68 | settings.setUseWideViewPort(true); 69 | settings.setLoadWithOverviewMode(true); 70 | setHorizontalScrollBarEnabled(false); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/view/webview/CommonWebViewClient.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.view.webview; 2 | 3 | import android.graphics.Bitmap; 4 | import androidx.appcompat.app.ActionBar; 5 | import androidx.appcompat.app.AppCompatActivity; 6 | import android.webkit.WebResourceRequest; 7 | import android.webkit.WebResourceResponse; 8 | import android.webkit.WebView; 9 | 10 | import com.github.weiss.example.ui.WebActivity; 11 | 12 | 13 | public class CommonWebViewClient extends android.webkit.WebViewClient { 14 | 15 | 16 | private AppCompatActivity mActivity; 17 | 18 | public CommonWebViewClient(AppCompatActivity activity) { 19 | 20 | mActivity = activity; 21 | } 22 | 23 | @Override 24 | public void onLoadResource(WebView view, String url) { 25 | 26 | super.onLoadResource(view, url); 27 | } 28 | 29 | @Override 30 | public boolean shouldOverrideUrlLoading(WebView view, String url) { 31 | 32 | 33 | if (url != null && url.startsWith("orpheus")) { 34 | return true; 35 | } 36 | if (url != null && url.startsWith("http")) { 37 | WebActivity.start(mActivity, url); 38 | return true; 39 | } 40 | return true; 41 | } 42 | 43 | @Override 44 | public WebResourceResponse shouldInterceptRequest(WebView view, String url) { 45 | 46 | return super.shouldInterceptRequest(view, url); 47 | } 48 | 49 | @Override 50 | public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { 51 | 52 | return super.shouldInterceptRequest(view, request); 53 | } 54 | 55 | @Override 56 | public void onPageStarted(WebView view, String url, Bitmap favicon) { 57 | 58 | super.onPageStarted(view, url, favicon); 59 | } 60 | 61 | @Override 62 | public void onPageFinished(WebView view, String url) { 63 | 64 | super.onPageFinished(view, url); 65 | ActionBar actionBar = mActivity.getSupportActionBar(); 66 | if (actionBar != null) { 67 | actionBar.setTitle(view.getTitle()); 68 | } 69 | } 70 | 71 | @Override 72 | public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 73 | 74 | super.onReceivedError(view, errorCode, description, failingUrl); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/weiss/example/view/webview/LoveVideoView.java: -------------------------------------------------------------------------------- 1 | package com.github.weiss.example.view.webview; 2 | 3 | import android.content.Context; 4 | import android.media.MediaPlayer; 5 | import android.util.AttributeSet; 6 | import android.util.Base64; 7 | import android.webkit.WebChromeClient; 8 | import android.webkit.WebSettings; 9 | import android.webkit.WebView; 10 | import android.webkit.WebViewClient; 11 | 12 | import java.io.InputStream; 13 | 14 | 15 | public class LoveVideoView extends WebView { 16 | 17 | private final Context mContext; 18 | 19 | 20 | public LoveVideoView(Context context) { 21 | 22 | this(context, null); 23 | } 24 | 25 | 26 | public LoveVideoView(Context context, AttributeSet attrs) { 27 | 28 | this(context, attrs, 0); 29 | } 30 | 31 | 32 | public LoveVideoView(Context context, AttributeSet attrs, int defStyleAttr) { 33 | 34 | super(context, attrs, defStyleAttr); 35 | mContext = context; 36 | init(); 37 | } 38 | 39 | 40 | void init() { 41 | 42 | setWebViewClient(new LoveClient()); 43 | setWebChromeClient(new Chrome()); 44 | WebSettings webSettings = getSettings(); 45 | webSettings.setJavaScriptEnabled(true); 46 | webSettings.setAllowFileAccess(true); 47 | webSettings.setDatabaseEnabled(true); 48 | webSettings.setDomStorageEnabled(true); 49 | webSettings.setSaveFormData(false); 50 | webSettings.setAppCacheEnabled(true); 51 | webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); 52 | webSettings.setLoadWithOverviewMode(false); 53 | webSettings.setUseWideViewPort(true); 54 | } 55 | 56 | 57 | private class LoveClient extends WebViewClient { 58 | 59 | @Override 60 | public boolean shouldOverrideUrlLoading(WebView view, String url) { 61 | 62 | view.loadUrl(url); 63 | return true; 64 | } 65 | 66 | 67 | @Override 68 | public void onPageFinished(WebView view, String url) { 69 | 70 | super.onPageFinished(view, url); 71 | // 这些视频需要hack CSS才能达到全屏播放的效果 72 | if (url.contains("www.vmovier.com")) { 73 | injectCSS("vmovier.css"); 74 | } else if (url.contains("video.weibo.com")) { 75 | injectCSS("weibo.css"); 76 | } else if (url.contains("m.miaopai.com")) { 77 | injectCSS("miaopai.css"); 78 | } 79 | } 80 | } 81 | 82 | 83 | private void injectCSS(String filename) { 84 | 85 | try { 86 | InputStream inputStream = mContext.getAssets().open(filename); 87 | byte[] buffer = new byte[inputStream.available()]; 88 | inputStream.read(buffer); 89 | inputStream.close(); 90 | String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP); 91 | loadUrl("javascript:(function() {" + 92 | "var parent = document.getElementsByTagName('head').item(0);" + 93 | "var style = document.createElement('style');" + 94 | "style.type = 'text/css';" + 95 | // Tell the browser to BASE64-decode the string into your script !!! 96 | "style.innerHTML = window.atob('" + encoded + "');" + 97 | "parent.appendChild(style)" + 98 | "})()"); 99 | } catch (Exception e) { 100 | e.printStackTrace(); 101 | } 102 | } 103 | 104 | 105 | private class Chrome extends WebChromeClient 106 | implements MediaPlayer.OnCompletionListener { 107 | 108 | @Override 109 | public void onCompletion(MediaPlayer player) { 110 | 111 | if (player != null) { 112 | if (player.isPlaying()) player.stop(); 113 | player.reset(); 114 | player.release(); 115 | player = null; 116 | } 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/black_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_copy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xm1nam0/RxCore/595f08ee480e17d7b4ec93bed3e5e8929434c8e2/app/src/main/res/drawable/ic_copy.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_share.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xm1nam0/RxCore/595f08ee480e17d7b4ec93bed3e5e8929434c8e2/app/src/main/res/drawable/ic_share.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 21 | 22 | 23 | 24 | 29 | 30 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_picture.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 15 | 16 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_splash.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 17 | 18 | 19 | 22 | 23 | 24 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/src/main/res/layout/content_main.xml: -------------------------------------------------------------------------------- 1 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_gank.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 19 | 20 | 21 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /app/src/main/res/layout/navigation_content.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 17 | 18 | 46 | 47 | 48 | 55 | 56 | 61 | 62 | 69 | 70 | 71 | 72 | 73 | 83 | 84 | 90 | 98 | 99 | -------------------------------------------------------------------------------- /app/src/main/res/layout/recyclerview_base.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 18 | 19 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/view_load_more.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 11 | 12 | 18 | 19 | 27 | 28 | 29 |