自定义视图在AlertDialog中显示为空白

时间:2019-02-13 14:36:08

标签: android kotlin android-adapter android-dialogfragment android-dialog

为什么每次尝试将其用于AlertDialog适配器时我的自定义视图都不会出现在RecyclerView中?我创建了必要的适配器和类,但是由于某种原因,我没有看到预期的自定义布局。

预期结果

enter image description here

当前结果

enter image description here

适配器

class MyRVAdapter(private val myList: ArrayList<Facility>) : RecyclerView.Adapter<RVAdapterFacilities.ViewHolder>() {
    override fun getItemCount(): Int {
        return facilitiesList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        // Hide LinearLayout containing TextViews on fragment load
        holder.mLLTextViews.visibility = View.GONE

        val item = facilitiesList[position]

        holder.myButton.setOnClickListener {
            // Initialize a new instance of AlertDialog
            val builder = android.app.AlertDialog.Builder(holder.itemView.context)

            // Set the alert dialog title
            builder.setTitle("Dialog title")

            // Set the alert dialog icon
            builder.setIcon(R.drawable.ic_info_black_24dp)

            // Set the alert dialog view
            builder.setView(R.layout.fragment_dialog)

            // Find TabLayout and ViewPager views
            val tabLayout: TabLayout = holder.itemView.findViewById(R.id.m_dialog_tabs) as TabLayout
            val viewPager: ViewPager = holder.itemView.findViewById(R.id.m_dialog_viewPager) as ViewPager

            // Set classes per tab, set tab names
            val adapter = CustomAdapter(getChildFragmentManager())
            adapter.addFragment("Boy",CustomFragment.createInstance("John"));
            adapter.addFragment("Girl",CustomFragment.createInstance("Stacy"));
            adapter.addFragment("Robot", CustomFragment.createInstance("Aeon"));
            viewPager.adapter = adapter
            tabLayout.setupWithViewPager(viewPager)

            // Set TabLayout text colour
            // Set TabLayout indicator colour
            tabLayout.setTabTextColors(holder.itemView.resources.getColor(R.color.piccadilly, null), holder.itemView.resources.getColor(R.color.grey, null))
            tabLayout.setSelectedTabIndicatorColor(holder.itemView.resources.getColor(R.color.grey, null))


            // Display a positive button on alert dialog
            builder.setPositiveButton(R.string.ok){ dialog, _ -> dialog.dismiss()}

            // Finally, make the alert dialog using 'builder'
            val dialog: android.app.AlertDialog = builder.create()

            // Display the alert dialog on app interface
            dialog.show()
        }
     }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.my_cv, parent, false)
        return ViewHolder(v)
    }

    // button view holder
    class ViewHolder (itemView : View):RecyclerView.ViewHolder(itemView) {
        val myBtn = itemView.findViewById<Button>(R.id.my_btn)!!
    }
}

CustomFragment类

class CustomFragment : androidx.fragment.app.Fragment() {
    private var mText = ""

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val v = inflater.inflate(R.layout.fragment_sample, container, false)
        (v.findViewById(R.id.textView_sample) as TextView).text = mText
        return v
    }

    companion object {
        fun createInstance(txt: String): CustomFragment {
            val fragment = CustomFragment()
            fragment.mText = txt
            return fragment
        }
    }
}

CustomAdapter类

class CustomAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
    private var mFragmentCollection: MutableList<Fragment> = ArrayList()
    private var mTitleCollection: MutableList<String> = ArrayList()
    fun addFragment(title: String, fragment: Fragment) {
        mTitleCollection.add(title)
        mFragmentCollection.add(fragment)
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return mTitleCollection[position]
    }

    override fun getItem(position: Int): Fragment {
        return mFragmentCollection[position]
    }

    override fun getCount(): Int {
        return mFragmentCollection.size
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

我怀疑它是基于使视图膨胀的上下文。尝试稍微修改一下。将您的活动传递到recyclerview并将其实例保存在那里。这完全是出于测试目的。

像这样:

(function() { var myVariable = 10 });

我猜你的问题在这里:

class MyRVAdapter(private val myList: ArrayList<Facility>, val myActivity: Activity) : RecyclerView.Adapter<RVAdapterFacilities.ViewHolder>() {

尝试这样做。

val builder = android.app.AlertDialog.Builder(holder.itemView.context)

如果这对您有用,那么通货膨胀过程就需要活动上下文而不是适配器上下文。

然后由您决定是否要在适配器中保留活动上下文,或者可以在适配器外部处理对话框扩展。

我个人的喜好是对处理单击事件的回调进行回调,该事件超出了项目和/或索引,并允许拥有者进行膨胀。那是我的建议,但是出于测试目的,您可以执行上述操作,然后自己拨打电话。

快乐编码。