这是小米标签,我想做到他这种折叠效果,该怎么实现?十分感谢。
网上的那些CoordinateLayout之类的我都试过了,根本不是一个效果。
这个好像类似,可以参考一下
https://www.cnblogs.com/fdsf/p/15799998.html
首先,要实现折叠效果的Android布局,你可以尝试使用CollapsingToolbarLayout和NestedScrollView。
implementation 'com.google.android.material:material:1.2.0-alpha02'
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 添加其他子视图 -->
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 添加Toolbar和其他折叠内容 -->
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- 添加其他子视图 -->
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
/>
<!-- 添加其他折叠内容 -->
</com.google.android.material.appbar.CollapsingToolbarLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- AppBarLayout内容 -->
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- CollapsingToolbarLayout内容 -->
</com.google.android.material.appbar.CollapsingToolbarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- 主要内容 -->
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
根据以上布局,你可以实现一个具有折叠效果的Android布局。请注意,你可能需要对布局进行一些样式和参数的调整以适应你的需求。
这是一个基本的布局示例,如果你想要实现与示例图片相似的效果,你可能还需要更多的代码和调整。具体的实现方式取决于你的需求和设计。
希望这个解决方案对你有帮助!如果你有任何问题,请随时问我。
可以使用RecyclerView
item_label.xml
<!-- item_label.xml -->
<TextView
android:id="@+id/text_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textColor="#000"
android:textSize="16sp"
android:background="@drawable/bg_label"
android:gravity="center"
android:clickable="true"
android:focusable="true"
android:onClick="onLabelClick"/>
2.一个用于显示所有标签的RecyclerView
<!-- activity_main.xml -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
LabelAdapter
用于管理标签的展开与折叠public class LabelAdapter extends RecyclerView.Adapter<LabelAdapter.LabelViewHolder> {
private List<String> labels;
private Set<Integer> collapsedPositions;
public LabelAdapter(List<String> labels) {
this.labels = labels;
this.collapsedPositions = new HashSet<>();
}
@NonNull
@Override
public LabelViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_label, parent, false);
return new LabelViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull LabelViewHolder holder, int position) {
String label = labels.get(position);
holder.textLabel.setText(label);
holder.itemView.setActivated(collapsedPositions.contains(position));
}
@Override
public int getItemCount() {
return labels.size();
}
public void toggleLabel(int position) {
if (collapsedPositions.contains(position)) {
collapsedPositions.remove(position);
} else {
collapsedPositions.add(position);
}
notifyItemChanged(position);
}
static class LabelViewHolder extends RecyclerView.ViewHolder {
TextView textLabel;
public LabelViewHolder(@NonNull View itemView) {
super(itemView);
textLabel = itemView.findViewById(R.id.text_label);
}
}
}
toggleLabel
方法来实现折叠效果public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private LabelAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
List<String> labels = Arrays.asList("标签1", "标签2", "标签3", "标签4", "标签5");
adapter = new LabelAdapter(labels);
recyclerView.setAdapter(adapter);
}
public void onLabelClick(View view) {
int position = recyclerView.getChildAdapterPosition(view);
adapter.toggleLabel(position);
}
}
监听拖拉动作,然后设置组件移动
效果图 https://github.com/Ramotion/folding-cell-android/raw/master/folding_cell_preview.gif
详细参考 https://github.com/Ramotion/folding-cell-android