Divider line in dialog

2019-05-23 04:15发布

I noticed something very useful in Google apps.

When we have dialog with big content, divider line is showing in order to emphasize ability to scroll, like in this two pictures:

firstsecond

But, I have no idea how to implement such behaviour using known utils.

Best example is while choosing phone ring, at the start there is only divider on the bottom, but when we start scrolling two dividers appear.

enter image description here

QUESTION

How to implement appearing and disappearing behaviour in dialog?

3条回答
Melony?
2楼-- · 2019-05-23 04:25

Looking at Android source code the solution is really simple. Just add this code to your custom view:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
   final int indicators = (hasTopPanel() ? View.SCROLL_INDICATOR_TOP : 0)
              | (hasBottomPanel() ? View.SCROLL_INDICATOR_BOTTOM : 0);
   view.setScrollIndicators(indicators, View.SCROLL_INDICATOR_TOP | View.SCROLL_INDICATOR_BOTTOM);
}
查看更多
一纸荒年 Trace。
3楼-- · 2019-05-23 04:33

The behaviour that you want to achieve comes with a Dialog with Scrollable Content. Please read this document to get an idea of that.

In this case, you need to create your own custom layout xml with ListView and couple of buttons. And you need to keep this entire layout under ScrollView.

The following is just a dummy code to help you with:

Edited the dummy code with the actual one::

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ListView"/>

</LinearLayout>

MainActivity.class:

 LayoutInflater inflater= LayoutInflater.from(this);
    view=inflater.inflate(R.layout.layout, null);
    ListView listView = (ListView)view.findViewById(R.id.ListView);
    String[] items = { "Milk", "Butter", "Yogurt", "Toothpaste", "Ice Cream" };
    ArrayAdapter adapter_dest = new ArrayAdapter(this,android.R.layout.simple_list_item_1,items);
    listView.setAdapter(adapter_dest);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Custom Dialog")
            .setCancelable(false)
            .setView(view)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();

Hope this helps!

查看更多
forever°为你锁心
4楼-- · 2019-05-23 04:41

If you have custom layout like this

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll_container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <LinearLayout
        android:id="@+id/views_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
</ScrollView>

you can inflate View and find ScrollView by

v.findViewById(R.id.scroll_container)

and set scroll TOP and BOTTOM indicators if you have TITLE above and BUTTONS below your custom view

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        final int indicators = View.SCROLL_INDICATOR_TOP | View.SCROLL_INDICATOR_BOTTOM;
        scrollView.setScrollIndicators(indicators, View.SCROLL_INDICATOR_TOP | View.SCROLL_INDICATOR_BOTTOM);
}
查看更多
登录 后发表回答