I want to implement a feature that is sorta like a focus mode, essentially when someone clicks on a image view when looking at an image the tool bar would disappear unless it is reclicked. I believe this allows users to focus more on the image instead of toolbar unless they want information on the image. I have tinkered with this idea and tried to set an onclick feature on the image view, so that once clicked it would turn the visibility on the toolbar to invisible and when its clicked again it would make it visible. The problem is that I can only access the image view in the adapter I set for it and even when I got the alogrithm right (as in I put print statements to see what if statement I enter, and that works successfully) but what happens is that the toolbar doesn't react to it as if I cannot communicate with it.
from adapter
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val page_layout: View = inflater.inflate(R.layout.activity_viewer, container, false)
val presenter: Toolbar = page_layout.findViewById<View>(R.id.presenter) as Toolbar
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
Picasso.get().load(PageList[position].link).into(page_image)
page_image.setOnClickListener(View.OnClickListener {
println("clicked")
println(presenter.visibility)
if (presenter.visibility == View.INVISIBLE) {
println("outside")
presenter.visibility = View.VISIBLE
} else {
println("inside")
presenter.visibility = View.INVISIBLE
}
})
container.addView(image_layout)
return image_layout
}
from onCreate method from activity
class Page_Activity : AppCompatActivity() {
lateinit var lstPages: MutableList<Page>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_viewer)
lstPages = ArrayList()
mangaPages()
val myrv = findViewById<View>(R.id.view_page) as ViewPager
myViewPager = PageViewAdapter(this, lstPages)
myrv.adapter = myViewPager
the foucus on the activity is to make the call so that there is an array full of image link which gets throws into the adapter so that I can display it back in the activity. Nevertheless, I tested with this concept with a framelayout where I attack a onClickListener, but for someone reason when I do it to an image view in an adapter it act different. Anyways any help with this would be much appreciated!!! Thank you!
Your toolbar doesn't behave as you want because you are inflating a new toolbar in your adapter.
In this code
val page_layout: View = inflater.inflate(R.layout.activity_viewer, container, false)
val presenter: Toolbar = page_layout.findViewById<View>(R.id.presenter) as Toolbar
So, in order to access the toolbar that you have inflated in your Page_Activity
, you can implement a callback from your adapter
to your Page_Activity
, like this
First, create a callback
interface PageImageCallback {
fun onClick()
}
Then, create a variable, setter function and call onClick
function in your adapter
class
class Adapter() {
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
Picasso.get().load(PageList[position].link).into(page_image)
page_image.setOnClickListener(View.OnClickListener {
println("clicked")
pageImageCallback.onClick()
})
container.addView(image_layout)
return image_layout
}
}
Lastly, implement the callback in Page_Activity
class Page_Activity : AppCompatActivity(), PageImageCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_viewer)
...
myViewPager.setPageImageCallback(this)
}
...
override fun onClick() {
if (presenter.visibility == View.INVISIBLE) {
println("outside")
presenter.visibility = View.VISIBLE
} else {
println("inside")
presenter.visibility = View.INVISIBLE
}
}
}