I received help a while ago by a hero that helped me make a Toolbar
disappear when an ImageView
was clicked by using a callback function and an interface. I have copied this kind of algorithm for a different page view adapter and it works completely fine in the starting page adapter, but when I switch it, the toolbar doesn't disappear and reappear even though the tapping works. I have tried this for a while, but no luck. Any help will be appreciated.
from adapter1.kt
private lateinit var pageImageCallback: PageImageCallback
fun setPageImageCallback(pageImageCallback: PageImageCallback) {
this.pageImageCallback = pageImageCallback
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val image_layout: View = inflater.inflate(R.layout.view_pager_item, container, false)
val page_image: PhotoView = image_layout.findViewById<View>(R.id.page_image) as PhotoView
Glide.with(image_layout).load(PageList[position].link).placeholder(R.drawable.drip_splash_theme).error(R.drawable.drip_splash_theme).diskCacheStrategy(DiskCacheStrategy.ALL).priority(Priority.HIGH).into(page_image)
page_image.setOnClickListener(View.OnClickListener {
pageImageCallback.onClick()
})
container.addView(image_layout)
return image_layout
}
from adapter2
private lateinit var pageImageCallback: PageImageCallback
fun setPageImageCallback(pageImageCallback: PageImageCallback) {
this.pageImageCallback = pageImageCallback
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val newView: View = LayoutInflater.from(context).inflate(R.layout.vertical_pager_item, null)
val web_image: PhotoView = newView.findViewById<View>(R.id.chapterPage) as PhotoView
Glide.with(context).load(Data[position].link).dontTransform().into(holder.chapter_title)
holder.chapter_title.setOnClickListener(View.OnClickListener {
pageImageCallback.onClick()
})
}
from interface
package com.example.dripk.Interface
interface PageImageCallback {
fun onClick()
}
now for my activity file, it is pretty big but the point of it is that I have four different versions that i display the images. One is horizontal where it displays images in a regular gallery, which has two sub category where you can change the direction of reading from left to right or from right to left and the final way the ImageView
is displayed in vertical and that is throw a reyclerview. But when I switch the views using the my menu settings, it just doesn't respond after the first switch. I have tried implementing a different interface for vertical reading but I had no luck. I feel its due to my lack of understanding of interface and callbacks. But again, I appreciated any help.
from page_activity.kt
class Page_Activity : AppCompatActivity(),
PageImageCallback, PopupMenu.OnMenuItemClickListener {
private var myViewPager: HorizontalAdapter? = null
private var Vertical: VerticalViewAdapter? = null
private var reading_direction: Float? = 180F
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_viewer)
HorizontalView()
}
private fun HorizontalView() {
setContentView(R.layout.activity_Horizontal)
val myrv = findViewById<View>(R.id.right_page) as ViewPager
myViewPager = HorizontalAdapter(this, lstPages)
myrv.rotationY = reading_direction!!
myViewPager!!.setPageImageCallback(this)
myrv.setPageTransformer(false,
ViewPager.PageTransformer { page, position ->
page.rotationY =
reading_direction as Float
})
myrv.adapter = myViewPager
}
private fun VerticalView() {
setContentView(R.layout.activity_Vertical)
val myrv = findViewById<View>(R.id.recycler) as RecyclerView
Vertical = VerticalViewAdapter(this, lstPages)
Vertical !!.setPageImageCallback(this)
myrv.layoutManager = LinearLayoutManager(
this,
LinearLayoutManager.VERTICAL,
false
)
myrv.adapter = Vertical
}
solution
override fun onClick() {
val presenterHorizontal11 = findViewById<Toolbar>(R.id.presenterHorizontal)
val presenter11Horizontal = findViewById<Toolbar>(R.id.presenter1Horizontal)
val presenterVertical22 = findViewById<Toolbar>(R.id.presenterVertical)
val presenter11Vertical = findViewById<Toolbar>(R.id.presenter1Vertical)
if (view == 1) {
if (presenterVertical22.visibility == View.INVISIBLE) {
presenterVertical22.visibility = View.VISIBLE
presenter11Vertical.visibility = View.VISIBLE
} else {
presenterVertical22.visibility = View.INVISIBLE
presenter11Vertical.visibility = View.INVISIBLE
}
}
if (view == 2) {
if (presenterHorizontal11.visibility == View.INVISIBLE) {
println("webtoon visible")
presenterHorizontal11.visibility = View.VISIBLE
presenter11Horizontal.visibility = View.VISIBLE
} else {
presenterHorizontal11.visibility = View.INVISIBLE
presenter11Horizontal.visibility = View.INVISIBLE
}
}
}
override fun onMenuItemClick(p0: MenuItem?): Boolean {
return when (p0?.itemId) {
R.id.item1 -> {
view = 1
MangaView()
true
}
R.id.item2 -> {
view = 2
WebToonView()
true
}
R.id.item3 -> {
true
}
R.id.left -> {
view = 1
reading_direction = 0F
MangaView()
true
}
R.id.right -> {
view = 1
reading_direction = 180F
MangaView()
true
}
else -> false
}
}
from vertical_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".Activity.Page_Activity"
android:id="@+id/web_viewer"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/reader">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
android:id="@+id/presenter"
layout="@layout/presenter">
</include>
<include
android:id="@+id/presenter1"
layout="@layout/presenter1">
</include>
</FrameLayout>
</LinearLayout>
from horizontal_reader
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".Activity.Page_Activity"
android:id="@+id/viewer"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/reader">
<androidx.viewpager.widget.ViewPager
android:layoutDirection="locale"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/right_page">
</androidx.viewpager.widget.ViewPager>
<include
android:id="@+id/presenter"
layout="@layout/presenter">
</include>
<include
android:id="@+id/presenter1"
layout="@layout/presenter1">
</include>
</FrameLayout>
</LinearLayout>
from presenter1.xml
<?xml version="1.0" encoding="utf-8"?>
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#2C2C2C">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<Button
android:layout_width="50dp"
android:id="@+id/to_chapters"
android:clickable="true"
android:layout_height="wrap_content"
android:background="#2C2C2C"
android:drawableTop="@drawable/ic_baseline_arrow_back_24_2"
android:focusable="true"
android:gravity="center_horizontal|top">
</Button>
<Button
android:id="@+id/settings"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_marginLeft="275dp"
android:background="#2C2C2C"
android:drawableTop="@drawable/ic_baseline_settings_24"
android:gravity="center_horizontal|top"
android:onClick="showPopup">
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="25dp"
android:text="Name"
android:id="@+id/name"
android:textColor="#FFFFFF">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="Chapter Name"
android:textColor="#FFFFFF">
</TextView>
</LinearLayout>
</LinearLayout>
</Toolbar>
from presenter.xml
<?xml version="1.0" encoding="utf-8"?>
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#2C2C2C">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="50dp"
android:id="@+id/back"
android:layout_height="wrap_content"
android:layout_marginEnd="278dp"
android:background="#2C2C2C"
android:drawableLeft="@drawable/ic_baseline_arrow_back_24"
android:focusable="true">
</Button>
<Button
android:layout_width="50dp"
android:id="@+id/next"
android:clickable="true"
android:layout_height="wrap_content"
android:background="#2C2C2C"
android:drawableLeft="@drawable/ic_baseline_arrow_forward_24"
android:focusable="true">
</Button>
</LinearLayout>
</Toolbar>
I believe the problems lies in your onClick()
function
override fun onClick() {
if (presenter.visibility == View.INVISIBLE) {
println("clicked")
presenter.visibility = View.VISIBLE
presenter1.visibility = View.VISIBLE
} else {
presenter.visibility = View.INVISIBLE
presenter1.visibility = View.INVISIBLE
}
}
Here you are using Kotlin Android Extension, to access the ID of the layout for presenter
and presenter1
.
You also define exactly the same ID for presenter
and presenter1
in activity_Horizontal
and activity_Vertical
So, in your page_activity
there must be an import like this
import kotlinx.android.synthetic.main.activity_Horizontal.presenter
import kotlinx.android.synthetic.main.activity_Horizontal.presenter1
This is why, when you switch to VerticalView()
, then do a tapping, there is no reaction because it is referencing to your presenter
and presenter1
in activity_Horizontal
.
You can solve this by making a difference for the layout ID like presenterHorizontal
, presenter1Horizontal
, presenterVertical
, and presenter1Vertical
.
Then in your onClick()
function, add another checker like this
override fun onClick() {
if (viewer.visibility == View.VISIBLE) {
if (presenterHorizontal.visibility == View.INVISIBLE) {
println("clicked")
presenterHorizontal.visibility = View.VISIBLE
presenter1Horizontal.visibility = View.VISIBLE
} else {
presenterHorizontal.visibility = View.INVISIBLE
presenter1Horizontal.visibility = View.INVISIBLE
}
} else {
if (presenterVertical.visibility == View.INVISIBLE) {
println("clicked")
presenterVertical.visibility = View.VISIBLE
presenter1Vertical.visibility = View.VISIBLE
} else {
presenterVertical.visibility = View.INVISIBLE
presenter1Vertical.visibility = View.INVISIBLE
}
}
}