Android Application

Kotlin 고급 위젯 - 그 밖의 뷰 컨테이너(View Container) : 슬라이딩드로어(SlidingDrawer)

작성자 임베디드코리아 작성일23-09-24 03:13 조회1,595회 댓글0건

첨부파일

------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="여기는 서랍밖입니다" />

    <SlidingDrawer
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:content="@+id/content"
        android:handle="@+id/handle"
        android:orientation="vertical">

        <Button
            android:id="@+id/handle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="서랍 손잡이" />

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00FF00"
            android:gravity="center">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="여기는 서랍안입니다" />

        </LinearLayout>

    </SlidingDrawer>

</LinearLayout>

*************************************************************************

------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">

    <SlidingDrawer
        android:id="@+id/simpleSlidingDrawer1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:content="@+id/content1"
        android:handle="@+id/handle1"
        android:orientation="horizontal"
        android:rotation="180">

        <!-- Let us define the button -->
        <Button
            android:id="@id/handle1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0f0"
            android:rotation="270"
            android:text="Open"
            android:textColor="#fff" />

        <!-- layout for the content of the SlidingDrawer -->
        <LinearLayout
            android:id="@id/content1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:rotation="180">

            <!-- Define all your current, Widgets here which
                you want to add in Sliding Drawer Layout -->
            <ListView
                android:id="@+id/simpleListView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </LinearLayout>
    </SlidingDrawer>

</LinearLayout>

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

    <!-- TextView for the list item -->
    <TextView
        android:id="@+id/name1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="#0022FF" />

</LinearLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    var nameArray = arrayOf("Bitcoin", "Ethereum", "Litecoin", "IOTA", "Libra", "Monero",
        "EOS", "NEO", "ATOM", "Tether", "XRP", "Bitcoin Cash", "Binance Coin",
        "REN", "Bitcoin SV", "USD Coin", "Stellar", "Tezos", "Dash", "Zcash")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // initiate the SlidingDrawer
        val simpleSlidingDrawer1 = findViewById<SlidingDrawer>(R.id.simpleSlidingDrawer1)

        // initiate a Button which is used for handling the content of SlidingDrawer
        // We can able to have open and close methods for this handleButton
        val handleButton = findViewById<Button>(R.id.handle1)

        // initiate the ListView that is used for content of SlidingDrawer adapter
        // for the list view. As all are text, it is defined as ArrayAdapter<String>
        // Your cryptocurrency items are going to be displayed via this view using
        // below cryptocurrency ArrayAdapter
        val simpleListView1 = findViewById<ListView>(R.id.simpleListView1)

        // Below syntax is for defining ArrayAdapter
        val cryptocurrencyArrayAdapter = ArrayAdapter(applicationContext, R.layout.list_item, R.id.name1, nameArray)

        // set an adapter to fill the data in the ListView
        simpleListView1.adapter = cryptocurrencyArrayAdapter

        // implement setOnDrawerOpenListener event, name itself suggests that
        // we need to write code for drawer opening
        simpleSlidingDrawer1.setOnDrawerOpenListener {
            // When drawer is opened, we may need to indicate user that close option is available,
            // so just setting text to close. But required functionality can be done here
            handleButton.text = "Close"
        }

        // implement setOnDrawerCloseListener event, name itself suggests we need to write close events
        simpleSlidingDrawer1.setOnDrawerCloseListener {
            // if close is done, we should have the option to open. according
            // to the requirement, carry out necessary steps for close
            handleButton.text = "Open"
        }
    }
}

uild.gradle (app) 에 라이브러리 추가하기
DrawerLayout은 Support Library v4에 포함된 클래스이므로 build.gradle (Module: app)에 Support Library를 추가한다.

android {
          :
  dependencies {
          :
    implementation 'androidx.legacy:legacy-support-v4:1.0.0' // DrawerLayout
    implementation 'com.google.android.material:material:1.0.0' // NavigationView
          :
  }
          :
}