Handle recyclerview touches and add single app activity

This commit is contained in:
2020-05-17 06:00:18 +03:00
parent 6baa26648f
commit 50957abeca
5 changed files with 142 additions and 1 deletions
+3
View File
@@ -18,6 +18,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.SingleAppActivity" android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity">
</activity>
<activity android:name=".activities.AppsListActivity" />
</application>
@@ -0,0 +1,44 @@
package com.illusionware.npsbrowser
import android.content.Context
import android.view.GestureDetector
import android.view.GestureDetector.SimpleOnGestureListener
import android.view.MotionEvent
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.OnItemTouchListener
class RecyclerTouchListener(context: Context?, recyclerView: RecyclerView,
private val clickListener: ClickListener?) : OnItemTouchListener {
interface ClickListener {
fun onClick(view: View?, position: Int)
fun onLongClick(view: View?, recyclerView: RecyclerView?, position: Int)
}
private val gestureDetector: GestureDetector
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
val child = rv.findChildViewUnder(e.x, e.y)
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildAdapterPosition(child))
}
return false
}
override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}
override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
init {
gestureDetector = GestureDetector(context, object : SimpleOnGestureListener() {
override fun onSingleTapUp(e: MotionEvent): Boolean {
return true
}
override fun onLongPress(e: MotionEvent) {
val child = recyclerView.findChildViewUnder(e.x, e.y)
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView, recyclerView.getChildAdapterPosition(child))
}
}
})
}
}
@@ -5,7 +5,6 @@ import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.illusionware.npsbrowser.AppData
@@ -31,6 +30,20 @@ class AppsListActivity : AppCompatActivity() {
recyclerView.apply {
layoutManager = LinearLayoutManager(applicationContext)
adapter = viewAdapter
recyclerView.addOnItemTouchListener(RecyclerTouchListener(applicationContext, recyclerView, object : RecyclerTouchListener.ClickListener {
override fun onClick(view: View?, position: Int) {
val myIntent = Intent(context, SingleAppActivity::class.java)
SingleAppActivity.app = apps[position]
startActivity(myIntent)
}
override fun onLongClick(view: View?, recyclerView: RecyclerView?, position: Int) {
Toast.makeText(applicationContext, "TODO: Long Click ", Toast.LENGTH_SHORT).show()
}
}
))
}
}
}
@@ -0,0 +1,20 @@
package com.illusionware.npsbrowser.activities
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.illusionware.npsbrowser.AppData
import com.illusionware.npsbrowser.databinding.ActivitySingleGameBinding
class SingleAppActivity: AppCompatActivity() {
companion object {
lateinit var app : AppData
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivitySingleGameBinding.inflate(layoutInflater);
setContentView(binding.root)
binding.titleID.text = app.TitleID
binding.gameTitle.text = app.Title;
}
}
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:id="@+id/gameTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="48dp"
android:fontFamily="sans-serif-medium"
android:text="gameTitle"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/titleID" />
<TextView
android:id="@+id/titleID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginTop="56dp"
android:layout_marginEnd="48dp"
android:fontFamily="sans-serif-smallcaps"
android:text="titleID"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/gameIcon"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginStart="140dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="141dp"
android:contentDescription="Game Icon"
app:srcCompat="@drawable/ic_games_black_24dp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>