问题描述
我已经搜索了一段时间,如下图所示,但不幸的是我找不到任何类似的东西
edit menu item
在展开时移动到 CollapsingToolbarLayout
的底部
我尝试了很长的菜单项属性组合,但没有成功
这是我的尝试
我的屏幕
<android.support.design.widget.AppBarLayoutandroid:id="@+id/app_bar"android:layout_width="match_parent"android:layout_height="@dimen/app_bar_height"机器人:fitsSystemWindows =真"android:theme="@style/AppTheme.AppBarOverlay"><android.support.design.widget.CollapsingToolbarLayoutandroid:id="@+id/toolbar_layout"android:layout_width="match_parent"android:layout_height="match_parent"机器人:fitsSystemWindows =真"app:contentScrim="?attr/colorPrimary"app:layout_scrollFlags="scroll|exitUntilCollapsed"app:toolbarId="@+id/toolbar"><图像视图android:id="@+id/照片"android:scaleType="centerCrop"android:transitionName="@string/ambassador_photo"android:layout_width="match_parent"android:layout_height="match_parent"机器人:layout_gravity="中心"机器人:fitsSystemWindows =真"应用程序:layout_collapseMode="视差"android:contentDescription="@string/ambassador_photo"/><android.support.v7.widget.Toolbarandroid:id="@+id/工具栏"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"应用程序:layout_collapseMode="pin"app:navigationIcon="@drawable/ic_arrow_back_white_24dp"app:popupTheme="@style/AppTheme.PopupOverlay"/></android.support.design.widget.CollapsingToolbarLayout></android.support.design.widget.AppBarLayout><include layout="@layout/content_ambassador"/></android.support.design.widget.CoordinatorLayout>
我的菜单
<menu <item android:id="@+id/action_skills"android:title="@string/skills"应用程序:showAsAction="ifRoom"android:orderInCategory="200"android:icon="@drawable/ic_local_activity_white_24dp"/><item android:id="@+id/action_edit_name"android:title="@string/edit_name"应用程序:showAsAction="ifRoom"android:orderInCategory="100"android:icon="@drawable/ic_edit_white_24dp"/></菜单>
如何实现像WhatsApp组一样的菜单项行为?
感谢您的任何建议!
来自
希望这对你有帮助.
I've been searching for a while as shown on the following images but unfortunately I was not able to find anything similar
edit menu item
is moving to bottom of CollapsingToolbarLayout
when it is expanded
I tried a long combination of atributes for menu item but without success
This is my attempt
My screen
<?
My menu
<menu
How to implement this menu item behavior like WhatsApp group?
Thanks for any suggestion!
From the suggested project i will show you some modified approach to get your desired behaviour.
Also i added my project in github also Check Here
Demonstration
- Create top_view(which is visible in toolbar) & extended_view(which is visible in collapsing layout).
- Create custom class which is common to top_view & extended_view.
- Add
Coordinatelayout
behaviour to extended view. - Update both views visibility,position & size based on AppBar
onOffsetchangedListener
.
Here is the code
header_view.(Extended View)
<?
header_view_top.(Top View)
<?
activity_main.
<?
HeaderView.Java(Custom class)
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
public class HeaderView extends RelativeLayout {
@BindView(R.id.name)
TextView tvName;
@BindView(R.id.description)
TextView tvDescription;
public HeaderView(Context context) {
super(context);
}
public HeaderView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public HeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
ButterKnife.bind(this);
}
public void bindTo(String name, String lastSeen) {
this.tvName.setText(name);
this.tvDescription.setText(lastSeen);
}
public void setTextSize(float size) {
tvName.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
}
Header_behaviour.java
public class HeaderBehaviour extends CoordinatorLayout.Behavior<HeaderView> {
private Context mContext;
private int mStartMarginLeft;
private int mEndMarginLeft;
private int mMarginRight;
private int mStartMarginBottom;
private float mTitleStartSize;
private float mTitleEndSize;
private boolean isHide;
public HeaderBehaviour(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
public HeaderBehaviour(Context context, AttributeSet attrs, Context mContext) {
super(context, attrs);
this.mContext = mContext;
}
public static int getToolbarHeight(Context context) {
int result = 0;
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
result = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
}
return result;
}
@Override
public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull HeaderView child, @NonNull View dependency) {
return dependency instanceof AppBarLayout;
}
@Override
public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull HeaderView child, @NonNull View dependency) {
shouldInitProperties();
int maxScroll = ((AppBarLayout) dependency).getTotalScrollRange();
float percentage = Math.abs(dependency.getY()) / (float) maxScroll;
float childPosition = dependency.getHeight()
+ dependency.getY()
- child.getHeight()
- (getToolbarHeight(mContext) - child.getHeight()) * percentage / 2;
childPosition = childPosition - mStartMarginBottom * (1f - percentage);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
if (Math.abs(dependency.getY()) >= maxScroll / 2) {
float layoutPercentage = (Math.abs(dependency.getY()) - (maxScroll / 2)) / Math.abs(maxScroll / 2);
lp.leftMargin = (int) (layoutPercentage * mEndMarginLeft) + mStartMarginLeft;
lp.rightMargin = (int) (layoutPercentage * mEndMarginLeft) + mStartMarginLeft;
child.setTextSize(getTranslationOffset(mTitleStartSize, mTitleEndSize, layoutPercentage));
} else {
lp.leftMargin = mStartMarginLeft;
lp.rightMargin = mStartMarginLeft;
}
child.setLayoutParams(lp);
child.setY(childPosition);
if (isHide && percentage < 1) {
child.setVisibility(View.VISIBLE);
isHide = false;
} else if (!isHide && percentage == 1) {
child.setVisibility(View.GONE);
isHide = true;
}
return true;
}
private float getTranslationOffset(float expandedOffset, float collapsedOffset, float ratio) {
return expandedOffset + ratio * (collapsedOffset - expandedOffset);
}
private void shouldInitProperties() {
if (mStartMarginLeft == 0) {
mStartMarginLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_start_margin_left);
}
if (mEndMarginLeft == 0) {
mEndMarginLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_end_margin_left);
}
if (mStartMarginBottom == 0) {
mStartMarginBottom = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_start_margin_bottom);
}
if (mMarginRight == 0) {
mMarginRight = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_end_margin_right);
}
if (mTitleStartSize == 0) {
mTitleEndSize = mContext.getResources().getDimensionPixelSize(R.dimen.header_view_end_text_size);
}
if (mTitleStartSize == 0) {
mTitleStartSize = mContext.getResources().getDimensionPixelSize(R.dimen.header_view_start_text_size);
}
}
}
Mainactivity.java
public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener{
@BindView(R.id.toolbar_header_view)
protected HeaderView toolbarHeaderView;
@BindView(R.id.float_header_view)
protected HeaderView floatHeaderView;
@BindView(R.id.appbar)
protected AppBarLayout appBarLayout;
@BindView(R.id.toolbar)
protected Toolbar toolbar;
@BindView(R.id.collapsing_toolbar)
protected CollapsingToolbarLayout collapsingToolbarLayout;
private boolean isHideToolbarView = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
initUi();
/*Remove no need to change custom scrim colour */
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.photo);
int color = getDominantColor(icon);
collapsingToolbarLayout.setContentScrimColor(color);
collapsingToolbarLayout.setStatusBarScrimColor(color);
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(darker(color,0.8f));
}
/*Remove no need to change custom scrim colour*/
}
public static int darker (int color, float factor) {
int a = Color.alpha( color );
int r = Color.red( color );
int g = Color.green( color );
int b = Color.blue( color );
return Color.argb( a,
Math.max( (int)(r * factor), 0 ),
Math.max( (int)(g * factor), 0 ),
Math.max( (int)(b * factor), 0 ) );
}
public static int getDominantColor(Bitmap bitmap) {
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true);
final int color = newBitmap.getPixel(0, 0);
newBitmap.recycle();
return color;
}
private void initUi() {
appBarLayout.addOnOffsetChangedListener(this);
toolbarHeaderView.bindTo("Group Name", "Created by Developer");
floatHeaderView.bindTo("Group Name", "Created by Developer");
}
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
int maxScroll = appBarLayout.getTotalScrollRange();
float percentage = (float) Math.abs(verticalOffset) / (float) maxScroll;
if (percentage == 1f && isHideToolbarView) {
toolbarHeaderView.setVisibility(View.VISIBLE);
isHideToolbarView = !isHideToolbarView;
} else if (percentage < 1f && !isHideToolbarView) {
toolbarHeaderView.setVisibility(View.GONE);
isHideToolbarView = !isHideToolbarView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.
}
OUTPUT
Hope this will help you.!
这篇关于展开时 CollapsingToolbarLayout 底部的菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!