我尝试重新绘制使用ViewPager放入TabLayout中的片段时遇到问题,我根据anko数据库中的行以动态方式生成的标签, 在第一次尝试中它可以正常工作,但是如果我退出选项卡视图并再次输入,则ViewPager不会显示该片段,例如:
我使用登录页面进入应用程序:
我填写了字段,应用程序将我发送到第二个片段:
因此,如果我单击名为“ Salir” 的操作按钮栏,则该应用程序将重定向到我的登录页面,如果我再次在我的标签片段中输入,则如下所示:
如果我两次未对“ TablePlanFragment()”实例调用函数“ setZoneViewPager”,则我的代码如下:
/**
* @param pager: The container that displays the Fragments of the tables
* @param zones: The Data that retrieves from the anko database
*/
private fun setZoneViewPager(pager: ViewPager, zones: List<ZonesParser>) {
this.activity.let {
it?.supportFragmentManager.let { fm ->
if (fm != null) {
val pagerAdapter = ViewPagerAdapter(fm)
// Limpíar los tabs y las páginas de las tabs
for (zone in zones) {
val bundle = Bundle()
// Put the necessary resources to create a tables
bundle.putInt("idZone", zone.idZone)
bundle.putString("zone", zone.zone)
val page = TablePlanFragment() // ***************This instance only init in one loop, if the setZoneViewPager is called again the tables doesn't renders!!
page.retainInstance = true
page.arguments = bundle
pagerAdapter.addPage(page, zone.zone)
}
pager.adapter = pagerAdapter
}
}
}
}
那是我的扩展片段的类:
class TablePlanFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.tables_plan_layout, container, false)
loadTablePlanByZone(arguments?.getInt("idZone"), arguments?.getString("zone"), view)
return view
}
/**
* Cargar el plano de mesas dependiendo de la zona que se haya seleccionado
*/
private fun loadTablePlanByZone(idZone: Int?, zone: String?, view: View) {
Log.e("MESA", "ENTRO A loadTablePlanByZone")
if (idZone == null) {
longToast("EL identificador de la zona no puede ser null")
} else {
view.context?.let {
Log.e("MESA", "ENTRO AL CONTEXTO DE LA APP")
val context = it
val tables = mutableListOf<TablesParser>()
it.database.use {
select("tableplan", "*").whereArgs("idzone = {idZone}", "idZone" to idZone).orderBy("description")
.exec {
while (this.moveToNext()) {
tables.add(
TablesParser(
this.getInt(0), // ID mesa
this.getStringOrNull(1), // Color
this.getString(2), // Mesa
this.getInt(3), // Personas
this.getDouble(4), // x
this.getDouble(5), // y
this.getDoubleOrNull(6), // r
this.getInt(7), // ID Zona
this.getString(8), // Zona
this.getStringOrNull(9), // Color Zone
this.getStringOrNull(10), // Imagen zona
this.getString(11), // Status
this.getString(12), // Ocupado
this.getIntOrNull(13), // Relacionado a
this.getStringOrNull(14), // Dividida
this.getStringOrNull(15), // Unida
this.getIntOrNull(16), // Venta relacionada
this.getIntOrNull(17) // Venta Original :D
)
)
}
}
}
/**
* Generación del plano de mesas
*/
if (tables.isNotEmpty()) {
// Definir el sistema del grid del RecyclerView
val gridLayoutManager = GridLayoutManager(context, 3, RecyclerView.VERTICAL, false)
gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int = if (position == tables.size) 4 else 1
}
view.recyclerview_tableplan.layoutManager = gridLayoutManager
val adapter = TableRecyclerViewAdapter(tables)
view.recyclerview_tableplan.adapter = adapter
view.recyclerview_tableplan.setHasFixedSize(true)
} else {
context.longToast("No hay mesas en la zona $zone")
}
} ?: longToast("No se encontró el contexto del plano de mesas")
}
}
}
我做错了什么?希望他能帮上忙,谢谢!