Skip to content

Commit

Permalink
feat: implement edge-to-edge support
Browse files Browse the repository at this point in the history
- Update layout to use CoordinatorLayout and ConstraintLayout- Add toolbar and implement proper window insets handling
- Enable edge-to-edge support for immersive UI experience
- Refactor MainActivity to use new UI components
- Update themes to use NoActionBar parent for consistency
  • Loading branch information
dingyi222666 committed Nov 28, 2024
1 parent 397bfda commit 9f35c78
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 23 deletions.
32 changes: 21 additions & 11 deletions app/src/main/kotlin/com/dingyi/treeview/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.activity.enableEdgeToEdge
import com.dingyi.treeview.databinding.ActivityMainBinding
import com.dingyi.treeview.databinding.ItemDirBinding
import com.dingyi.treeview.databinding.ItemFileBinding
Expand Down Expand Up @@ -48,10 +52,19 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
enableEdgeToEdge()

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

setSupportActionBar(binding.toolbar)

ViewCompat.setOnApplyWindowInsetsListener(binding.treeview) { view, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
view.updatePadding(bottom = insets.bottom)
windowInsets
}

val tree = createTree()
this.tree = tree

Expand Down Expand Up @@ -79,22 +92,23 @@ class MainActivity : AppCompatActivity() {
}

override fun onPrepareOptionsMenu(menu: Menu): Boolean {
println("prepare menu $menu")
menu.findItem(R.id.drag_node).apply {
isChecked = binding.treeview.supportDragging
isEnabled =
binding.treeview.selectionMode != TreeView.SelectionMode.MULTIPLE_WITH_CHILDREN
binding.treeview.selectionMode != TreeView.SelectionMode.NONE
}
menu.findItem(R.id.select_mode).apply {
isChecked =
binding.treeview.selectionMode == TreeView.SelectionMode.MULTIPLE_WITH_CHILDREN
binding.treeview.selectionMode != TreeView.SelectionMode.NONE
isEnabled = !binding.treeview.supportDragging
}
menu.findItem(R.id.slow_mode).apply {
isChecked = isSlow
}
menu.findItem(R.id.selected_group).apply {
isEnabled =
binding.treeview.selectionMode == TreeView.SelectionMode.MULTIPLE_WITH_CHILDREN
binding.treeview.selectionMode != TreeView.SelectionMode.NONE
}
return super.onPrepareOptionsMenu(menu)
}
Expand Down Expand Up @@ -306,7 +320,7 @@ class MainActivity : AppCompatActivity() {

applyDepth(holder, node)

(getCheckableView(node, holder) as MaterialCheckBox).apply {
getCheckableView(node, holder).apply {
isVisible = node.selected
isSelected = node.selected
}
Expand Down Expand Up @@ -342,7 +356,7 @@ class MainActivity : AppCompatActivity() {
override fun getCheckableView(
node: TreeNode<DataSource<String>>,
holder: TreeView.ViewHolder
): Checkable {
): MaterialCheckBox {
return if (node.isChild) {
ItemDirBinding.bind(holder.itemView).checkbox
} else {
Expand All @@ -351,11 +365,7 @@ class MainActivity : AppCompatActivity() {
}

override fun onClick(node: TreeNode<DataSource<String>>, holder: TreeView.ViewHolder) {
if (node.isChild) {
applyDir(holder, node)
} else {
Toast.makeText(this@MainActivity, "Clicked ${node.name}", Toast.LENGTH_LONG).show()
}
Toast.makeText(this@MainActivity, "Clicked ${node.name}", Toast.LENGTH_LONG).show()
}

override fun onMoveView(
Expand Down
46 changes: 35 additions & 11 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<ProgressBar
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/progress"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:indeterminate="true" />
android:fitsSystemWindows="true">

<io.github.dingyi222666.view.treeview.TreeView
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways|snap" />

</com.google.android.material.appbar.AppBarLayout>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/treeview"
/>
android:animateLayoutChanges="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<ProgressBar
android:id="@+id/progress"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
app:layout_constraintTop_toTopOf="parent" />

<io.github.dingyi222666.view.treeview.TreeView
android:id="@+id/treeview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/progress" />

</androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.TreeView" parent="Theme.Material3.DynamicColors.Light">
<style name="Theme.TreeView" parent="Theme.Material3.DynamicColors.Light.NoActionBar">
<!-- Primary brand color. -->
<!-- <item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
Expand Down

0 comments on commit 9f35c78

Please sign in to comment.