Skip to content

Commit

Permalink
use multi-column channel menu
Browse files Browse the repository at this point in the history
  • Loading branch information
pointshd committed May 8, 2020
1 parent 5156567 commit 19642d4
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 20 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "cn.turboshow.iptv.tv"
minSdkVersion 21
targetSdkVersion 29
versionCode 30010
versionName "0.3.1"
versionCode 30020
versionName "0.3.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down
Binary file modified app/src/main/assets/www.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.leanback.widget.OnItemViewClickedListener
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import cn.turboshow.iptv.tv.AppViewModel
import cn.turboshow.iptv.tv.R
import cn.turboshow.iptv.tv.di.activityViewModelProvider
import cn.turboshow.iptv.tv.model.Channel
import dagger.android.support.DaggerDialogFragment
import kotlinx.android.synthetic.main.fragment_channels.*
import javax.inject.Inject
Expand Down Expand Up @@ -37,13 +40,14 @@ class ChannelSelectorFragment : DaggerDialogFragment() {
}

private fun initChannelsView() {
viewModel.channels.observe(this, androidx.lifecycle.Observer {
channelsView.adapter = ArrayAdapter(context!!, android.R.layout.simple_list_item_1, it)
channelsView.setOnItemClickListener { _, _, position, _ ->
viewModel.selectChannel(position)
dismiss()
viewModel.channels.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
channelsView.adapter = ChannelsAdapter(it).apply {
onItemViewClickedListener = OnItemViewClickedListener { _, channel, _, _ ->
viewModel.selectChannel(it.indexOf(channel))
dismiss()
}
}
channelsView.post { channelsView.setSelection(it.indexOf(viewModel.currentChannel.value)) }
channelsView.post { channelsView.scrollToPosition(it.indexOf(viewModel.currentChannel.value)) }
})
}

Expand All @@ -52,3 +56,36 @@ class ChannelSelectorFragment : DaggerDialogFragment() {
fun newInstance() = ChannelSelectorFragment()
}
}

class ChannelsAdapter(private val channels: List<Channel>) :
RecyclerView.Adapter<ChannelsAdapter.ViewHolder>() {
var onItemViewClickedListener: OnItemViewClickedListener? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val itemView =
LayoutInflater.from(parent.context)
.inflate(R.layout.item_channel, parent, false)
return ViewHolder(itemView)
}

override fun getItemCount(): Int {
return channels.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val channel = channels[position]
holder.setTitle(channel.title)
holder.itemView.setOnClickListener {
if (onItemViewClickedListener != null) {
onItemViewClickedListener!!.onItemClicked(null, channel, null, null)
}
}
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun setTitle(title: String) {
(itemView as TextView).text = title
}
}
}

5 changes: 5 additions & 0 deletions app/src/main/res/drawable/bg_channel_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/browse_item_background_focused" android:state_focused="true" /> <!-- focused -->
<item android:drawable="@color/browse_item_background" />
</selector>
25 changes: 14 additions & 11 deletions app/src/main/res/layout/fragment_channels.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/channelsView"
android:layout_width="300dp"
android:layout_height="match_parent"
android:padding="20dp"
android:layout_margin="20dp"
android:background="@drawable/bg_channels_view"
android:listSelector="#66FFFFFF"
android:scrollbars="none"/>
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.leanback.widget.VerticalGridView
android:id="@+id/channelsView"
app:numberOfColumns="4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:background="@drawable/bg_channels_view"
android:listSelector="#66FFFFFF"
android:padding="20dp"
android:scrollbars="none" />
</FrameLayout>
15 changes: 15 additions & 0 deletions app/src/main/res/layout/item_channel.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/titleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:background="@drawable/bg_channel_item"
android:focusable="true"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:textSize="24sp" />
3 changes: 3 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<color name="colorPrimary">#1565c0</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>

<color name="browse_item_background">@android:color/transparent</color>
<color name="browse_item_background_focused">#44FFFFFF</color>
</resources>

0 comments on commit 19642d4

Please sign in to comment.