在弹出的 Android Studio 中添加视图寻呼机
Posted
技术标签:
【中文标题】在弹出的 Android Studio 中添加视图寻呼机【英文标题】:Add view pager in popup Android Studio 【发布时间】:2021-07-19 23:22:51 【问题描述】:我正在使用 android Studio,我想在弹出窗口中添加一个视图寻呼机。我可以得到一些关于如何做到这一点的提示吗?我想在按下按钮时出现的弹出窗口中插入一个可滑动的页面,但我不知道该怎么做。
这是 ViewPagerAdapter 的代码:
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
class ViewPagerAdapter(supportFragmentManager: FragmentManager) : FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
private val mFragmentList = ArrayList<Fragment>()
private val mFragmentTitleList = ArrayList<String>()
override fun getCount(position: Int): Fragment
return mFragmentList[position]
override fun getItem(): Int
return mFragmentList.size
override fun getPageTitle(position: Int): CharSequence?
return mFragmentTitleList[position]
fun addFragment(fragment: Fragment , title: String)
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
这是片段的代码:
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.drawerlayout.widget.DrawerLayout
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import it.gaonaaragamba.kcalrecipes.R.*
import it.gaonaaragamba.kcalrecipes.ViewPagerAdapter.ViewPagerAdapter
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
lateinit var viewPager: ViewPager
lateinit var tabs: TabLayout
class Principal_fragment :
Fragment()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
viewPager = getActivity().findViewById(R.id.viewPager)
setUpTabs()
private fun setUpTabs()
tabs = tabs
val adapter = ViewPagerAdapter(supportFragmentManager = )
adapter.addFragment(BlankFragment(), "Ricetta")
adapter.addFragment(IngredientFragment(), "Ingrediente")
val viewPager =
viewPager.adapter = adapter
如果有人可以帮助我并给我一些建议,我将非常感激。
【问题讨论】:
您想添加具有 corozol 滑动或正常滑动的 viewpager?,如果您使用 viewpager 2,您可以添加 corozol 选项,否则您必须自定义实现滑动选项 【参考方案1】:我已经在 Activity 中完成了。检查这个..!!
**Open Dialog on Button Click**
val dialog = Dialog(requireActivity(), your_theme)
val binding = DialogBinding.inflate(layoutInflater)
dialog.apply
requestWindowFeature(Window.FEATURE_NO_TITLE)
setCancelable(false)
setContentView(binding.root)
//set your viewpager here
binding.viewpager2.adpter = mAdapter
try
show()
catch (e: Exception)
e.printStackTrace()
**Dialog layout with viewpager2 (dialog.xml)**
<?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_
android:layout_>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager2"
android:layout_
android:layout_
android:background="@drawable/top_blue_corner_gradient_bg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
**your Viewpager Adapter**
class PagerAdapter(
private val list: List<String>,
private val context: Context
) : RecyclerView.Adapter<PagerAdapter.Holder>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder
return Holder(
ItemPagerBinding.inflate(LayoutInflater.from(parent.context), parent, false)
)
override fun getItemCount(): Int
return list.size
override fun onBindViewHolder(holder: PlansViewHolder, position: Int)
holder.bind(list[position])
inner class Holder(private val binding: ItemPagerBinding) :
RecyclerView.ViewHolder(binding.root)
fun bind(youtText: String)
binding.txtHeader.text = youtText
**Viewpager item layout (item_pager.xml)**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtHeader"
android:layout_
android:layout_
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:gravity="center"
android:lineSpacingExtra="5dp"
android:padding="10dp"
android:textSize="15sp"/>
</LinearLayout>
**intialize adapter like this**
private val mAdapter by lazy
PagerAdapter(
listOf(
requireContext().getString(R.string.string1),
requireContext().getString(R.string.string2),
requireContext().getString(R.string.string3),
requireContext().getString(R.string.string4),
requireContext().getString(R.string.string5)
), requireContext()
)
【讨论】:
以上是关于在弹出的 Android Studio 中添加视图寻呼机的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio中如何使用Materal插件生成图标