I am trying to display Fragment
but I do not know why I can't do that. Here is my code:
The main_activity.xml
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container" />
</androidx.constraintlayout.widget.ConstraintLayout>
Followed by MainActivity.kt
:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
setContentView(R.layout.main_activity)
supportFragmentManager.beginTransaction().apply {
replace(R.id.fragment_container, CurrencyListFragment())
addToBackStack(null)
commit()
}
}
}
The layout currency_list_fragment.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_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.CurrencyListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Another class CurrencyListFragment.kt
:
class CurrencyListFragment : Fragment(), MainContract.View {
private val restModel: RestModel = RestModel()
lateinit var mainPresenter: MainPresenter
var isLoading: Boolean = false
var apiResponseList: MutableList<ApiResponse> = arrayListOf()
lateinit var itemAdapter: ItemAdapter
var handler: Handler = Handler()
lateinit var _layoutManager: LinearLayoutManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.e("a","a")
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.e("a","a")
return inflater.inflate(R.layout.currency_list_fragment,container,false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.e("a1","a1")
_layoutManager = LinearLayoutManager(activity)
mainPresenter = activity?.let { MainPresenter(this, it.getPreferences(Context.MODE_PRIVATE)) }!!
val currentDate = mainPresenter.convertDate()
mainPresenter.makeACall("2021-07-22")
addScrollerListener()
}
private fun addScrollerListener() {
rvItem.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(_rvItem: RecyclerView, newState: Int) {
super.onScrollStateChanged(_rvItem, newState)
if (!isLoading) {
if (!_rvItem.canScrollVertically(1)) {
loadMore()
isLoading = true
}
}
}
})
}
private fun loadMore() {
//notify adapter using Handler.post() or RecyclerView.post()
handler.post {
apiResponseList.add(ApiResponse("", "", listOf(Currency2("", 0f)), true))
itemAdapter.notifyItemInserted(apiResponseList.size - 1)
}
handler.postDelayed({
apiResponseList.removeAt(apiResponseList.size - 1)
val listSize = apiResponseList.size
itemAdapter.notifyItemRemoved(listSize)
val nextLimit = listSize + 1
for (i in listSize until nextLimit) {
apiResponseList.add(
ApiResponse("", "2020-06-11", listOf(Currency2("a", 2f)), false)
)
}
itemAdapter.notifyDataSetChanged()
isLoading = false
}, 2000)
}
override fun onDestroy() {
super.onDestroy()
restModel.cancelJob()
}
}
Neither of onCreate()
, onCreateView()
nor onViewCreated()
are not called (I suppose that because Logs texts are not shown in Logcat. I guess I added everything you need to solve my problem but if I am wrong and you need something more just ask. Why is that so?
Thank you very much for help!
事实证明,我在扩展 AppCompatActivity 的类上覆盖了错误的 onCreate 方法。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.