Skip to content

Commit

Permalink
Use item decoration for recycler spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandr7035 committed Jan 4, 2022
1 parent 2bcd966 commit c97484a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.navGraphViewModels
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import by.alexandr7035.gitstat.R
import by.alexandr7035.gitstat.RepositoriesListGraphDirections
Expand Down Expand Up @@ -34,15 +36,28 @@ class ActiveRepositoriesFragment : Fragment(), RepoClickListener {

// Setup adapter
adapter = RepositoriesAdapter(this)
binding!!.recycler.adapter = adapter
binding!!.recycler.layoutManager = LinearLayoutManager(context)
binding?.recycler?.adapter = adapter
val layoutManager = LinearLayoutManager(context)

val decoration = DividerItemDecoration(
binding?.recycler?.context,
layoutManager.orientation
)

ContextCompat.getDrawable(requireContext(), R.drawable.decoration_repository_item)?.let {
decoration.setDrawable(it)
}

binding?.recycler?.addItemDecoration(decoration)
binding?.recycler?.layoutManager = layoutManager


viewModel.getTabRefreshedLiveData().observeNullSafe(viewLifecycleOwner, {
// If current fragment
// FIXME find better solution
if (it == 0) {
//binding!!.root.smoothScrollToPosition(0)
binding!!.recycler.layoutManager!!.scrollToPosition(0)
binding?.recycler?.layoutManager?.scrollToPosition(0)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.navGraphViewModels
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import by.alexandr7035.gitstat.R
import by.alexandr7035.gitstat.RepositoriesListGraphDirections
Expand Down Expand Up @@ -35,8 +37,21 @@ class ArchivedRepositoriesFragment : Fragment(), RepoClickListener {

// Setup adapter
adapter = RepositoriesAdapter(this)
binding!!.recycler.adapter = adapter
binding!!.recycler.layoutManager = LinearLayoutManager(context)
binding?.recycler?.adapter = adapter
val layoutManager = LinearLayoutManager(context)

val decoration = DividerItemDecoration(
binding?.recycler?.context,
layoutManager.orientation
)

ContextCompat.getDrawable(requireContext(), R.drawable.decoration_repository_item)?.let {
decoration.setDrawable(it)
}

binding?.recycler?.addItemDecoration(decoration)
binding?.recycler?.layoutManager = layoutManager


viewModel.getTabRefreshedLiveData().observeNullSafe(viewLifecycleOwner, {
// If current fragment
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/decoration_repository_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:height="@dimen/decoration_repository_item_spacing"/>
<solid android:color="@android:color/transparent"/>
</shape>
1 change: 0 additions & 1 deletion app/src/main/res/layout/view_repository.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:elevation="5dp"
android:layout_marginBottom="16dp"
android:background="@drawable/background_repository_item"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<dimen name="profile_card_field_margin">16dp</dimen>
<dimen name="profile_card_label_margin">8dp</dimen>
<dimen name="profile_fields_vertical_spacing">4dp</dimen>
<dimen name="decoration_repository_item_spacing">16dp</dimen>
</resources>

0 comments on commit c97484a

Please sign in to comment.