Насколько я понимаю, вы хотите взять ConstraintLayout
и иметь такое же ConstraintLayout
сокращение, как и RecyclerView
, расположенное под ним, движется вверх и растет при движении RecyclerView
вниз.Примерно так:
Ваша мысль использовать CoordinatorLayout
имеет смысл, и то, что вы пытаетесь достичь, может быть чем-то, что CoordinatorLayout.Behavior
может справиться, но я покажу способ достижения вышеупомянутого с помощью AppBarLayout.OnOffsetChangedListener
.
Крис Ларсон задокументировал подобный метод, который, вероятно, является более надежным подходом.Вы можете найти его Collapsing-Image проект здесь .У него также есть хорошая запись об этом, но я не могу найти это.Если я столкнусь с ним, я опубликую его здесь.
MainActivity.java В основном это стандартный шаблон, за исключением onOffsetChanged()
.
public class MainActivity extends AppCompatActivity
implements AppBarLayout.OnOffsetChangedListener {
private ConstraintLayout mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
mLayout = findViewById(R.id.shrinkingLayout);
AppBarLayout appBar = findViewById(R.id.appbar);
appBar.addOnOffsetChangedListener(this);
setSupportActionBar(toolbar);
List<String> items = new ArrayList<>();
for (int i = 0; i < 50; i++) {
items.add("Item #" + i);
}
RecyclerView recyclerView = findViewById(R.id.recyclerView);
RecyclerViewAdapter mAdapter = new RecyclerViewAdapter(items);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(mAdapter);
}
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
ViewGroup.LayoutParams lp = mLayout.getLayoutParams();
int newHeight = Math.max(mLayout.getMinHeight(), mLayout.getMaxHeight() + verticalOffset);
if (newHeight != lp.height) {
lp.height = newHeight;
mLayout.setLayoutParams(lp);
}
}
private static class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<String> mItems;
RecyclerViewAdapter(List<String> items) {
mItems = items;
}
@Override
public @NonNull
RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_list_item_1, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
ItemViewHolder vh = (ItemViewHolder) holder;
String itemText = mItems.get(position);
vh.mItemTextView.setText(itemText);
int bgColor = (position % 2 == 0)
? android.R.color.holo_blue_light
: android.R.color.holo_green_light;
holder.itemView.setBackgroundColor(
holder.itemView.getContext().getResources().getColor(bgColor));
}
@Override
public int getItemCount() {
return (mItems == null) ? 0 : mItems.size();
}
@Override
public int getItemViewType(int position) {
return TYPE_ITEM;
}
static class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView mItemTextView;
ItemViewHolder(View item) {
super(item);
mItemTextView = item.findViewById(android.R.id.text1);
}
}
private final static int TYPE_ITEM = 1;
}
}
activity_main.xml Кроме того, в основном это стандартный шаблон, кроме FrameLayout
, указанного в CollapsingToolbarLayout
.
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:expanded="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title="Toolbar Collapsing"
app:titleEnabled="false"
app:titleTextColor="@android:color/white">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
app:contentInsetStart="0dp"
app:layout_collapseMode="pin"
app:title="Toolbar"
app:titleTextColor="@android:color/white" />
<!--
The following FrameLayout should remain a static maximum height. It is the ConstraintLayout
that will change in size through program action. The layout "jitters" up and down
if the contents of the CollapsingToolbarLayout is allowed to change size.
The following two lines keeps the FrameLayout stationary so the RecyclerView can
slide over it.
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="1.0"
-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:layout_marginTop="?attr/actionBarSize"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="1.0">
<android.support.constraint.ConstraintLayout
android:id="@+id/shrinkingLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rectangle"
android:maxHeight="256dp"
android:minHeight="80dp">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:padding="8dp"
android:tint="@android:color/holo_green_dark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_little_green_man" />
</android.support.constraint.ConstraintLayout>
</FrameLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>