14 | * 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
15 | */
16 | public class MovieActivity extends BaseActivity {
17 | private Button mControlBtn;
18 |
19 | @Override
20 | protected void initView(Bundle savedInstanceState) {
21 | setContentView(R.layout.activity_movie);
22 | mControlBtn = getViewById(R.id.btn_movie_control);
23 | }
24 |
25 | @Override
26 | protected void setListener() {
27 | }
28 |
29 | @Override
30 | protected void processLogic(Bundle savedInstanceState) {
31 | mSwipeBackHelper.setIsNavigationBarOverlap(true);
32 | }
33 |
34 | public void hideNavigationBar(View view) {
35 | mControlBtn.postDelayed(new Runnable() {
36 | @Override
37 | public void run() {
38 | int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
39 | getWindow().getDecorView().setSystemUiVisibility(uiOptions);
40 |
41 | mControlBtn.setVisibility(View.INVISIBLE);
42 | }
43 | }, 2000);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/demo/src/main/res/layout/activity_translucent.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 | * 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
14 | */
15 | public class MainActivity extends BaseActivity {
16 |
17 | /**
18 | * 主界面不需要支持滑动返回,重写该方法永久禁用当前界面的滑动返回功能
19 | *
20 | * @return
21 | */
22 | @Override
23 | public boolean isSupportSwipeBack() {
24 | return false;
25 | }
26 |
27 | @Override
28 | protected void initView(Bundle savedInstanceState) {
29 | setContentView(R.layout.activity_main);
30 | mToolbar = getViewById(R.id.toolbar);
31 | }
32 |
33 | @Override
34 | protected void setListener() {
35 | }
36 |
37 | @Override
38 | protected void processLogic(Bundle savedInstanceState) {
39 | setSupportActionBar(mToolbar);
40 | setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
41 | }
42 |
43 | public void testSwipeBack(View v) {
44 | mSwipeBackHelper.forward(TestActivity.class);
45 | }
46 |
47 | public void testTranslucent(View v) {
48 | mSwipeBackHelper.forward(TranslucentActivity.class);
49 |
50 | // mSwipeBackHelper.forwardAndFinish(TranslucentActivity.class);
51 | }
52 |
53 | public void testPullRefreshAndWebView(View v) {
54 | mSwipeBackHelper.forward(WebViewActivity.class);
55 | }
56 |
57 | public void testSwipeDelete(View v) {
58 | mSwipeBackHelper.forward(SwipeDeleteActivity.class);
59 | }
60 |
61 | public void testRecyclerView(View v) {
62 | mSwipeBackHelper.forward(RecyclerIndexActivity.class);
63 | }
64 |
65 | public void testEditText(View v) {
66 | mSwipeBackHelper.forward(EditTextActivity.class);
67 | }
68 |
69 | public void testNavigationBarOverlap(View v) {
70 | mSwipeBackHelper.forward(MovieActivity.class);
71 | }
72 |
73 | @Override
74 | public void onBackPressed() {
75 | finish();
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/demo/src/main/java/cn/bingoogolapple/swipebacklayout/demo/adapter/SwipeDeleteAdapter.java:
--------------------------------------------------------------------------------
1 | package cn.bingoogolapple.swipebacklayout.demo.adapter;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | import cn.bingoogolapple.baseadapter.BGARecyclerViewAdapter;
9 | import cn.bingoogolapple.baseadapter.BGAViewHolderHelper;
10 | import cn.bingoogolapple.swipebacklayout.demo.R;
11 | import cn.bingoogolapple.swipebacklayout.demo.model.NormalModel;
12 | import cn.bingoogolapple.swipeitemlayout.BGASwipeItemLayout;
13 |
14 | /**
15 | * 作者:王浩 邮件:bingoogolapple@gmail.com
16 | * 创建时间:17/1/11 上午2:48
17 | * 描述:
18 | */
19 | public class SwipeDeleteAdapter extends BGARecyclerViewAdapter
25 | * 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
26 | */
27 | public class TranslucentActivity extends BaseActivity {
28 | private ContentAdapter mContentAdapter;
29 | private RecyclerView mContentRv;
30 |
31 | @Override
32 | protected void initView(Bundle savedInstanceState) {
33 | setContentView(R.layout.activity_translucent);
34 | mToolbar = getViewById(R.id.toolbar);
35 | mContentRv = getViewById(R.id.recyclerView);
36 | }
37 |
38 | @Override
39 | protected void setListener() {
40 | }
41 |
42 | @Override
43 | protected void processLogic(Bundle savedInstanceState) {
44 | initToolbar();
45 | initRecyclerView();
46 | StatusBarUtil.setTranslucentForImageView(this, 0, findViewById(R.id.ll_translucent_content));
47 | }
48 |
49 | private void initToolbar() {
50 | mToolbar.setBackgroundResource(android.R.color.transparent);
51 | setSupportActionBar(mToolbar);
52 | getSupportActionBar().setDisplayHomeAsUpEnabled(true);
53 | getSupportActionBar().setTitle("Test Translucent StatusBar");
54 | }
55 |
56 | private void initRecyclerView() {
57 | mContentRv.addItemDecoration(BGADivider.newBitmapDivider());
58 |
59 | mContentAdapter = new ContentAdapter(mContentRv);
60 | mContentAdapter.setOnRVItemClickListener(new BGAOnRVItemClickListener() {
61 | @Override
62 | public void onRVItemClick(ViewGroup parent, View itemView, int position) {
63 | Toast.makeText(parent.getContext(), "点击了条目 " + (position + 1), Toast.LENGTH_SHORT).show();
64 | if (position == 0) {
65 | mSwipeBackHelper.forward(RecyclerIndexActivity.class);
66 | }
67 | }
68 | });
69 | mContentAdapter.setOnRVItemLongClickListener(new BGAOnRVItemLongClickListener() {
70 | @Override
71 | public boolean onRVItemLongClick(ViewGroup parent, View itemView, int position) {
72 | Toast.makeText(parent.getContext(), "长按了条目 " + (position + 1), Toast.LENGTH_SHORT).show();
73 | return true;
74 | }
75 | });
76 | List
23 | * 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
24 | */
25 | public class SwipeDeleteActivity extends BaseActivity implements BGAOnRVItemClickListener, BGAOnRVItemLongClickListener, BGAOnItemChildClickListener, BGAOnItemChildLongClickListener {
26 | private SwipeDeleteAdapter mAdapter;
27 | private RecyclerView mDataRv;
28 |
29 | @Override
30 | protected void initView(Bundle savedInstanceState) {
31 | setContentView(R.layout.activity_swipe_delete);
32 | mDataRv = getViewById(R.id.rv_swipe_delete_data);
33 | }
34 |
35 | @Override
36 | protected void setListener() {
37 | mAdapter = new SwipeDeleteAdapter(mDataRv);
38 | mAdapter.setOnRVItemClickListener(this);
39 | mAdapter.setOnRVItemLongClickListener(this);
40 | mAdapter.setOnItemChildClickListener(this);
41 | mAdapter.setOnItemChildLongClickListener(this);
42 |
43 | mDataRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
44 | @Override
45 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
46 | if (RecyclerView.SCROLL_STATE_DRAGGING == newState) {
47 | mAdapter.closeOpenedSwipeItemLayoutWithAnim();
48 | }
49 | }
50 | });
51 | }
52 |
53 | @Override
54 | protected void processLogic(Bundle savedInstanceState) {
55 | initToolbar();
56 |
57 | mAdapter.setData(DataUtil.loadNormalModelDatas());
58 | mDataRv.addItemDecoration(BGADivider.newBitmapDivider());
59 | mDataRv.setAdapter(mAdapter);
60 | }
61 |
62 | private void initToolbar() {
63 | setSupportActionBar(mToolbar);
64 | getSupportActionBar().setDisplayHomeAsUpEnabled(true);
65 | getSupportActionBar().setTitle("测试滑动删除");
66 | setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
67 | }
68 |
69 | @Override
70 | public void onItemChildClick(ViewGroup parent, View childView, int position) {
71 | if (childView.getId() == R.id.tv_item_bgaswipe_delete) {
72 | mAdapter.closeOpenedSwipeItemLayoutWithAnim();
73 | mAdapter.removeItem(position);
74 | }
75 | }
76 |
77 | @Override
78 | public boolean onItemChildLongClick(ViewGroup parent, View childView, int position) {
79 | if (childView.getId() == R.id.tv_item_bgaswipe_delete) {
80 | Toast.makeText(this, "长按了删除 " + mAdapter.getItem(position).mTitle, Toast.LENGTH_SHORT).show();
81 | return true;
82 | }
83 | return false;
84 | }
85 |
86 | @Override
87 | public void onRVItemClick(ViewGroup parent, View itemView, int position) {
88 | Toast.makeText(this, "点击了条目 " + mAdapter.getItem(position).mTitle, Toast.LENGTH_SHORT).show();
89 | }
90 |
91 | @Override
92 | public boolean onRVItemLongClick(ViewGroup parent, View itemView, int position) {
93 | Toast.makeText(this, "长按了条目 " + mAdapter.getItem(position).mTitle, Toast.LENGTH_SHORT).show();
94 | return true;
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/demo/src/main/java/cn/bingoogolapple/swipebacklayout/demo/fragment/BaseFragment.java:
--------------------------------------------------------------------------------
1 | package cn.bingoogolapple.swipebacklayout.demo.fragment;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.os.Bundle;
6 | import android.support.annotation.IdRes;
7 | import android.support.annotation.LayoutRes;
8 | import android.support.v4.app.Fragment;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 |
13 | /**
14 | * 作者:王浩 邮件:bingoogolapple@gmail.com
15 | * 创建时间:16/12/27 下午11:29
16 | * 描述:
17 | */
18 | public abstract class BaseFragment extends Fragment {
19 | protected String TAG;
20 | protected View mContentView;
21 | protected Activity mActivity;
22 |
23 | protected boolean mIsLoadedData = false;
24 |
25 | @Override
26 | public void onAttach(Context context) {
27 | super.onAttach(context);
28 | TAG = this.getClass().getSimpleName();
29 | mActivity = getActivity();
30 | }
31 |
32 | @Override
33 | public void setUserVisibleHint(boolean isVisibleToUser) {
34 | super.setUserVisibleHint(isVisibleToUser);
35 | if (isResumed()) {
36 | handleOnVisibilityChangedToUser(isVisibleToUser);
37 | }
38 | }
39 |
40 | @Override
41 | public void onResume() {
42 | super.onResume();
43 | if (getUserVisibleHint()) {
44 | handleOnVisibilityChangedToUser(true);
45 | }
46 | }
47 |
48 | @Override
49 | public void onPause() {
50 | super.onPause();
51 | if (getUserVisibleHint()) {
52 | handleOnVisibilityChangedToUser(false);
53 | }
54 | }
55 |
56 | /**
57 | * 处理对用户是否可见
58 | *
59 | * @param isVisibleToUser
60 | */
61 | private void handleOnVisibilityChangedToUser(boolean isVisibleToUser) {
62 | if (isVisibleToUser) {
63 | // 对用户可见
64 | if (!mIsLoadedData) {
65 | mIsLoadedData = true;
66 | onLazyLoadOnce();
67 | }
68 | onVisibleToUser();
69 | } else {
70 | // 对用户不可见
71 | onInvisibleToUser();
72 | }
73 | }
74 |
75 | /**
76 | * 懒加载一次。如果只想在对用户可见时才加载数据,并且只加载一次数据,在子类中重写该方法
77 | */
78 | protected void onLazyLoadOnce() {
79 | }
80 |
81 | /**
82 | * 对用户可见时触发该方法。如果只想在对用户可见时才加载数据,在子类中重写该方法
83 | */
84 | protected void onVisibleToUser() {
85 | }
86 |
87 | /**
88 | * 对用户不可见时触发该方法
89 | */
90 | protected void onInvisibleToUser() {
91 | }
92 |
93 | @Override
94 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
95 | // 避免多次从xml中加载布局文件
96 | if (mContentView == null) {
97 | initView(savedInstanceState);
98 | setListener();
99 | processLogic(savedInstanceState);
100 | } else {
101 | ViewGroup parent = (ViewGroup) mContentView.getParent();
102 | if (parent != null) {
103 | parent.removeView(mContentView);
104 | }
105 | }
106 | return mContentView;
107 | }
108 |
109 | protected void setContentView(@LayoutRes int layoutResID) {
110 | mContentView = LayoutInflater.from(mActivity).inflate(layoutResID, null);
111 | }
112 |
113 | /**
114 | * 初始化View控件
115 | */
116 | protected abstract void initView(Bundle savedInstanceState);
117 |
118 | /**
119 | * 给View控件添加事件监听器
120 | */
121 | protected abstract void setListener();
122 |
123 | /**
124 | * 处理业务逻辑,状态恢复等操作
125 | *
126 | * @param savedInstanceState
127 | */
128 | protected abstract void processLogic(Bundle savedInstanceState);
129 |
130 | /**
131 | * 查找View
132 | *
133 | * @param id 控件的id
134 | * @param
23 | * 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
24 | */
25 | public class RecyclerIndexActivity extends BaseActivity implements BGAOnRVItemClickListener {
26 | private RecyclerIndexAdapter mAdapter;
27 | private RecyclerView mDataRv;
28 | private IndexView mIndexView;
29 | private TextView mTipTv;
30 | private BGARVVerticalScrollHelper mRecyclerViewScrollHelper;
31 |
32 | @Override
33 | protected void initView(Bundle savedInstanceState) {
34 | setContentView(R.layout.activity_recycler_index);
35 | mDataRv = getViewById(R.id.rv_recyclerindexview_data);
36 |
37 | mIndexView = getViewById(R.id.indexview);
38 | mTipTv = getViewById(R.id.tv_recyclerindexview_tip);
39 | }
40 |
41 | @Override
42 | protected void setListener() {
43 | mAdapter = new RecyclerIndexAdapter(mDataRv);
44 | mAdapter.setOnRVItemClickListener(this);
45 |
46 | final BGADivider.StickyDelegate stickyDelegate = new BGADivider.StickyDelegate() {
47 | @Override
48 | protected boolean isCategoryFistItem(int position) {
49 | return mAdapter.isCategory(position);
50 | }
51 |
52 | @Override
53 | protected String getCategoryName(int position) {
54 | return mAdapter.getItem(position).topc;
55 | }
56 |
57 | @Override
58 | protected int getFirstVisibleItemPosition() {
59 | return mRecyclerViewScrollHelper.findFirstVisibleItemPosition();
60 | }
61 | };
62 |
63 | mDataRv.addItemDecoration(BGADivider.newBitmapDivider()
64 | .setStartSkipCount(0)
65 | .setMarginLeftResource(R.dimen.size_level3)
66 | .setMarginRightResource(R.dimen.size_level9)
67 | .setDelegate(stickyDelegate));
68 | mRecyclerViewScrollHelper = BGARVVerticalScrollHelper.newInstance(mDataRv, new BGARVVerticalScrollHelper.SimpleDelegate() {
69 | @Override
70 | public int getCategoryHeight() {
71 | return stickyDelegate.getCategoryHeight();
72 | }
73 | });
74 |
75 | mIndexView.setDelegate(new IndexView.Delegate() {
76 | @Override
77 | public void onIndexViewSelectedChanged(IndexView indexView, String text) {
78 | int position = mAdapter.getPositionForCategory(text.charAt(0));
79 | if (position != -1) {
80 | mRecyclerViewScrollHelper.smoothScrollToPosition(position);
81 | }
82 | }
83 | });
84 | }
85 |
86 | @Override
87 | protected void processLogic(Bundle savedInstanceState) {
88 | initToolbar();
89 |
90 | mIndexView.setTipTv(mTipTv);
91 |
92 | mAdapter.setData(DataUtil.loadIndexModelData());
93 | mDataRv.setAdapter(mAdapter);
94 | }
95 |
96 | private void initToolbar() {
97 | setSupportActionBar(mToolbar);
98 | getSupportActionBar().setDisplayHomeAsUpEnabled(true);
99 | getSupportActionBar().setTitle("测试 RecyclerView");
100 | setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
101 | }
102 |
103 | @Override
104 | public void onRVItemClick(ViewGroup parent, View itemView, int position) {
105 | Toast.makeText(this, "选择了城市 " + mAdapter.getItem(position).name, Toast.LENGTH_SHORT).show();
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/demo/src/main/java/cn/bingoogolapple/swipebacklayout/demo/activity/WebViewActivity.java:
--------------------------------------------------------------------------------
1 | package cn.bingoogolapple.swipebacklayout.demo.activity;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 | import android.webkit.WebChromeClient;
6 | import android.webkit.WebView;
7 | import android.webkit.WebViewClient;
8 |
9 | import cn.bingoogolapple.progressbar.BGAProgressBar;
10 | import cn.bingoogolapple.refreshlayout.BGAMoocStyleRefreshViewHolder;
11 | import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
12 | import cn.bingoogolapple.swipebacklayout.demo.R;
13 |
14 | /**
15 | * 作者:王浩 邮件:bingoogolapple@gmail.com
16 | * 创建时间:16/12/28 上午10:10
17 | * 描述:测试下拉刷新、WebView等
18 | *
19 | * 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
20 | */
21 | public class WebViewActivity extends BaseActivity implements BGARefreshLayout.BGARefreshLayoutDelegate {
22 | private BGARefreshLayout mRefreshLayout;
23 | private WebView mContentWv;
24 | private BGAProgressBar mProgressBar;
25 |
26 | @Override
27 | protected void initView(Bundle savedInstanceState) {
28 | setContentView(R.layout.activity_webview);
29 | mRefreshLayout = getViewById(R.id.refreshLayout);
30 | mContentWv = getViewById(R.id.wv_webview_content);
31 | mProgressBar = getViewById(R.id.progressBar);
32 | }
33 |
34 | @Override
35 | protected void setListener() {
36 | mRefreshLayout.setDelegate(this);
37 |
38 | mContentWv.setWebViewClient(new WebViewClient() {
39 | @Override
40 | public void onPageFinished(WebView view, String url) {
41 | mRefreshLayout.endRefreshing();
42 | }
43 | });
44 | mContentWv.setWebChromeClient(new WebChromeClient() {
45 | @Override
46 | public void onProgressChanged(WebView view, int newProgress) {
47 | if (newProgress == 100) {
48 | mProgressBar.setVisibility(View.GONE);
49 | } else {
50 | if (mProgressBar.getVisibility() == View.GONE) {
51 | mProgressBar.setVisibility(View.VISIBLE);
52 | }
53 | mProgressBar.setProgress(newProgress);
54 | }
55 | }
56 | });
57 | findViewById(R.id.transparent).setOnClickListener(this);
58 | findViewById(R.id.not_transparent).setOnClickListener(this);
59 | findViewById(R.id.to_transparent).setOnClickListener(this);
60 | }
61 |
62 | @Override
63 | protected void processLogic(Bundle savedInstanceState) {
64 | initToolbar();
65 |
66 | initRefreshLayout();
67 |
68 | initWebView();
69 | }
70 |
71 | private void initToolbar() {
72 | setSupportActionBar(mToolbar);
73 | getSupportActionBar().setDisplayHomeAsUpEnabled(true);
74 | getSupportActionBar().setTitle("测试下拉刷新和WebView");
75 | setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
76 | }
77 |
78 | private void initRefreshLayout() {
79 | BGAMoocStyleRefreshViewHolder moocStyleRefreshViewHolder = new BGAMoocStyleRefreshViewHolder(this, false);
80 | moocStyleRefreshViewHolder.setOriginalImage(R.mipmap.bga_refresh_moooc);
81 | moocStyleRefreshViewHolder.setUltimateColor(R.color.imoocstyle);
82 | mRefreshLayout.setRefreshViewHolder(moocStyleRefreshViewHolder);
83 | }
84 |
85 | private void initWebView() {
86 | mContentWv.getSettings().setJavaScriptEnabled(true);
87 | mContentWv.loadUrl("https://github.com/bingoogolapple");
88 | }
89 |
90 | @Override
91 | public void onClick(View v) {
92 | if (v.getId() == R.id.transparent) {
93 | setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
94 | } else if (v.getId() == R.id.not_transparent) {
95 | setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark), 0);
96 | } else if (v.getId() == R.id.to_transparent) {
97 | mSwipeBackHelper.forward(TranslucentActivity.class);
98 | }
99 | }
100 |
101 | @Override
102 | public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
103 | mContentWv.reload();
104 | }
105 |
106 | @Override
107 | public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
108 | return false;
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/demo/src/main/res/layout/activity_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
24 | * 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
25 | */
26 | public abstract class BaseActivity extends AppCompatActivity implements BGASwipeBackHelper.Delegate, View.OnClickListener {
27 | protected BGASwipeBackHelper mSwipeBackHelper;
28 | protected Toolbar mToolbar;
29 |
30 | @Override
31 | protected void onCreate(@Nullable Bundle savedInstanceState) {
32 | // 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
33 | // 在 super.onCreate(savedInstanceState) 之前调用该方法
34 | initSwipeBackFinish();
35 | super.onCreate(savedInstanceState);
36 | Log.d(this.getClass().getSimpleName(), "onCreate");
37 |
38 |
39 | initView(savedInstanceState);
40 | mToolbar = getViewById(R.id.toolbar);
41 |
42 | setListener();
43 | processLogic(savedInstanceState);
44 | }
45 |
46 | /**
47 | * 初始化滑动返回。在 super.onCreate(savedInstanceState) 之前调用该方法
48 | */
49 | private void initSwipeBackFinish() {
50 | mSwipeBackHelper = new BGASwipeBackHelper(this, this);
51 |
52 | // 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
53 | // 下面几项可以不配置,这里只是为了讲述接口用法。
54 |
55 | // 设置滑动返回是否可用。默认值为 true
56 | mSwipeBackHelper.setSwipeBackEnable(true);
57 | // 设置是否仅仅跟踪左侧边缘的滑动返回。默认值为 true
58 | mSwipeBackHelper.setIsOnlyTrackingLeftEdge(true);
59 | // 设置是否是微信滑动返回样式。默认值为 true
60 | mSwipeBackHelper.setIsWeChatStyle(true);
61 | // 设置阴影资源 id。默认值为 R.drawable.bga_sbl_shadow
62 | mSwipeBackHelper.setShadowResId(R.drawable.bga_sbl_shadow);
63 | // 设置是否显示滑动返回的阴影效果。默认值为 true
64 | mSwipeBackHelper.setIsNeedShowShadow(true);
65 | // 设置阴影区域的透明度是否根据滑动的距离渐变。默认值为 true
66 | mSwipeBackHelper.setIsShadowAlphaGradient(true);
67 | // 设置触发释放后自动滑动返回的阈值,默认值为 0.3f
68 | mSwipeBackHelper.setSwipeBackThreshold(0.3f);
69 | // 设置底部导航条是否悬浮在内容上,默认值为 false
70 | mSwipeBackHelper.setIsNavigationBarOverlap(false);
71 | }
72 |
73 | /**
74 | * 是否支持滑动返回。这里在父类中默认返回 true 来支持滑动返回,如果某个界面不想支持滑动返回则重写该方法返回 false 即可
75 | *
76 | * @return
77 | */
78 | @Override
79 | public boolean isSupportSwipeBack() {
80 | return true;
81 | }
82 |
83 | /**
84 | * 正在滑动返回
85 | *
86 | * @param slideOffset 从 0 到 1
87 | */
88 | @Override
89 | public void onSwipeBackLayoutSlide(float slideOffset) {
90 | }
91 |
92 | /**
93 | * 没达到滑动返回的阈值,取消滑动返回动作,回到默认状态
94 | */
95 | @Override
96 | public void onSwipeBackLayoutCancel() {
97 | }
98 |
99 | /**
100 | * 滑动返回执行完毕,销毁当前 Activity
101 | */
102 | @Override
103 | public void onSwipeBackLayoutExecuted() {
104 | mSwipeBackHelper.swipeBackward();
105 | }
106 |
107 | @Override
108 | public void onBackPressed() {
109 | // 正在滑动返回的时候取消返回按钮事件
110 | if (mSwipeBackHelper.isSliding()) {
111 | return;
112 | }
113 | mSwipeBackHelper.backward();
114 | }
115 |
116 | /**
117 | * 设置状态栏颜色
118 | *
119 | * @param color
120 | */
121 | protected void setStatusBarColor(@ColorInt int color) {
122 | setStatusBarColor(color, StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA);
123 | }
124 |
125 | /**
126 | * 设置状态栏颜色
127 | *
128 | * @param color
129 | * @param statusBarAlpha 透明度
130 | */
131 | public void setStatusBarColor(@ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) {
132 | StatusBarUtil.setColorForSwipeBack(this, color, statusBarAlpha);
133 | }
134 |
135 | @Override
136 | public boolean onOptionsItemSelected(MenuItem item) {
137 | if (item.getItemId() == android.R.id.home) {
138 | onBackPressed();
139 | return true;
140 | }
141 | return super.onOptionsItemSelected(item);
142 | }
143 |
144 | /**
145 | * 初始化布局以及View控件
146 | */
147 | protected abstract void initView(Bundle savedInstanceState);
148 |
149 | /**
150 | * 给View控件添加事件监听器
151 | */
152 | protected abstract void setListener();
153 |
154 | /**
155 | * 处理业务逻辑,状态恢复等操作
156 | *
157 | * @param savedInstanceState
158 | */
159 | protected abstract void processLogic(Bundle savedInstanceState);
160 |
161 | /**
162 | * 需要处理点击事件时,重写该方法
163 | *
164 | * @param v
165 | */
166 | public void onClick(View v) {
167 | }
168 |
169 | /**
170 | * 查找View
171 | *
172 | * @param id 控件的id
173 | * @param
184 |
25 | * 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
26 | */
27 | public class TestActivity extends BaseActivity {
28 | private TabLayout mTabLayout;
29 | private ViewPager mViewPager;
30 | private SwitchCompat mSwipeBackEnableSwitch;
31 |
32 | @Override
33 | protected void initView(Bundle savedInstanceState) {
34 | setContentView(R.layout.activity_test);
35 | mToolbar = getViewById(R.id.toolbar);
36 | mViewPager = getViewById(R.id.viewPager);
37 | mSwipeBackEnableSwitch = getViewById(R.id.swipeBackEnableSwitch);
38 | }
39 |
40 | @Override
41 | protected void setListener() {
42 | testSwipeBackLayout();
43 | }
44 |
45 | @Override
46 | protected void processLogic(Bundle savedInstanceState) {
47 | initToolbar();
48 | setUpTabLayoutAndViewPager();
49 | }
50 |
51 | private void initToolbar() {
52 | setSupportActionBar(mToolbar);
53 | getSupportActionBar().setDisplayHomeAsUpEnabled(true);
54 | getSupportActionBar().setTitle("测试滑动返回布局的接口");
55 | }
56 |
57 | private void setUpTabLayoutAndViewPager() {
58 | mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
59 | @Override
60 | public void onPageSelected(int position) {
61 | changeTopBgColor(position);
62 | }
63 | });
64 | mViewPager.setAdapter(new ContentPagerAdapter(getSupportFragmentManager()));
65 | mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
66 | @Override
67 | public void onPageSelected(int position) {
68 | // 测试只有ViewPager在第0页时才开启滑动返回
69 | mSwipeBackHelper.setSwipeBackEnable(position == 0);
70 | mSwipeBackEnableSwitch.setChecked(position == 0);
71 | }
72 | });
73 |
74 | mTabLayout = getViewById(R.id.tabLayout);
75 | mTabLayout.setupWithViewPager(mViewPager);
76 | changeTopBgColor(mViewPager.getCurrentItem());
77 | }
78 |
79 | /**
80 | * 测试滑动返回相关接口
81 | */
82 | private void testSwipeBackLayout() {
83 | // 测试动态设置滑动返回是否可用
84 | mSwipeBackEnableSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
85 | @Override
86 | public void onCheckedChanged(CompoundButton buttonView, boolean swipeBackEnable) {
87 | /**
88 | * 设置滑动返回是否可用
89 | */
90 | mSwipeBackHelper.setSwipeBackEnable(swipeBackEnable);
91 | }
92 | });
93 |
94 | // 测试动态设置是否仅仅跟踪左侧边缘的滑动返回
95 | ((SwitchCompat) getViewById(R.id.onlyTrackingLeftEdgeSwitch)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
96 | @Override
97 | public void onCheckedChanged(CompoundButton buttonView, boolean isOnlyTrackingLeftEdge) {
98 | /**
99 | * 设置是否仅仅跟踪左侧边缘的滑动返回
100 | */
101 | mSwipeBackHelper.setIsOnlyTrackingLeftEdge(isOnlyTrackingLeftEdge);
102 | }
103 | });
104 |
105 | // 测试动态设置是否是微信滑动返回样式
106 | ((SwitchCompat) getViewById(R.id.weChatStyleSwitch)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
107 | @Override
108 | public void onCheckedChanged(CompoundButton buttonView, boolean isWeChatStyle) {
109 | /**
110 | * 设置是否是微信滑动返回样式
111 | */
112 | mSwipeBackHelper.setIsWeChatStyle(isWeChatStyle);
113 | }
114 | });
115 |
116 | // 测试动态设置是否显示滑动返回的阴影效果
117 | ((SwitchCompat) getViewById(R.id.needShowShadowSwitch)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
118 | @Override
119 | public void onCheckedChanged(CompoundButton buttonView, boolean isNeedShowShadow) {
120 | /**
121 | * 设置是否显示滑动返回的阴影效果
122 | */
123 | mSwipeBackHelper.setIsNeedShowShadow(isNeedShowShadow);
124 | }
125 | });
126 |
127 | // 测试动态设置阴影区域的透明度是否根据滑动的距离渐变
128 | ((SwitchCompat) getViewById(R.id.shadowAlphaGradientSwitch)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
129 | @Override
130 | public void onCheckedChanged(CompoundButton buttonView, boolean isShadowAlphaGradient) {
131 | /**
132 | * 设置阴影区域的透明度是否根据滑动的距离渐变
133 | */
134 | mSwipeBackHelper.setIsShadowAlphaGradient(isShadowAlphaGradient);
135 | }
136 | });
137 | }
138 |
139 | public void testTranslucent(View v) {
140 | mSwipeBackHelper.forward(TranslucentActivity.class);
141 |
142 | // mSwipeBackHelper.forwardAndFinish(TranslucentActivity.class);
143 | }
144 |
145 | public void testPullRefreshAndWebView(View v) {
146 | mSwipeBackHelper.forward(WebViewActivity.class);
147 | }
148 |
149 | public void testSwipeDelete(View v) {
150 | mSwipeBackHelper.forward(SwipeDeleteActivity.class);
151 | }
152 |
153 | public void testRecyclerView(View v) {
154 | mSwipeBackHelper.forward(RecyclerIndexActivity.class);
155 | }
156 |
157 | /**
158 | * 测试 ViewPager 的适配器
159 | */
160 | private class ContentPagerAdapter extends FragmentPagerAdapter {
161 | private ContentFragment mOneFragment;
162 | private ContentFragment mTwoFragment;
163 | private ContentFragment mThreeFragment;
164 |
165 | public ContentPagerAdapter(FragmentManager fragmentManager) {
166 | super(fragmentManager);
167 | }
168 |
169 | @Override
170 | public Fragment getItem(int position) {
171 | if (position == 0) {
172 | if (mOneFragment == null) {
173 | mOneFragment = ContentFragment.newInstance(position);
174 | }
175 | return mOneFragment;
176 | } else if (position == 1) {
177 | if (mTwoFragment == null) {
178 | mTwoFragment = ContentFragment.newInstance(position);
179 | }
180 | return mTwoFragment;
181 | } else {
182 | if (mThreeFragment == null) {
183 | mThreeFragment = ContentFragment.newInstance(position);
184 | }
185 | return mThreeFragment;
186 | }
187 | }
188 |
189 | @Override
190 | public int getCount() {
191 | return 3;
192 | }
193 |
194 | @Override
195 | public CharSequence getPageTitle(int position) {
196 | return "第" + (position + 1) + "页";
197 | }
198 | }
199 |
200 | /**
201 | * 根据Palette提取的颜色,修改tab和toolbar以及状态栏的颜色
202 | */
203 | private void changeTopBgColor(int position) {
204 | int imageResource = R.mipmap.one;
205 | if (position == 1) {
206 | imageResource = R.mipmap.two;
207 | } else if (position == 2) {
208 | imageResource = R.mipmap.three;
209 | }
210 | Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imageResource);
211 | Palette.Builder builder = Palette.from(bitmap);
212 | builder.generate(new Palette.PaletteAsyncListener() {
213 | @Override
214 | public void onGenerated(Palette palette) {
215 | Palette.Swatch vibrant = palette.getVibrantSwatch();
216 | mTabLayout.setBackgroundColor(vibrant.getRgb());
217 | mTabLayout.setSelectedTabIndicatorColor(colorBurn(vibrant.getRgb()));
218 | mTabLayout.setSelectedTabIndicatorColor(colorBurn(vibrant.getRgb()));
219 | mToolbar.setBackgroundColor(vibrant.getRgb());
220 |
221 | setStatusBarColor(colorBurn(vibrant.getRgb()));
222 | }
223 | });
224 | }
225 |
226 | /**
227 | * 颜色加深处理
228 | */
229 | private int colorBurn(int RGBValues) {
230 | int red = RGBValues >> 16 & 0xFF;
231 | int green = RGBValues >> 8 & 0xFF;
232 | int blue = RGBValues & 0xFF;
233 | red = (int) Math.floor(red * (1 - 0.1));
234 | green = (int) Math.floor(green * (1 - 0.1));
235 | blue = (int) Math.floor(blue * (1 - 0.1));
236 | return Color.rgb(red, green, blue);
237 | }
238 | }
239 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/bingoogolapple/swipebacklayout/BGASwipeBackShadowView.java:
--------------------------------------------------------------------------------
1 | package cn.bingoogolapple.swipebacklayout;
2 |
3 | import android.app.Activity;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Bitmap;
6 | import android.graphics.Canvas;
7 | import android.support.annotation.DrawableRes;
8 | import android.support.v4.view.ViewCompat;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 | import android.widget.FrameLayout;
12 | import android.widget.ImageView;
13 | import android.widget.LinearLayout;
14 | import android.widget.RelativeLayout;
15 |
16 | import java.lang.ref.WeakReference;
17 |
18 | /**
19 | * 作者:王浩 邮件:wanghao76@meituan.com
20 | * 创建时间:2017/10/13
21 | * 描述:左侧阴影控件
22 | */
23 | class BGASwipeBackShadowView extends FrameLayout {
24 | private static final String TAG = BGASwipeBackShadowView.class.getSimpleName();
25 | private static final float WE_CHAT_STYLE_MAX_OFFSET = 0.75f;
26 | private Activity mActivity;
27 | private WeakReference
185 |