-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LH-112] Add ViewPage fragment for different category #16
- Loading branch information
1 parent
eaa365c
commit bfe5bcb
Showing
5 changed files
with
123 additions
and
8 deletions.
There are no files selected for viewing
63 changes: 59 additions & 4 deletions
63
feature/board/src/main/java/com/lighthouse/board/BoardFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,73 @@ | ||
package com.lighthouse.board | ||
|
||
import android.os.Bundle | ||
import androidx.fragment.app.Fragment | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ArrayAdapter | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.viewpager2.adapter.FragmentStateAdapter | ||
import com.google.android.material.tabs.TabLayoutMediator | ||
import com.lighthouse.board.databinding.FragmentBoardBinding | ||
import com.lighthouse.board.view.TabContentFragment | ||
import com.lighthouse.board.viewmodel.BoardViewModel | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
@AndroidEntryPoint | ||
class BoardFragment : Fragment() { | ||
private val viewModel: BoardViewModel by viewModels() | ||
private lateinit var binding: FragmentBoardBinding | ||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View? { | ||
// Inflate the layout for this fragment | ||
return inflater.inflate(R.layout.fragment_board, container, false) | ||
): View { | ||
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_board, container, false) | ||
|
||
initSpinner() | ||
initTab() | ||
|
||
return binding.root | ||
} | ||
|
||
private fun initSpinner() { | ||
val arrayList = arrayListOf( | ||
resources.getString(com.lighthouse.android.common_ui.R.string.sort_latest), | ||
resources.getString(com.lighthouse.android.common_ui.R.string.sort_top_rated) | ||
) | ||
val arrayAdapter = ArrayAdapter( | ||
requireContext(), | ||
R.layout.spinner_item, | ||
arrayList | ||
).apply { | ||
setDropDownViewResource(com.google.android.material.R.layout.support_simple_spinner_dropdown_item) | ||
} | ||
|
||
binding.spinnerSort.adapter = arrayAdapter | ||
} | ||
|
||
private fun initTab() { | ||
binding.tabBoard | ||
binding.vpBoard.adapter = TabViewPagerAdapter(this) | ||
TabLayoutMediator(binding.tabBoard, binding.vpBoard) { tab, position -> | ||
tab.text = resources.getStringArray(R.array.tab_name)[position] | ||
}.attach() | ||
} | ||
|
||
inner class TabViewPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) { | ||
override fun getItemCount(): Int = resources.getStringArray(R.array.tab_name).size | ||
|
||
override fun createFragment(position: Int): Fragment { | ||
val fragment = TabContentFragment() | ||
|
||
fragment.arguments = Bundle().apply { | ||
putInt("tab_pos", position) | ||
putString("order", binding.spinnerSort.selectedItem.toString()) | ||
} | ||
return fragment | ||
} | ||
} | ||
|
||
|
||
} |
31 changes: 31 additions & 0 deletions
31
feature/board/src/main/java/com/lighthouse/board/view/TabContentFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.lighthouse.board.view | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.Toast | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.fragment.app.Fragment | ||
import com.lighthouse.board.R | ||
import com.lighthouse.board.databinding.FragmentTabContentBinding | ||
|
||
class TabContentFragment : Fragment() { | ||
private lateinit var binding: FragmentTabContentBinding | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View { | ||
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_tab_content, container, false) | ||
|
||
val content = arguments?.getInt("tab_pos") ?: "Default Content" | ||
|
||
binding.srBoard.setOnRefreshListener { | ||
Toast.makeText(context, "Hello!", Toast.LENGTH_SHORT).show() | ||
binding.srBoard.isRefreshing = false | ||
} | ||
|
||
return binding.root | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
feature/board/src/main/res/layout/fragment_tab_content.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout 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"> | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".view.TabContentFragment"> | ||
|
||
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout | ||
android:id="@+id/sr_board" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:layout_marginBottom="50dp" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent"> | ||
|
||
<androidx.recyclerview.widget.RecyclerView | ||
android:id="@+id/rv_question" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:itemCount="3" | ||
tools:listitem="@layout/question_tile" /> | ||
|
||
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters