您的位置:首页 > 其它

toolbar和SwipeBackLayout使用

2016-03-15 16:00 375 查看

效果图




一、SwipeBackLayout,就是侧滑返回上一页 的那个效果

如果要在自己的项目中使用的话,可以参考以下步骤: 
1、添加SwipeBackLayout作为你项目的依赖包 
2、让你的Activity继承SwipeBackActivity 
3、在你的Activity的Theme中添加<item name="android:windowIsTranslucent">true</item>

可能会遇到的问题:

1、首页需要禁止返回上一页

setSwipeBackEnable(false);
//禁止滑动删除   

2、在使用的主题中添加下面的属性,否则滑动时activity的下层是黑色的
<
item
name
=
"android:windowIsTranslucent"
>true</
item
>


3、 设置滑动方向,可设置EDGE_LEFT,
EDGE_RIGHT, EDGE_ALL, EDGE_BOTTOM  就是左右和底部都可以侧滑返回上一页

    mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);


4、滑动删除的效果只能从边界滑动才有效果,这样很不爽,贴吧里面都是任意位置侧滑就可以返回上一页




mSwipeBackLayout.setEdgeSize(intsize);//不过这个不管用,推荐改源码,修改<span style="font-family: 'Microsoft YaHei', Verdana, sans-serif, SimSun; font-size: 14px; line-height: 21px;">ViewDragHelper.java这个类源码中的getEdgeTouched(int x, int y)方法</span>
修改下面源码
private int getEdgeTouched(int x, int y) {
int result = 0;
result = EDGE_LEFT;//这样每次都是全屏左滑删除

//解决只有点击屏幕左边才有响应的问题
if (x < mParentView.getLeft() + mEdgeSize)
result = EDGE_LEFT;
if (y < mParentView.getTop() + mEdgeSize)
result = EDGE_TOP;
if (x > mParentView.getRight() - mEdgeSize)
result = EDGE_RIGHT;
if (y > mParentView.getBottom() - mEdgeSize)
result = EDGE_BOTTOM;

return result;
}


二、Toolbar

Toolbar是Actionbar的升级版  Actionbar使用的时候需要考虑到低版本兼容问题,而Toolbar可以兼容低版本

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary" >
</android.support.v7.widget.Toolbar>

<android.support.v4.widget.DrawerLayout
android:id="@+id/dl"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#f9f9f9" >
</FrameLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onCl
b2ec
ick="gotoNext"
android:text="gotoNext" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>

<!-- kaishi   android.support.v4.widget.DrawerLayout -->

</LinearLayout>

toolbar配置




protected void initActionBar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);

toolbar.setTitle("market");// 设置标题,要在setSupportActionBar前调用
toolbar.setTitleTextColor(Color.parseColor("#000000"));// 设置标题字体颜色

if (toolbar != null) {
setSupportActionBar(toolbar);
}

// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// getSupportActionBar().setHomeButtonEnabled(true);
// toolbar.setSubtitle("This is subtitle");// 设置子标题
// toolbar.setSubtitleTextColor(Color.parseColor("#000000"));// 副标题字体颜色

toolbar.setLogo(R.drawable.ic_launcher);// 设置标题左边的logo图片
// toolbar.setNavigationIcon(getResources().getDrawable(
// R.drawable.ic_navigation));// 设置导航按钮图标
// 用下面的ActionBarDrawerToggle就不用配置了
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open, R.string.drawer_close);

mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
// toolbar.setNavigationOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// mDrawerLayout.openDrawer(Gravity.LEFT);
// // mDrawerLayout.closeDrawer(Gravity.LEFT);
// Toast.makeText(getApplicationContext(),
// "ActionBarDrawerToggle点击事件", 0).show();
// }
// });
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "base_settings",
0).show();
break;
case R.id.action_share:
Toast.makeText(getApplicationContext(), "action_share", 0)
.show();
break;
default:
break;
}
return true;
}
});

}


右侧菜单按钮的设置

private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
//分享的设置
mShareActionProvider = (ShareActionProvider) MenuItemCompat
.getActionProvider(menu.findItem(R.id.action_share));
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/*");
mShareActionProvider.setShareIntent(intent);
// 搜索的监听
if (android.os.Build.VERSION.SDK_INT > 11) {
SearchView searchView = (SearchView) menu.findItem(
R.id.action_search).getActionView();
searchView.setOnQueryTextListener(this);
}
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
return mDrawerToggle.onOptionsItemSelected(item)|super.onOptionsItemSelected(item);//加上<span style="font-family: Arial, Helvetica, sans-serif;">DrawerToggle才能显示菜单</span>

}

关于按钮

首页按钮和返回按钮

首页按钮就是DrawerToggle,设置好DrawerToggle和DrawerLayout的关联

返回按钮需要在清单文件中配置一下需要返回的页面parentActivity

<activity
android:name=".NextActivity"
android:parentActivityName="com.zzx.zzxtoolbar.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.zzx.zzxtoolbar.MainActivity" />
</activity>


  

最后注意点

SwipeBackLayout源码中 me.imid.swipebacklayout.lib.app.SwipeBackActivity继承FragmentActivity的,而ActionBarActivity又是继承FragmentActivity,所以修改库源码SwipeBackActivity extends ActionBarActivity,这么写自己都看晕了,

SwipeBackActivity extends FragmentActivity

ActionBarActivity extends FragmentActivity

SwipeBackActivity extends ActionBarActivity(其实也带了FragmentActivity),这样就能解决页面中FragmentActivity继承问题了

可以写一个BaseActivity (继承SwipeBackActivity,在里面写Toolbar)
子类可以重写Toolbar的按钮



代码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SwipeBackLayout Toolbar