【发布时间】:2018-11-30 09:35:21
【问题描述】:
活动
class NewPaymentActivity : AppCompatActivity(),SummaryFragment.OnFragmentInteractionListener,ReceiptDetailsFragment.OnFragmentInteractionListener{
internal lateinit var viewpageradapter: ViewPagerAdapter //Declare PagerAdapter
private var tabLayout: TabLayout? = null
var viewPager: ViewPager? = null
var toolbar: Toolbar? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new_payment)
viewpageradapter= ViewPagerAdapter(supportFragmentManager)
viewPager = findViewById(R.id.viewPager) as ViewPager
setupViewPager(viewPager!!)
tabLayout = findViewById(R.id.tab_layout) as TabLayout
tabLayout!!.setupWithViewPager(viewPager)
// tabLayout!!.setSelectedTabIndicatorColor(getResources().getColor(R.color.t));
tabLayout!!.setSelectedTabIndicatorHeight(0)
val headerView = (getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater)
.inflate(R.layout.custom_tabs_for_payments, null, false)
val receiptDetails = headerView.findViewById(R.id.tb_receipt_details) as TextView
val paymentDetails = headerView.findViewById(R.id.tb_payment_details) as TextView
val invoiceDetails = headerView.findViewById(R.id.tb_invoice_details) as TextView
val summary = headerView.findViewById(R.id.tb_summary) as TextView
receiptDetails.setTextColor(resources.getColor(R.color.white))
val drawable = receiptDetails.getBackground() as GradientDrawable
drawable.setColor(resources.getColor(R.color.received_payments))
paymentDetails.setTextColor(resources.getColor(R.color.main))
val drawablepaymentDetails = paymentDetails.getBackground() as GradientDrawable
drawablepaymentDetails.setColor(resources.getColor(R.color.white))
invoiceDetails.setTextColor(resources.getColor(R.color.main))
val drawableinvoiceDetails = invoiceDetails.getBackground() as GradientDrawable
drawableinvoiceDetails.setColor(resources.getColor(R.color.white))
summary.setTextColor(resources.getColor(R.color.main))
val summarydrawable = summary.getBackground() as GradientDrawable
summarydrawable.setColor(resources.getColor(R.color.white))
tabLayout!!.getTabAt(0)!!.setCustomView(receiptDetails)
tabLayout!!.getTabAt(1)!!.setCustomView(paymentDetails)
tabLayout!!.getTabAt(2)!!.setCustomView(invoiceDetails)
tabLayout!!.getTabAt(3)!!.setCustomView(summary)
tabLayout!!.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
if(tab!!.position==0)
{
receiptDetails.setTextColor(resources.getColor(R.color.white))
val drawable = receiptDetails.getBackground() as GradientDrawable
drawable.setColor(resources.getColor(R.color.received_payments))
paymentDetails.setTextColor(resources.getColor(R.color.main))
val drawablepaymentDetails = paymentDetails.getBackground() as GradientDrawable
drawablepaymentDetails.setColor(resources.getColor(R.color.white))
invoiceDetails.setTextColor(resources.getColor(R.color.main))
val drawableinvoiceDetails = invoiceDetails.getBackground() as GradientDrawable
drawableinvoiceDetails.setColor(resources.getColor(R.color.white))
summary.setTextColor(resources.getColor(R.color.main))
val summarydrawable = summary.getBackground() as GradientDrawable
summarydrawable.setColor(resources.getColor(R.color.white))
}
else if(tab!!.position==1)
{
paymentDetails.setTextColor(resources.getColor(R.color.white))
val drawable = paymentDetails.getBackground() as GradientDrawable
drawable.setColor(resources.getColor(R.color.received_payments))
receiptDetails.setTextColor(resources.getColor(R.color.main))
val drawablereceiptDetails= receiptDetails.getBackground() as GradientDrawable
drawablereceiptDetails.setColor(resources.getColor(R.color.white))
invoiceDetails.setTextColor(resources.getColor(R.color.main))
val drawableinvoiceDetails = invoiceDetails.getBackground() as GradientDrawable
drawableinvoiceDetails.setColor(resources.getColor(R.color.white))
summary.setTextColor(resources.getColor(R.color.main))
val summarydrawable = summary.getBackground() as GradientDrawable
summarydrawable.setColor(resources.getColor(R.color.white))
}
else if(tab!!.position==2)
{
invoiceDetails.setTextColor(resources.getColor(R.color.white))
val drawable = invoiceDetails.getBackground() as GradientDrawable
drawable.setColor(resources.getColor(R.color.received_payments))
receiptDetails.setTextColor(resources.getColor(R.color.main))
val drawablereceiptDetails= receiptDetails.getBackground() as GradientDrawable
drawablereceiptDetails.setColor(resources.getColor(R.color.white))
paymentDetails.setTextColor(resources.getColor(R.color.main))
val drawablepaymentDetails = paymentDetails.getBackground() as GradientDrawable
drawablepaymentDetails.setColor(resources.getColor(R.color.white))
summary.setTextColor(resources.getColor(R.color.main))
val summarydrawable = summary.getBackground() as GradientDrawable
summarydrawable.setColor(resources.getColor(R.color.white))
}
else if(tab!!.position==3)
{
summary.setTextColor(resources.getColor(R.color.white))
val drawable = summary.getBackground() as GradientDrawable
drawable.setColor(resources.getColor(R.color.received_payments))
receiptDetails.setTextColor(resources.getColor(R.color.main))
val drawablereceiptDetails = receiptDetails.getBackground() as GradientDrawable
drawablereceiptDetails.setColor(resources.getColor(R.color.white))
paymentDetails.setTextColor(resources.getColor(R.color.main))
val drawablepaymentDetails = paymentDetails.getBackground() as GradientDrawable
drawablepaymentDetails.setColor(resources.getColor(R.color.white))
invoiceDetails.setTextColor(resources.getColor(R.color.main))
val drawableinvoiceDetails = invoiceDetails.getBackground() as GradientDrawable
drawableinvoiceDetails.setColor(resources.getColor(R.color.white))
}
else {
}
}
override fun onTabUnselected(tab:TabLayout.Tab?){
}
override fun onTabReselected(tab:TabLayout.Tab?) {
}
})
}
private fun setupViewPager(viewPager: ViewPager) {
val adapter = ViewPagerAdapter(supportFragmentManager)
adapter.addFragment(ReceiptDetailsFragment(), "Receipt Details")
adapter.addFragment(PaymentDetailsFragment(), "Payment Details")
adapter.addFragment(InvoiceDetailsFragment(), "Invoice Details")
adapter.addFragment(SummaryFragment(), "Summary")
viewPager.adapter = adapter
}
internal inner class ViewPagerAdapter(manager: FragmentManager) : FragmentStatePagerAdapter(manager) {
private val mFragmentList = ArrayList<Fragment>()
private val mFragmentTitleList = ArrayList<String>()
override fun getItem(position: Int): Fragment {
return mFragmentList[position]
}
override fun getCount(): Int {
return mFragmentList.size
}
fun addFragment(fragment: Fragment, title: String) {
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
}
override fun getPageTitle(position: Int): CharSequence {
return mFragmentTitleList[position]
}
}
override fun onFragmentInteraction(jsonObject: JSONObject) {
//session intialization
val myPreference = Session(this)
myPreference.setJsonObject(jsonObject.toString())
viewPager!!.setCurrentItem(1,true)
}
override fun onFragmentInteraction(uri: Uri) {
}
}
片段
class PaymentDetailsFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_payment_details, container, false)
val replacingLayout = rootView.findViewById(R.id.replacing_layout) as LinearLayout
val viewPaymentCheque = activity!!.layoutInflater.inflate(R.layout.payment_details_cheques, null)
Log.e("test Object","test")
replacingLayout.addView(viewPaymentCheque)
val myPreference = Session(activity!!)
Log.e("test Object", myPreference.getJsonObject())
if(myPreference.getJsonObject()!="") {
var obj = JSONObject(myPreference.getJsonObject());
Log.e("test Object", obj.toString())
}
return rootView
}
}
我正在使用 android ViewPager 并在单击单独的选项卡时用于加载不同的片段。但问题是当我点击标签片段时没有加载。当加载所有片段的活动被加载时,片段不会改变。我该如何解决这个问题?当我在视图分页器中选择选项卡时需要加载片段。
【问题讨论】:
-
您的
onFragmentInteraction是做什么的?看起来它是唯一一个调用setCurrentItem并始终设置为第二个片段的地方? -
它用于一旦我单击该特定片段上的按钮,它将通过界面调用该方法。即使我单击标签片段也不会调用。仅在第一次加载所有片段时。
-
对,那么你应该尝试在
onTabSelected()中添加viewpager.setCurrentItem(position)。 -
已添加但未加载片段 ViewPagerAdapter.getItem 方法也调用但片段未运行。
-
当你说它没有加载时,你的意思是你可以滑动但它只是空的?
标签: android kotlin android-viewpager