Innovation training blog (17) - ListView in Kotlin is highly adaptive and the list of different Item types is shared

Highly adaptive ListView is a very common par...
Highly adaptive
Different lists share ListView

Highly adaptive

ListView is a very common part of Android development, and it is used in all kinds of APP, whether it is QQ's chat list or WeChat's official account list.

In practice, we have encountered such a problem:

Maybe I want the ListView to be nested in the ScrollView, and there are many other components in the ScrollView itself, so as to form a whole.

But in practice, it is found that as long as the ListView is placed in the ScrollView, the height will become only one line, which greatly affects the experience.

Therefore, I decided to write a highly adaptive function by myself.

Get list object

val listAdapter = recentView.adapter

Traversal elements, adding height

for (i in 0 until listAdapter.count) { val listItem = listAdapter.getView(i, null, recentView) listItem.measure(0, 0) totalHeight += listItem.measuredHeight }

LayoutParams settings

val params = recentView.layoutParams params.height = totalHeight + (recentView.dividerHeight * (listAdapter.count - 1)) recentView.layoutParams = params

Effect of automatic measurement

Different lists share ListView

Take the homepage as an example:

In fact, you don't need to assign a Fragment to each tag. When you need to click to a certain point, you can process it again.

But there is such a problem that you cannot set the List directly.

On the one hand, because the element types in each List are different, on the other hand, even for elements of the same type, their click jump may be different.

So, after groping, I came up with a relatively good solution.

Assign a List to each List

var globalList = LinkedList<ArticleItem>() var globalList_2 = LinkedList<ArticleItem>() var globalList_3 = LinkedList<TopItem>() var globalList_4 = LinkedList<TopItem>()

Set Adapter and List elements

// Configure list fun setLikestList(list: LinkedList<TopItem>) { val mAdapter = TopItemAdapter(list, activity) // monitor val onClickListener = object : AdapterView.OnItemClickListener{ override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { val bundle = arguments val token = bundle!!.getString("token").toString() val blogIntent = Intent(activity, BlogActivity::class.java) blogIntent.putExtra("id", list.get(position).id) blogIntent.putExtra("token", token) startActivity(blogIntent) } } val listView = activity!!.findViewById<ListView>(R.id.index_blog_list) listView.adapter = mAdapter listView.setOnItemClickListener(onClickListener) }

Record the location of the current page

var currentPage = 1 var listPage = 1 var lastItem = 0

Judge the list first when switching pages, and refresh only when pulling down

val listener_1 = object : View.OnClickListener { override fun onClick(v: View?) { refreshTabs() tab_1.setTextColor(activity!!.getColor(R.color.bBlue)) currentPage = 1 val swipeView:SwipeRefreshLayout = activity!!.findViewById(R.id.index_swipe_layout) swipeView.isRefreshing = true if (!globalList.isEmpty()) { setSubList(globalList) swipeView.isRefreshing = false } else { fetchSubData() } } }

Judge the current page when pulling down to refresh

val onRefreshListener = SwipeRefreshLayout.OnRefreshListener { // Up to top // Toast.makeText(activity, "refreshing...", Toast.LENGTH_SHORT).show() if(currentPage == 1){ fetchSubData() } else if(currentPage == 2){ globalList_2 = LinkedList<ArticleItem>() listPage = 1 fetchLatestData(listPage) } else if(currentPage == 3){ fetchHotData() } else if(currentPage == 4){ fetchLikestData() } }

Effect achieved

15 June 2020, 23:50 | Views: 7217

Add new comment

For adding a comment, please log in
or create account

0 comments