首页 > 解决方案 > 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%。我需要实现这样一个视图,它默认显示一些项目和扩展项目的其余部分,我可以与可用的“快速设置”选项相关联。

任何形式的帮助都将是可观的。

提前致谢。

标签: androidanimationsettingsbottomnavigationviewbottom-sheet

解决方案


推荐阅读