简体   繁体   English

单击图像视图时使工具栏消失

[英]make toolbar disappear when image view is clicked

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.我已经修改了这个想法,并尝试在图像视图上设置一个 onclick 功能,这样一旦单击它就会将工具栏上的可见性变为不可见,并且再次单击它会使其可见。 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.问题是我只能访问我为它设置的适配器中的图像视图,即使我得到了正确的算法(如我放置打印语句以查看我输入的 if 语句,并且它成功)但会发生什么工具栏不会对它做出反应,就好像我无法与之交流一样。

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来自活动的 onCreate 方法

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.尽管如此,我用一个框架布局测试了这个概念,我在其中攻击了一个 onClickListener,但是由于某种原因,当我对适配器中的图像视图执行此操作时,它的行为有所不同。 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因此,为了访问您在Page_Activity中膨胀的工具栏,您可以实现从adapterPage_Activity的回调,如下所示

First, create a callback一、创建回调

interface PageImageCallback {
    fun onClick()
}

Then, create a variable, setter function and call onClick function in your adapter class然后,创建一个变量,设置器 function 并调用onClick function 在您的adapter ZA2F2ED4F2F8EBC2ABCB4D4C2

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最后,在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
        }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM