Android:折叠线性布局而不是折叠工具栏 [英] Android: Collapsing Linearlayout instead of Collapsing Toolbar
问题描述
我正在尝试在一个片段中创建一个主/明细事务.我想到了将LinearLayout用作标题的edittext容器.然后是RecyclerView以获取详细信息.
I'm trying to create a Master/Detail transaction in a single fragment. I thought of using LinearLayout as the container of my edittext for my header. Then a RecyclerView for details.
如何实现类似于CollapsingToolbar效果的LinearLayout的折叠/展开?
How would one implement the collapsing/expanding of LinearLayout similar to that of the CollapsingToolbar effect?
这是我正在尝试做的屏幕截图.
Here's a screenshot of what I'm trying to do.
到目前为止,我的xml代码.
My xml code so far.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/colorAccent"
android:padding="@dimen/activity_horizontal_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_date_range_black_24dp"
android:tint="@android:color/darker_gray" />
<android.support.v4.widget.Space
android:layout_width="@dimen/activity_horizontal_margin"
android:layout_height="wrap_content" />
<android.support.design.widget.TextInputLayout
android:id="@+id/date_til"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Date">
<android.support.design.widget.TextInputEditText
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:focusable="false"
android:longClickable="false" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_store_black_24dp"
android:tint="@android:color/darker_gray" />
<android.support.v4.widget.Space
android:layout_width="@dimen/activity_horizontal_margin"
android:layout_height="wrap_content" />
<android.support.design.widget.TextInputLayout
android:id="@+id/store_til"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Store">
<android.support.design.widget.TextInputEditText
android:id="@+id/store"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_place_black_24dp"
android:tint="@android:color/darker_gray" />
<android.support.v4.widget.Space
android:layout_width="@dimen/activity_horizontal_margin"
android:layout_height="wrap_content" />
<android.support.design.widget.TextInputLayout
android:id="@+id/location_til"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="@+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Location" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/list_car" />
</LinearLayout>
此外,不确定是否可以使用CollapsingToolbar来完成此操作,因为我在工具栏中仅看到了ImageView崩溃的情况.
Also, not sure if this can be done with the CollapsingToolbar since I've mostly seen only ImageView collapsing in the Toolbar.
感谢任何帮助.
更新:基本上,我想在这里做的是能够在向上滚动时折叠标题视图.然后向下滚动时展开.
UPDATE: Basically what I want to do here is to be able to collapse the header view when scrolling up. Then expand when scrolling down.
推荐答案
您可以使用计时器并逐渐缩小顶部栏的LinearLayout的高度/边距(安东·马约洛夫(Anton Maiorov)中的缺陷已修复这里)
You can use a timer and gradually shrink the height/margin of the topbar's LinearLayout ( flaws in Anton Maiorov's answer is fixed here)
请参见下面的代码段(在设备上进行了测试)
See below snippet (tested on devices)
方法I:安东·迈奥罗夫(Anton Maiorov)的答案,已修复的缺陷,这比下面的第二种实现要简单得多
Approach I: Anton Maiorov's answer, flaws fixed, this is much simpler than the 2nd implementation below
public class MainActivity extends AppCompatActivity {
LinearLayout toolbar;
int mOriginalHeight;
boolean initialSizeObtained = false;
boolean isShrink = false;
Animation _hideAnimation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) toolbar.getLayoutParams();
params.topMargin = -(int) (mOriginalHeight * interpolatedTime);
toolbar.setLayoutParams(params);
}
};
Animation _showAnimation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) toolbar.getLayoutParams();
params.topMargin = (int) (mOriginalHeight * (interpolatedTime - 1));
toolbar.setLayoutParams(params);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (LinearLayout) findViewById(R.id.toolbar);
//Get the original height, which is measured according to WRAP_CONTENT
toolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (initialSizeObtained)
return;
initialSizeObtained = true;
mOriginalHeight = toolbar.getMeasuredHeight();
}
});
_hideAnimation.setDuration(2000);
_showAnimation.setDuration(2000);
}
//Click on the Olimpic image --> Toggles the top toolbar
public void ToggleTopBar(View view) {
isShrink = !isShrink;
toolbar.clearAnimation(); //Important
toolbar.startAnimation(isShrink? _hideAnimation : _showAnimation);
}
}
方法二:我最初的答案是通过更改工具栏的高度,并手动使用计时器,这涉及更多:
Approach II: my original answer by changing the toolbar's height, and also using timer manually, which is more involved:
public class MainActivity extends AppCompatActivity {
LinearLayout toolbar;
int mOriginalHeight;
boolean initialSizeObtained = false;
int currentHeight;
boolean isShrink = false;
Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (LinearLayout) findViewById(R.id.toolbar);
toolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (initialSizeObtained)
return;
initialSizeObtained = true;
mOriginalHeight = toolbar.getMeasuredHeight();
currentHeight = mOriginalHeight;
Log.d("Demo", "Original height is " + mOriginalHeight);
}
});
}
//Click on the Olimpic image --> Toggles the top toolbar
public void ToggleTopBar(View view) {
isShrink = !isShrink;
Resize(isShrink, toolbar, 250);
}
void Resize(final boolean isShrink, final LinearLayout layout, final int minHeight) {
final int H0 = mOriginalHeight;
timer = runTimerAction(10, new Runnable() {
public void run() {
Log.d("demo", "Current Height= " + currentHeight);
if (isShrink && currentHeight > minHeight) {
currentHeight -= 10;
layout.getLayoutParams().height = currentHeight;
refreshToolbar();
} else if (!isShrink && currentHeight < H0) {
currentHeight += 10;
layout.getLayoutParams().height = currentHeight;
refreshToolbar();
} else {
layout.getLayoutParams().height = isShrink ? minHeight : H0;
refreshToolbar();
if (timer != null)
timer.cancel();
}
}
}
);
}
public void refreshToolbar() {
runOnUiThread(new Runnable() {
@Override
public void run() {
toolbar.requestLayout();
}
});
}
这篇关于Android:折叠线性布局而不是折叠工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!