diff --git a/.idea/encodings.xml b/.idea/encodings.xml index 2ecf143..ada076f 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -1,6 +1,8 @@ + + diff --git a/app/build.gradle b/app/build.gradle index c4411b9..9725a14 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ android { } repositories { - maven { url "https://jitpack.matto" } + maven { url "https://jitpack.io" } } dependencies { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f6e8ef3..e97d472 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -24,6 +24,7 @@ + diff --git a/app/src/main/java/com/matto/App.java b/app/src/main/java/com/matto/App.java index 063e667..7061fe3 100644 --- a/app/src/main/java/com/matto/App.java +++ b/app/src/main/java/com/matto/App.java @@ -1,6 +1,6 @@ package com.matto; -import com.common.EasyApplication; +import com.common.BasicApplication; import com.common.model.control.LogicProxy; import com.matto.model.LoginLogic; import com.matto.model.MainLogic; @@ -9,11 +9,12 @@ * author miekoz on 2016/3/17. * email meikoz@126.com */ -public class App extends EasyApplication { +public class App extends BasicApplication { @Override public void onCreate() { super.onCreate(); + LogicProxy.getInstance().init( LoginLogic.class, MainLogic.class ); diff --git a/app/src/main/java/com/matto/model/config/ApiConstant.java b/app/src/main/java/com/matto/model/config/ApiConstant.java new file mode 100644 index 0000000..1cadc12 --- /dev/null +++ b/app/src/main/java/com/matto/model/config/ApiConstant.java @@ -0,0 +1,10 @@ +package com.matto.model.config; + +/** + * author meikoz on 2016/4/19. + * email meikoz@126.com + */ +public interface ApiConstant { + + String BASE_URL = "http://gank.avosapps.com/api/"; +} diff --git a/app/src/main/java/com/matto/model/http/ServiceFactory.java b/app/src/main/java/com/matto/model/http/ServiceFactory.java index 5c66c32..56dec21 100644 --- a/app/src/main/java/com/matto/model/http/ServiceFactory.java +++ b/app/src/main/java/com/matto/model/http/ServiceFactory.java @@ -1,6 +1,7 @@ package com.matto.model.http; import com.common.model.http.HttpClient; +import com.matto.model.config.ApiConstant; import com.matto.pojo.Gank; import retrofit2.Call; @@ -16,13 +17,9 @@ public class ServiceFactory { public static MainService getMainIns(){ if (mService == null){ - mService = HttpClient.getIns().createService(MainService.class); + mService = HttpClient.getIns(ApiConstant.BASE_URL).createService(MainService.class); } return mService; } - public Call getBenefitsGoods(int size, int page){ - return mService.getMainAndroid(size,page); - } - } diff --git a/app/src/main/java/com/matto/ui/activity/AndroidSwipActivity.java b/app/src/main/java/com/matto/ui/activity/AndroidSwipActivity.java new file mode 100644 index 0000000..eb931d6 --- /dev/null +++ b/app/src/main/java/com/matto/ui/activity/AndroidSwipActivity.java @@ -0,0 +1,21 @@ +package com.matto.ui.activity; + +import android.os.Bundle; + +import com.common.view.base.SwipeBackActivity; +import com.common.view.base.SwipeBackLayout; +import com.matto.R; + +/** + * author meikoz on 2016/4/19. + * email meikoz@126.com + */ +public class AndroidSwipActivity extends SwipeBackActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_common); + setDragEdge(SwipeBackLayout.DragEdge.LEFT); + } +} diff --git a/app/src/main/java/com/matto/ui/activity/LoginActivity.java b/app/src/main/java/com/matto/ui/activity/LoginActivity.java index 908f5bb..52f688e 100644 --- a/app/src/main/java/com/matto/ui/activity/LoginActivity.java +++ b/app/src/main/java/com/matto/ui/activity/LoginActivity.java @@ -6,6 +6,7 @@ import com.common.model.basic.ToastTip; import com.common.view.base.BaseActivity; +import com.matto.ui.widget.TitleBar; import com.matto.R; import com.common.model.control.LogicProxy; import com.matto.model.LoginLogic; @@ -31,6 +32,9 @@ public static void start(Activity activity) { @Bind(R.id.edit_passwrod) EditText mEditPasswrod; + @Bind(R.id.title_bar) + TitleBar titlebar; + LoginLogic mLoginLogic; @Override @@ -40,18 +44,19 @@ protected int getLayoutResource() { @Override protected void onInitView() { + titlebar.setTitle("登录页面"); mLoginLogic = LogicProxy.getInstance().getBindViewProxy(LoginLogic.class, this); } @OnClick(R.id.btn_login) void login() { mLoginLogic.login("zhangsan", "123"); + startActivity(new Intent(LoginActivity.this, AndroidSwipActivity.class)); } @Override public void onLoginSuccess() { ToastTip.show("登录成功"); - MainActivity.start(LoginActivity.this); } @Override diff --git a/app/src/main/java/com/matto/ui/activity/MainActivity.java b/app/src/main/java/com/matto/ui/activity/MainActivity.java index 98b2367..bffdfcd 100644 --- a/app/src/main/java/com/matto/ui/activity/MainActivity.java +++ b/app/src/main/java/com/matto/ui/activity/MainActivity.java @@ -11,6 +11,7 @@ import com.matto.R; import com.matto.model.MainLogic; import com.matto.ui.fragment.DiscoveryFragment; +import com.matto.ui.fragment.HomeFragment; import com.matto.ui.fragment.ShowMeFragment; import com.matto.ui.view.MainView; @@ -42,7 +43,7 @@ protected void onInitView() { @Override public void switchHome() { - startFragment(new DiscoveryFragment()); + startFragment(new HomeFragment()); } @Override diff --git a/app/src/main/java/com/matto/ui/fragment/HomeFragment.java b/app/src/main/java/com/matto/ui/fragment/HomeFragment.java index 0e42d86..5241be4 100644 --- a/app/src/main/java/com/matto/ui/fragment/HomeFragment.java +++ b/app/src/main/java/com/matto/ui/fragment/HomeFragment.java @@ -1,13 +1,26 @@ package com.matto.ui.fragment; +import android.content.Intent; +import android.view.View; +import android.widget.ImageView; + import com.common.view.base.BaseFragment; +import com.common.view.widget.OnClickEvent; import com.matto.R; +import com.matto.ui.activity.AndroidSwipActivity; +import com.matto.ui.activity.LoginActivity; + +import butterknife.Bind; /** * author meikoz on 2016/4/19. * email meikoz@126.com */ public class HomeFragment extends BaseFragment { + + @Bind(R.id.splash_view) + ImageView splashView; + @Override protected int getLayoutResource() { return R.layout.activity_spalsh; @@ -15,6 +28,11 @@ protected int getLayoutResource() { @Override protected void onInitData() { - + splashView.setOnClickListener(new OnClickEvent() { + @Override + public void singleClick(View v) { + startActivity(new Intent(getActivity(), AndroidSwipActivity.class)); + } + }); } } diff --git a/app/src/main/java/com/matto/ui/fragment/ShowMeFragment.java b/app/src/main/java/com/matto/ui/fragment/ShowMeFragment.java index 882dab9..e52cbbd 100644 --- a/app/src/main/java/com/matto/ui/fragment/ShowMeFragment.java +++ b/app/src/main/java/com/matto/ui/fragment/ShowMeFragment.java @@ -10,7 +10,7 @@ public class ShowMeFragment extends BaseFragment { @Override protected int getLayoutResource() { - return R.layout.activity_hosting; + return R.layout.activity_listview; } @Override diff --git a/basic/src/main/java/com/common/view/base/EasyToolBar.java b/app/src/main/java/com/matto/ui/widget/TitleBar.java similarity index 93% rename from basic/src/main/java/com/common/view/base/EasyToolBar.java rename to app/src/main/java/com/matto/ui/widget/TitleBar.java index e6c2a8d..5695770 100644 --- a/basic/src/main/java/com/common/view/base/EasyToolBar.java +++ b/app/src/main/java/com/matto/ui/widget/TitleBar.java @@ -1,4 +1,4 @@ -package com.common.view.base; +package com.matto.ui.widget; import android.app.Activity; import android.content.Context; @@ -11,14 +11,14 @@ import android.widget.RelativeLayout; import android.widget.TextView; -import com.common.R; import com.common.utils.DisplayUtil; +import com.matto.R; /** * author miekoz on 2016/3/15. * email meikoz@126.com */ -public class EasyToolBar extends RelativeLayout implements View.OnClickListener { +public class TitleBar extends RelativeLayout implements View.OnClickListener { LinearLayout mlvTitleRight; TextView mTvTitle; @@ -27,12 +27,12 @@ public class EasyToolBar extends RelativeLayout implements View.OnClickListener private OnClickTitleBarBackCallback mCallBack; private TextView mTvRightText; - public EasyToolBar(Context context) { + public TitleBar(Context context) { super(context); onInit(); } - public EasyToolBar(Context context, AttributeSet attrs) { + public TitleBar(Context context, AttributeSet attrs) { super(context, attrs); onInit(); TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.titlebarview); diff --git a/app/src/main/res/drawable-xxhdpi/app_logo.png b/app/src/main/res/drawable-xxhdpi/app_logo.png new file mode 100644 index 0000000..100112a Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/app_logo.png differ diff --git a/app/src/main/res/layout/activity_common.xml b/app/src/main/res/layout/activity_common.xml new file mode 100644 index 0000000..4d4d9db --- /dev/null +++ b/app/src/main/res/layout/activity_common.xml @@ -0,0 +1,15 @@ + + + + + + + + diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 682637c..2d30260 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -2,8 +2,11 @@ + + - diff --git a/basic/build.gradle b/basic/build.gradle index bdeaeed..3bfe319 100644 --- a/basic/build.gradle +++ b/basic/build.gradle @@ -1,7 +1,4 @@ apply plugin: 'com.android.library' -repositories { - maven { url "https://jitpack.matto" } -} android { compileSdkVersion 23 @@ -21,7 +18,9 @@ android { } } - +repositories { + maven { url "https://jitpack.io" } +} dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) @@ -34,6 +33,7 @@ dependencies { compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' compile 'com.squareup.okhttp3:logging-interceptor:3.1.2' + // Glide compile 'com.github.bumptech.glide:glide:3.7.0' compile files('libs/BaiduLBS_Android.jar') diff --git a/basic/src/main/java/com/common/BasicApplication.java b/basic/src/main/java/com/common/BasicApplication.java new file mode 100644 index 0000000..b15dc75 --- /dev/null +++ b/basic/src/main/java/com/common/BasicApplication.java @@ -0,0 +1,35 @@ +package com.common; + +import android.app.Application; +import android.content.Context; + +import com.common.model.basic.LocationTip; + +/** + * author miekoz on 2016/3/17. + * email meikoz@126.com + */ +public class BasicApplication extends Application { + + private static BasicApplication ourInstance = new BasicApplication(); + private static Context mContext; + + public static BasicApplication getInstance() { + return ourInstance; + } + + public static Context getContext() { + return mContext; + } + + @Override + public void onCreate() { + super.onCreate(); + ourInstance = this; + mContext = getApplicationContext(); + + new LocationTip(this).doStartLocation(); + } + + +} diff --git a/basic/src/main/java/com/common/EasyApplication.java b/basic/src/main/java/com/common/EasyApplication.java deleted file mode 100644 index b1dda9e..0000000 --- a/basic/src/main/java/com/common/EasyApplication.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.common; - -import android.app.Application; -import android.content.Context; - -import com.common.model.basic.LocationTip; -import com.common.view.Constant; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -/** - * author miekoz on 2016/3/17. - * email meikoz@126.com - */ -public class EasyApplication extends Application { - public Gson gson; - private static EasyApplication ourInstance = new EasyApplication(); - private static Context mContext; - - public static EasyApplication getInstance() { - return ourInstance; - } - - public static Context getContext() { - return mContext; - } - - @Override - public void onCreate() { - super.onCreate(); - ourInstance = this; - mContext = getApplicationContext(); - this.initGson(); - -// LogLevel logLevel; -// if (Config.DEBUG)logLevel =LogLevel.FULL; -// else logLevel=LogLevel.NONE; -// -// Logger.init().methodOffset(2).methodCount(2).logLevel(logLevel); - - new LocationTip(this).doStartLocation(); - } - - private void initGson() { - this.gson = new GsonBuilder() - .setDateFormat(Constant.GANK_DATA_FORMAT) - .create(); - } -} diff --git a/basic/src/main/java/com/common/model/basic/ToastTip.java b/basic/src/main/java/com/common/model/basic/ToastTip.java index 13f206e..dfaf997 100644 --- a/basic/src/main/java/com/common/model/basic/ToastTip.java +++ b/basic/src/main/java/com/common/model/basic/ToastTip.java @@ -2,7 +2,7 @@ import android.widget.Toast; -import com.common.EasyApplication; +import com.common.BasicApplication; /** * author meikoz on 2016/4/13. @@ -18,7 +18,7 @@ public static void show(CharSequence message) { if (message.length() > 10) { duration = Toast.LENGTH_LONG; } - mToast = Toast.makeText(EasyApplication.getInstance(), message, duration); + mToast = Toast.makeText(BasicApplication.getInstance(), message, duration); } else { mToast.setText(message); } diff --git a/basic/src/main/java/com/common/model/control/LogicProxy.java b/basic/src/main/java/com/common/model/control/LogicProxy.java index d73c07d..85e68c3 100644 --- a/basic/src/main/java/com/common/model/control/LogicProxy.java +++ b/basic/src/main/java/com/common/model/control/LogicProxy.java @@ -28,7 +28,6 @@ private LogicProxy() { //private Object m_proxy; public void init(Class... clss) { - Log.d("PROXY", "准备初始化Proxy"); List list = new LinkedList(); for (Class cls : clss) { if (cls.isAnnotationPresent(Implement.class)) { @@ -49,7 +48,6 @@ public void init(Class... clss) { } public T getProxy(Class cls) { - Log.d("PROXY", "获取Proxy"); //return (T) m_proxy; return (T) m_objects.get(cls); } diff --git a/basic/src/main/java/com/common/model/http/HttpCacheInterceptor.java b/basic/src/main/java/com/common/model/http/HttpCacheInterceptor.java index 3860707..9c772f6 100644 --- a/basic/src/main/java/com/common/model/http/HttpCacheInterceptor.java +++ b/basic/src/main/java/com/common/model/http/HttpCacheInterceptor.java @@ -1,11 +1,8 @@ package com.common.model.http; -import android.content.Context; -import android.net.ConnectivityManager; -import android.net.NetworkInfo; import android.util.Log; -import com.common.EasyApplication; +import com.common.BasicApplication; import com.common.utils.NetWorkUtil; import java.io.IOException; @@ -24,7 +21,7 @@ public class HttpCacheInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); - if (!NetWorkUtil.isNetConnected(EasyApplication.getContext())) { + if (!NetWorkUtil.isNetConnected(BasicApplication.getContext())) { request = request.newBuilder() .cacheControl(CacheControl.FORCE_CACHE) .build(); @@ -32,7 +29,7 @@ public Response intercept(Chain chain) throws IOException { } Response originalResponse = chain.proceed(request); - if (NetWorkUtil.isNetConnected(EasyApplication.getContext())) { + if (NetWorkUtil.isNetConnected(BasicApplication.getContext())) { //有网的时候读接口上的@Headers里的配置,你可以在这里进行统一的设置 String cacheControl = request.cacheControl().toString(); return originalResponse.newBuilder() diff --git a/basic/src/main/java/com/common/model/http/HttpClient.java b/basic/src/main/java/com/common/model/http/HttpClient.java index a4ebf13..50c0b82 100644 --- a/basic/src/main/java/com/common/model/http/HttpClient.java +++ b/basic/src/main/java/com/common/model/http/HttpClient.java @@ -1,7 +1,6 @@ package com.common.model.http; -import com.common.view.Constant; -import com.common.EasyApplication; +import com.common.BasicApplication; import java.io.File; import java.util.concurrent.TimeUnit; @@ -22,21 +21,21 @@ public class HttpClient { private static HttpClient mInstance; private Retrofit retrofit; - public static HttpClient getIns() { + public static HttpClient getIns(String base_url) { if (mInstance == null) { synchronized (HttpClient.class) { - if (mInstance == null) mInstance = new HttpClient(); + if (mInstance == null) mInstance = new HttpClient(base_url); } } return mInstance; } - public HttpClient() { + public HttpClient(String BASE_URL) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); - File cacheFile = new File(EasyApplication.getInstance().getCacheDir(), "android"); + File cacheFile = new File(BasicApplication.getInstance().getCacheDir(), "android"); Cache cache = new Cache(cacheFile, 1024 * 1024 * 100); //100Mb OkHttpClient okHttpClient = new OkHttpClient.Builder() @@ -49,9 +48,9 @@ public HttpClient() { retrofit = new Retrofit.Builder() - .baseUrl(Constant.BASE_URL) + .baseUrl(BASE_URL) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) - .addConverterFactory(GsonConverterFactory.create(EasyApplication.getInstance().gson)) + .addConverterFactory(GsonConverterFactory.create()) .client(okHttpClient) .build(); } diff --git a/basic/src/main/java/com/common/view/Constant.java b/basic/src/main/java/com/common/view/Constant.java deleted file mode 100644 index 944c16f..0000000 --- a/basic/src/main/java/com/common/view/Constant.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.common.view; - -/** - * author miekoz on 2016/3/16. - * email meikoz@126.com - */ -public class Constant { - public static final String GANK_DATA_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"; - public static final String BASE_URL = "http://gank.avosapps.com/api/"; -} diff --git a/basic/src/main/java/com/common/view/base/BaseActivity.java b/basic/src/main/java/com/common/view/base/BaseActivity.java index 7a3d91e..5bc0794 100644 --- a/basic/src/main/java/com/common/view/base/BaseActivity.java +++ b/basic/src/main/java/com/common/view/base/BaseActivity.java @@ -7,6 +7,8 @@ import com.common.R; +import java.util.logging.Logger; + import butterknife.ButterKnife; /** @@ -15,7 +17,6 @@ */ public abstract class BaseActivity extends FragmentActivity { - protected EasyToolBar mTitleBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -28,15 +29,6 @@ protected void onCreate(Bundle savedInstanceState) { protected abstract void onInitView(); - protected void initTitleBar(String title) { - findTitleBar(); - mTitleBar.setTitle(title); - } - - private void findTitleBar() { - mTitleBar = (EasyToolBar) findViewById(R.id.toolbar); - } - @Override protected void onDestroy() { super.onDestroy(); diff --git a/basic/src/main/java/com/common/view/base/SwipBackActivity.java b/basic/src/main/java/com/common/view/base/SwipBackActivity.java deleted file mode 100644 index 914059c..0000000 --- a/basic/src/main/java/com/common/view/base/SwipBackActivity.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.common.view.base; - -import android.content.Intent; -import android.os.Bundle; -import android.view.LayoutInflater; - -import com.common.R; -import com.common.view.widget.SwipeBackLayout; - -/** - * author meikoz on 2016/4/1. - * email meikoz@126.com - */ -public abstract class SwipBackActivity extends BaseActivity { - - protected SwipeBackLayout mSwipBack; - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - mSwipBack = (SwipeBackLayout) LayoutInflater.from(this).inflate( - R.layout.base_swip_layout, null); - mSwipBack.attachToActivity(this); - } - - @Override - public void startActivity(Intent intent) { - super.startActivity(intent); - overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain); - } - - // Press the back button in mobile phone - @Override - public void onBackPressed() { - super.onBackPressed(); - overridePendingTransition(0, R.anim.base_slide_right_out); - } -} diff --git a/basic/src/main/java/com/common/view/base/SwipeBackActivity.java b/basic/src/main/java/com/common/view/base/SwipeBackActivity.java new file mode 100644 index 0000000..718ac48 --- /dev/null +++ b/basic/src/main/java/com/common/view/base/SwipeBackActivity.java @@ -0,0 +1,60 @@ +package com.common.view.base; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.Window; +import android.widget.ImageView; +import android.widget.RelativeLayout; +import android.widget.RelativeLayout.LayoutParams; + +import com.common.R; + +/** + * Created by Eric on 15/3/3. + */ +public abstract class SwipeBackActivity extends AppCompatActivity implements SwipeBackLayout.SwipeBackListener { + + private SwipeBackLayout swipeBackLayout; + private ImageView ivShadow; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + requestWindowFeature(Window.FEATURE_NO_TITLE); + } + + @Override + public void setContentView(int layoutResID) { + super.setContentView(getContainer()); + View view = LayoutInflater.from(this).inflate(layoutResID, null); + swipeBackLayout.addView(view); + } + + private View getContainer() { + RelativeLayout container = new RelativeLayout(this); + swipeBackLayout = new SwipeBackLayout(this); + swipeBackLayout.setOnSwipeBackListener(this); + ivShadow = new ImageView(this); + ivShadow.setBackgroundColor(getResources().getColor(R.color.black_p50)); + LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); + container.addView(ivShadow, params); + container.addView(swipeBackLayout); + return container; + } + + public void setDragEdge(SwipeBackLayout.DragEdge dragEdge) { + swipeBackLayout.setDragEdge(dragEdge); + } + + public SwipeBackLayout getSwipeBackLayout() { + return swipeBackLayout; + } + + @Override + public void onViewPositionChanged(float fractionAnchor, float fractionScreen) { + ivShadow.setAlpha(1 - fractionScreen); + } + +} diff --git a/basic/src/main/java/com/common/view/base/SwipeBackLayout.java b/basic/src/main/java/com/common/view/base/SwipeBackLayout.java new file mode 100644 index 0000000..e03cb0c --- /dev/null +++ b/basic/src/main/java/com/common/view/base/SwipeBackLayout.java @@ -0,0 +1,468 @@ +/* + * Copyright 2015 Eric Liu + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.common.view.base; + +import android.app.Activity; +import android.content.Context; +import android.support.v4.view.ViewCompat; +import android.support.v4.view.ViewPager; +import android.support.v4.widget.ViewDragHelper; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; +import android.webkit.WebView; +import android.widget.AbsListView; +import android.widget.ScrollView; + +/** + * Swipe or Pull to finish a Activity. + *

+ * This layout must be a root layout and contains only one direct child view. + *

+ * The activity must use a theme that with translucent style. + * + *

+ * Created by Eric on 15/1/8. + */ +public class SwipeBackLayout extends ViewGroup { + + private static final String TAG = "SwipeBackLayout"; + + public enum DragEdge { + LEFT, + + TOP, + + RIGHT, + + BOTTOM + } + + private DragEdge dragEdge = DragEdge.TOP; + + public void setDragEdge(DragEdge dragEdge) { + this.dragEdge = dragEdge; + } + + + private static final double AUTO_FINISHED_SPEED_LIMIT = 2000.0; + + private final ViewDragHelper viewDragHelper; + + private View target; + + private View scrollChild; + + private int verticalDragRange = 0; + + private int horizontalDragRange = 0; + + private int draggingState = 0; + + private int draggingOffset; + + /** + * Whether allow to pull this layout. + */ + private boolean enablePullToBack = true; + + private static final float BACK_FACTOR = 0.5f; + + /** + * the anchor of calling finish. + */ + private float finishAnchor = 0; + + /** + * Set the anchor of calling finish. + * + * @param offset + */ + public void setFinishAnchor(float offset) { + finishAnchor = offset; + } + + private boolean enableFlingBack = true; + + /** + * Whether allow to finish activity by fling the layout. + * + * @param b + */ + public void setEnableFlingBack(boolean b) { + enableFlingBack = b; + } + + private SwipeBackListener swipeBackListener; + + @Deprecated + public void setOnPullToBackListener(SwipeBackListener listener) { + swipeBackListener = listener; + } + + public void setOnSwipeBackListener(SwipeBackListener listener) { + swipeBackListener = listener; + } + + public SwipeBackLayout(Context context) { + this(context, null); + } + + public SwipeBackLayout(Context context, AttributeSet attrs) { + super(context, attrs); + + viewDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelperCallBack()); + } + + public void setScrollChild(View view) { + scrollChild = view; + } + + public void setEnablePullToBack(boolean b) { + enablePullToBack = b; + } + + private void ensureTarget() { + if (target == null) { + if (getChildCount() > 1) { + throw new IllegalStateException("SwipeBackLayout must contains only one direct child"); + } + target = getChildAt(0); + + if (scrollChild == null && target != null) { + if (target instanceof ViewGroup) { + findScrollView((ViewGroup) target); + } else { + scrollChild = target; + } + + } + } + } + + /** + * Find out the scrollable child view from a ViewGroup. + * + * @param viewGroup + */ + private void findScrollView(ViewGroup viewGroup) { + scrollChild = viewGroup; + if (viewGroup.getChildCount() > 0) { + int count = viewGroup.getChildCount(); + View child; + for (int i = 0; i < count; i++) { + child = viewGroup.getChildAt(i); + if (child instanceof AbsListView || child instanceof ScrollView || child instanceof ViewPager || child instanceof WebView) { + scrollChild = child; + return; + } + } + } + } + + @Override + protected void onLayout(boolean changed, int l, int t, int r, int b) { + int width = getMeasuredWidth(); + int height = getMeasuredHeight(); + if (getChildCount() == 0) return; + + View child = getChildAt(0); + + int childWidth = width - getPaddingLeft() - getPaddingRight(); + int childHeight = height - getPaddingTop() - getPaddingBottom(); + int childLeft = getPaddingLeft(); + int childTop = getPaddingTop(); + int childRight = childLeft + childWidth; + int childBottom = childTop + childHeight; + child.layout(childLeft, childTop, childRight, childBottom); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + if (getChildCount() > 1) { + throw new IllegalStateException("SwipeBackLayout must contains only one direct child."); + } + + if (getChildCount() > 0) { + int measureWidth = MeasureSpec.makeMeasureSpec(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(), MeasureSpec.EXACTLY); + int measureHeight = MeasureSpec.makeMeasureSpec(getMeasuredHeight() - getPaddingTop() - getPaddingBottom(), MeasureSpec.EXACTLY); + getChildAt(0).measure(measureWidth, measureHeight); + } + } + + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + super.onSizeChanged(w, h, oldw, oldh); + verticalDragRange = h; + horizontalDragRange = w; + + switch (dragEdge) { + case TOP: + case BOTTOM: + finishAnchor = finishAnchor > 0 ? finishAnchor : verticalDragRange * BACK_FACTOR; + break; + case LEFT: + case RIGHT: + finishAnchor = finishAnchor > 0 ? finishAnchor : horizontalDragRange * BACK_FACTOR; + break; + } + } + + private int getDragRange() { + switch (dragEdge) { + case TOP: + case BOTTOM: + return verticalDragRange; + case LEFT: + case RIGHT: + return horizontalDragRange; + default: + return verticalDragRange; + } + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent ev) { + boolean handled = false; + ensureTarget(); + if (isEnabled()) { + handled = viewDragHelper.shouldInterceptTouchEvent(ev); + } else { + viewDragHelper.cancel(); + } + return !handled ? super.onInterceptTouchEvent(ev) : handled; + } + + @Override + public boolean onTouchEvent(MotionEvent event) { + viewDragHelper.processTouchEvent(event); + return true; + } + + @Override + public void computeScroll() { + if (viewDragHelper.continueSettling(true)) { + ViewCompat.postInvalidateOnAnimation(this); + } + } + + public boolean canChildScrollUp() { + return ViewCompat.canScrollVertically(scrollChild, -1); + } + + public boolean canChildScrollDown() { + return ViewCompat.canScrollVertically(scrollChild, 1); + } + + private boolean canChildScrollRight() { + return ViewCompat.canScrollHorizontally(scrollChild, -1); + } + + private boolean canChildScrollLeft() { + return ViewCompat.canScrollHorizontally(scrollChild, 1); + } + + private void finish() { + Activity act = (Activity) getContext(); + act.finish(); + act.overridePendingTransition(0, android.R.anim.fade_out); + } + + private class ViewDragHelperCallBack extends ViewDragHelper.Callback { + + @Override + public boolean tryCaptureView(View child, int pointerId) { + return child == target && enablePullToBack; + } + + @Override + public int getViewVerticalDragRange(View child) { + return verticalDragRange; + } + + @Override + public int getViewHorizontalDragRange(View child) { + return horizontalDragRange; + } + + @Override + public int clampViewPositionVertical(View child, int top, int dy) { + + int result = 0; + + if (dragEdge == DragEdge.TOP && !canChildScrollUp() && top > 0) { + final int topBound = getPaddingTop(); + final int bottomBound = verticalDragRange; + result = Math.min(Math.max(top, topBound), bottomBound); + } else if (dragEdge == DragEdge.BOTTOM && !canChildScrollDown() && top < 0) { + final int topBound = -verticalDragRange; + final int bottomBound = getPaddingTop(); + result = Math.min(Math.max(top, topBound), bottomBound); + } + + return result; + } + + @Override + public int clampViewPositionHorizontal(View child, int left, int dx) { + + int result = 0; + + if (dragEdge == DragEdge.LEFT && !canChildScrollRight() && left > 0) { + final int leftBound = getPaddingLeft(); + final int rightBound = horizontalDragRange; + result = Math.min(Math.max(left, leftBound), rightBound); + } else if (dragEdge == DragEdge.RIGHT && !canChildScrollLeft() && left < 0) { + final int leftBound = -horizontalDragRange; + final int rightBound = getPaddingLeft(); + result = Math.min(Math.max(left, leftBound), rightBound); + } + + return result; + } + + @Override + public void onViewDragStateChanged(int state) { + if (state == draggingState) return; + + if ((draggingState == ViewDragHelper.STATE_DRAGGING || draggingState == ViewDragHelper.STATE_SETTLING) && + state == ViewDragHelper.STATE_IDLE) { + // the view stopped from moving. + if (draggingOffset == getDragRange()) { + finish(); + } + } + + draggingState = state; + } + + + @Override + public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { + switch (dragEdge) { + case TOP: + case BOTTOM: + draggingOffset = Math.abs(top); + break; + case LEFT: + case RIGHT: + draggingOffset = Math.abs(left); + break; + default: + break; + } + + //The proportion of the sliding. + float fractionAnchor = (float) draggingOffset / finishAnchor; + if (fractionAnchor >= 1) fractionAnchor = 1; + + float fractionScreen = (float) draggingOffset / (float) getDragRange(); + if (fractionScreen >= 1) fractionScreen = 1; + + if (swipeBackListener != null) { + swipeBackListener.onViewPositionChanged(fractionAnchor, fractionScreen); + } + } + + @Override + public void onViewReleased(View releasedChild, float xvel, float yvel) { + if (draggingOffset == 0) return; + + if (draggingOffset == getDragRange()) return; + + boolean isBack = false; + + if (enableFlingBack && backBySpeed(xvel, yvel)) { + isBack = !canChildScrollUp(); + } else if (draggingOffset >= finishAnchor) { + isBack = true; + } else if (draggingOffset < finishAnchor) { + isBack = false; + } + + int finalLeft; + int finalTop; + switch (dragEdge) { + case LEFT: + finalLeft = isBack ? horizontalDragRange : 0; + smoothScrollToX(finalLeft); + break; + case RIGHT: + finalLeft = isBack ? -horizontalDragRange : 0; + smoothScrollToX(finalLeft); + break; + case TOP: + finalTop = isBack ? verticalDragRange : 0; + smoothScrollToY(finalTop); + break; + case BOTTOM: + finalTop = isBack ? -verticalDragRange : 0; + smoothScrollToY(finalTop); + break; + } + + } + } + + private boolean backBySpeed(float xvel, float yvel) { + switch (dragEdge) { + case TOP: + case BOTTOM: + if (Math.abs(yvel) > Math.abs(xvel) && Math.abs(yvel) > AUTO_FINISHED_SPEED_LIMIT) { + return dragEdge == DragEdge.TOP ? !canChildScrollUp() : !canChildScrollDown(); + } + break; + case LEFT: + case RIGHT: + if (Math.abs(xvel) > Math.abs(yvel) && Math.abs(xvel) > AUTO_FINISHED_SPEED_LIMIT) { + return dragEdge == DragEdge.LEFT ? !canChildScrollLeft() : !canChildScrollRight(); + } + break; + } + return false; + } + + private void smoothScrollToX(int finalLeft) { + if (viewDragHelper.settleCapturedViewAt(finalLeft, 0)) { + ViewCompat.postInvalidateOnAnimation(SwipeBackLayout.this); + } + } + + private void smoothScrollToY(int finalTop) { + if (viewDragHelper.settleCapturedViewAt(0, finalTop)) { + ViewCompat.postInvalidateOnAnimation(SwipeBackLayout.this); + } + } + + public interface SwipeBackListener { + + /** + * Return scrolled fraction of the layout. + * + * @param fractionAnchor relative to the anchor. + * @param fractionScreen relative to the screen. + */ + void onViewPositionChanged(float fractionAnchor, float fractionScreen); + + } + +} diff --git a/basic/src/main/java/com/common/view/base/WebViewActivity.java b/basic/src/main/java/com/common/view/base/WebViewActivity.java deleted file mode 100644 index ef9ea95..0000000 --- a/basic/src/main/java/com/common/view/base/WebViewActivity.java +++ /dev/null @@ -1,134 +0,0 @@ -package com.common.view.base; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.content.Intent; -import android.text.TextUtils; -import android.view.View; -import android.webkit.WebChromeClient; -import android.webkit.WebSettings; -import android.webkit.WebView; -import android.webkit.WebViewClient; -import android.widget.ProgressBar; - -import com.common.R; -import com.common.utils.WebViewUtil; - -/** - * author miekoz on 2016/3/15. - * email meikoz@126.com - */ -public class WebViewActivity extends BaseActivity { - - private static final int PROGRESS_RATIO = 1000; - ProgressBar webviewPb; - WebView webview; - EasyToolBar toolbar; - - public static void start(Context context, String url, String title, boolean isShow) { - if (TextUtils.isEmpty(url)) throw new IllegalArgumentException("url must not be empty"); - Intent intent = new Intent(context, WebViewActivity.class); - intent.putExtra("url", url); - intent.putExtra("title", title); - context.startActivity(intent); - } - - public static void start(Context context, String url, String title) { - start(context, url, title, false); - } - - - @Override - protected int getLayoutResource() { - return R.layout.activity_webview; - } - - @Override - protected void onInitView() { - webviewPb = (ProgressBar) this.findViewById(R.id.webview_pb); - webview = (WebView) this.findViewById(R.id.webview); - toolbar = (EasyToolBar) this.findViewById(R.id.toolbar); - initTitleBar(TextUtils.isEmpty(getUrlTitle()) ? "" : getUrlTitle()); - this.enableJavascript(); - this.enableCaching(); - this.enableCustomClients(); - this.enableAdjust(); - this.zoomedOut(); - this.webview.loadUrl(this.getUrl()); - - } - - @SuppressLint("SetJavaScriptEnabled") - private void enableJavascript() { - this.webview.getSettings().setJavaScriptEnabled(true); - this.webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); - } - - private void enableCaching() { - this.webview.getSettings().setAppCachePath(getFilesDir() + getPackageName() + "/cache"); - this.webview.getSettings().setAppCacheEnabled(true); - this.webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); - } - - private void enableAdjust() { - this.webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); - this.webview.getSettings().setLoadWithOverviewMode(true); - } - - private void zoomedOut() { - this.webview.getSettings().setLoadWithOverviewMode(true); - this.webview.getSettings().setUseWideViewPort(true); - this.webview.getSettings().setSupportZoom(true); - } - - private String getUrl() { - return this.getIntent().getStringExtra("url"); - } - - private String getUrlTitle() { - return this.getIntent().getStringExtra("title"); - } - - private void enableCustomClients() { - this.webview.setWebViewClient(new WebViewClient() { - @Override - public boolean shouldOverrideUrlLoading(WebView view, String url) { - view.loadUrl(url); - return true; - } - - /** - * @param view The WebView that is initiating the callback. - * @param url The url of the page. - */ - @Override - public void onPageFinished(WebView view, String url) { - super.onPageFinished(view, url); - if (url.contains("www.vmovier.com")) { - WebViewUtil.injectCss(WebViewActivity.this, WebViewActivity.this.webview, "vmovier.css"); - } else if (url.contains("video.weibo.com")) { - WebViewUtil.injectCss(WebViewActivity.this, WebViewActivity.this.webview, "weibo.css"); - } else if (url.contains("m.miaopai.com")) { - WebViewUtil.injectCss(WebViewActivity.this, WebViewActivity.this.webview, "miaopai.css"); - } - } - }); - this.webview.setWebChromeClient(new WebChromeClient() { - public void onProgressChanged(WebView view, int progress) { - WebViewActivity.this.webviewPb.setProgress(progress); - setProgress(progress * PROGRESS_RATIO); - if (progress >= 80) { - WebViewActivity.this.webviewPb.setVisibility(View.GONE); - } else { - WebViewActivity.this.webviewPb.setVisibility(View.VISIBLE); - } - } - }); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - if (this.webview != null) this.webview.destroy(); - } -} diff --git a/basic/src/main/java/com/common/view/widget/SwipeBackLayout.java b/basic/src/main/java/com/common/view/widget/SwipeBackLayout.java deleted file mode 100644 index 5d2fa78..0000000 --- a/basic/src/main/java/com/common/view/widget/SwipeBackLayout.java +++ /dev/null @@ -1,240 +0,0 @@ -package com.common.view.widget; - -import android.app.Activity; -import android.content.Context; -import android.content.res.TypedArray; -import android.graphics.Canvas; -import android.graphics.Rect; -import android.graphics.drawable.Drawable; -import android.support.v4.view.ViewPager; -import android.util.AttributeSet; -import android.util.Log; -import android.view.MotionEvent; -import android.view.View; -import android.view.ViewConfiguration; -import android.view.ViewGroup; -import android.widget.FrameLayout; -import android.widget.Scroller; - -import com.common.R; - -import java.util.LinkedList; -import java.util.List; - -/** - * author meikoz on 2016/4/1. - * email meikoz@126.com - */ -public class SwipeBackLayout extends FrameLayout { - private static final String TAG = SwipeBackLayout.class.getSimpleName(); - private View mContentView; - private int mTouchSlop; - private int downX; - private int downY; - private int tempX; - private Scroller mScroller; - private int viewWidth; - private boolean isSilding; - private boolean isFinish; - private Drawable mShadowDrawable; - private Activity mActivity; - private List mViewPagers = new LinkedList(); - - public SwipeBackLayout(Context context, AttributeSet attrs) { - this(context, attrs, 0); - } - - public SwipeBackLayout(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - - mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); - mScroller = new Scroller(context); - - mShadowDrawable = getResources().getDrawable(R.drawable.shadow_left); - } - - - public void attachToActivity(Activity activity) { - mActivity = activity; - TypedArray a = activity.getTheme().obtainStyledAttributes( - new int[] { android.R.attr.windowBackground }); - int background = a.getResourceId(0, 0); - a.recycle(); - - ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView(); - ViewGroup decorChild = (ViewGroup) decor.getChildAt(0); - decorChild.setBackgroundResource(background); - decor.removeView(decorChild); - addView(decorChild); - setContentView(decorChild); - decor.addView(this); - } - - private void setContentView(View decorChild) { - mContentView = (View) decorChild.getParent(); - } - - /** - * 事件拦截操作 - */ - @Override - public boolean onInterceptTouchEvent(MotionEvent ev) { - //处理ViewPager冲突问题 - ViewPager mViewPager = getTouchViewPager(mViewPagers, ev); - Log.i(TAG, "mViewPager = " + mViewPager); - - if(mViewPager != null && mViewPager.getCurrentItem() != 0){ - return super.onInterceptTouchEvent(ev); - } - - switch (ev.getAction()) { - case MotionEvent.ACTION_DOWN: - downX = tempX = (int) ev.getRawX(); - downY = (int) ev.getRawY(); - break; - case MotionEvent.ACTION_MOVE: - int moveX = (int) ev.getRawX(); - // 满足此条件屏蔽SildingFinishLayout里面子类的touch事件 - if (moveX - downX > mTouchSlop - && Math.abs((int) ev.getRawY() - downY) < mTouchSlop) { - return true; - } - break; - } - - return super.onInterceptTouchEvent(ev); - } - - @Override - public boolean onTouchEvent(MotionEvent event) { - switch (event.getAction()) { - case MotionEvent.ACTION_MOVE: - int moveX = (int) event.getRawX(); - int deltaX = tempX - moveX; - tempX = moveX; - if (moveX - downX > mTouchSlop - && Math.abs((int) event.getRawY() - downY) < mTouchSlop) { - isSilding = true; - } - - if (moveX - downX >= 0 && isSilding) { - mContentView.scrollBy(deltaX, 0); - } - break; - case MotionEvent.ACTION_UP: - isSilding = false; - if (mContentView.getScrollX() <= -viewWidth / 2) { - isFinish = true; - scrollRight(); - } else { - scrollOrigin(); - isFinish = false; - } - break; - } - - return true; - } - - /** - * 获取SwipeBackLayout里面的ViewPager的集合 - * @param mViewPagers - * @param parent - */ - private void getAlLViewPager(List mViewPagers, ViewGroup parent){ - int childCount = parent.getChildCount(); - for(int i=0; i mViewPagers, MotionEvent ev){ - if(mViewPagers == null || mViewPagers.size() == 0){ - return null; - } - Rect mRect = new Rect(); - for(ViewPager v : mViewPagers){ - v.getHitRect(mRect); - - if(mRect.contains((int)ev.getX(), (int)ev.getY())){ - return v; - } - } - return null; - } - - @Override - protected void onLayout(boolean changed, int l, int t, int r, int b) { - super.onLayout(changed, l, t, r, b); - if (changed) { - viewWidth = this.getWidth(); - - getAlLViewPager(mViewPagers, this); - Log.i(TAG, "ViewPager size = " + mViewPagers.size()); - } - } - - @Override - protected void dispatchDraw(Canvas canvas) { - super.dispatchDraw(canvas); - if (mShadowDrawable != null && mContentView != null) { - - int left = mContentView.getLeft() - - mShadowDrawable.getIntrinsicWidth(); - int right = left + mShadowDrawable.getIntrinsicWidth(); - int top = mContentView.getTop(); - int bottom = mContentView.getBottom(); - - mShadowDrawable.setBounds(left, top, right, bottom); - mShadowDrawable.draw(canvas); - } - - } - - - /** - * 滚动出界面 - */ - private void scrollRight() { - final int delta = (viewWidth + mContentView.getScrollX()); - // 调用startScroll方法来设置一些滚动的参数,我们在computeScroll()方法中调用scrollTo来滚动item - mScroller.startScroll(mContentView.getScrollX(), 0, -delta + 1, 0, - Math.abs(delta)); - postInvalidate(); - } - - /** - * 滚动到起始位置 - */ - private void scrollOrigin() { - int delta = mContentView.getScrollX(); - mScroller.startScroll(mContentView.getScrollX(), 0, -delta, 0, - Math.abs(delta)); - postInvalidate(); - } - - @Override - public void computeScroll() { - // 调用startScroll的时候scroller.computeScrollOffset()返回true, - if (mScroller.computeScrollOffset()) { - mContentView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); - postInvalidate(); - - if (mScroller.isFinished() && isFinish) { - mActivity.finish(); - } - } - } -} diff --git a/basic/src/main/res/drawable-xxhdpi/shadow_left.png b/basic/src/main/res/drawable-xxhdpi/shadow_left.png deleted file mode 100644 index 82c71c2..0000000 Binary files a/basic/src/main/res/drawable-xxhdpi/shadow_left.png and /dev/null differ diff --git a/basic/src/main/res/layout/activity_hosting.xml b/basic/src/main/res/layout/activity_hosting.xml deleted file mode 100644 index ebb8e78..0000000 --- a/basic/src/main/res/layout/activity_hosting.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - diff --git a/basic/src/main/res/layout/activity_webview.xml b/basic/src/main/res/layout/activity_webview.xml deleted file mode 100644 index 5c0efd8..0000000 --- a/basic/src/main/res/layout/activity_webview.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - diff --git a/basic/src/main/res/layout/base_swip_layout.xml b/basic/src/main/res/layout/base_swip_layout.xml deleted file mode 100644 index 9f482c1..0000000 --- a/basic/src/main/res/layout/base_swip_layout.xml +++ /dev/null @@ -1,5 +0,0 @@ - \ No newline at end of file diff --git a/basic/src/main/res/layout/fragment_list.xml b/basic/src/main/res/layout/fragment_list.xml deleted file mode 100644 index 82cf472..0000000 --- a/basic/src/main/res/layout/fragment_list.xml +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/basic/src/main/res/values/colors.xml b/basic/src/main/res/values/colors.xml index 0213513..bc618d8 100644 --- a/basic/src/main/res/values/colors.xml +++ b/basic/src/main/res/values/colors.xml @@ -27,4 +27,6 @@ #12FFFFFF #e0e0e0 + + #7f000000