android - Android 快速设置,例如使用底页实现
问题描述
我需要实现自定义视图,就像我的项目在通知面板中提供的快速设置一样。我做了一些研发部分,我将在这里分享。因为这个东西要放在屏幕的底部,所以我已经集成了底部视图。在此之前,我将添加需要创建类似自定义视图的视图的屏幕截图。
现在这是要与底板集成,因此在下面添加研发部分并带有底板视图:
MainActivity.java
public void initViews() {
View bottomSheet = findViewById(R.id.bottomSheet);
BottomSheetBehavior mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehavior.setPeekHeight(320);
RecyclerView recyclerView = bottomSheet.findViewById(R.id.bottomSheetRecyclerView);
recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
BottomSheetItemsAdapter bottomSheetItemsAdapter = new BottomSheetItemsAdapter(mContext, getBottomSheetItems());
recyclerView.setAdapter(bottomSheetItemsAdapter);
}
private ArrayList<String> getBottomSheetItems() {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
arrayList.add("F");
arrayList.add("G");
arrayList.add("H");
arrayList.add("I");
return arrayList;
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
</LinearLayout>
</ScrollView>
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="320dp"
android:clipToPadding="true"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<include layout="@layout/layout_bottom_sheet" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
layout_bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:src="@drawable/ic_down_arrow"
tools:ignore="ContentDescription" />
<android.support.v7.widget.RecyclerView
android:id="@+id/bottomSheetRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
</LinearLayout>
BottomSheetItemsAdapter.java
public class BottomSheetItemsAdapter extends RecyclerView.Adapter<BottomSheetItemsAdapter.ViewHolder> {
private Context mContext;
private ArrayList<String> bottomSheetItemsList;
public BottomSheetItemsAdapter(Context mContext, ArrayList<String> bottomSheetItemsList) {
this.mContext = mContext;
this.bottomSheetItemsList = bottomSheetItemsList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = inflater != null ? inflater.inflate(R.layout.list_items_bottom_sheet, parent, false) : null;
return new ViewHolder(convertView);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.txtTitle.setText(bottomSheetItemsList.get(position));
}
@Override
public int getItemCount() {
return bottomSheetItemsList.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView txtTitle;
ViewHolder(View itemView) {
super(itemView);
txtTitle = itemView.findViewById(R.id.txtTitle);
}
@Override
public void onClick(View v) {
}
}
}
通过谷歌搜索各种站点,我没有找到任何关于如何实现自定义或我们自己的快速设置(如视图)的帮助。我已经达到了一定程度的要求,但需要一些帮助才能达到 100%。我需要实现这样一个视图,它默认显示一些项目和扩展项目的其余部分,我可以与可用的“快速设置”选项相关联。
任何形式的帮助都将是可观的。
提前致谢。
解决方案
推荐阅读
- javascript - Vue Keep-Alive - 应用程序标头在路由更改期间不会保持活动状态
- java - 无法初始化类 sun.nio.ch.SocketChannelImpl$DefaultOptionsHolder - Teamcity 重启后无法工作
- python - 如何使用 np.unpackbits 解压 uint32 数组
- pytorch - 每个版本支持的 GPU 硬件的 Pytorch 列表(Ubuntu18.04)
- c# - 是否可以在页面加载时执行 ICommand?
- unix - 在 Unix 中读取单行大文本文件的最快方法是什么?
- c# - 从 C# 程序发送邮件时访问被拒绝
- python - 使用 apikey 请求 JSON 输出时出现 403 Forbidden 错误
- javascript - 无法使用 Builder.load() 动态加载组件
- spring - Spring LdapTemplate org.slf4j.impl.StaticLoggerBinder 问题