1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipe"
>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollview_ID"
android:descendantFocusability="blocksDescendants"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<SearchView
android:layout_width="match_parent"
android:layout_height="35dp"
android:id="@+id/search_ID"
android:imeOptions="actionSearch"
android:singleLine="true"
android:queryHint="搜索"
android:background="@drawable/search_background"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
</SearchView>
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_below="@id/search_ID"
android:background="#E3E0E0"
android:id="@+id/big_ID"
>
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:id="@+id/tip_ID"
android:layout_marginLeft="10dp"
android:layout_below="@id/big_ID"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="人员名单"
android:textStyle="bold"
android:textSize="12sp"
android:gravity="center"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/enter"
android:gravity="center"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#E3E0E0"
android:id="@+id/small_ID"
android:layout_below="@+id/tip_ID"
/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"
android:layout_below="@id/small_ID"
android:nestedScrollingEnabled="false"
android:layoutAnimation="@anim/recycler_enter_anim"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@mipmap/nothing"
android:layout_below="@id/search_ID"
android:id="@+id/nothing_ID"
/>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:id="@+id/fix_ID"
android:visibility="gone"
android:background="#ffffffff"
android:paddingLeft="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="人员名单"
android:textStyle="bold"
android:textSize="12sp"
android:gravity="center"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/enter"
android:gravity="center"
/>
</LinearLayout>
</RelativeLayout>
2.fragment
package fragment
import adapter.UserAdapter
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import bean.UserBean
import com.example.filemanager.R
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.yl.recyclerview.listener.OnScrollListener
import com.yl.recyclerview.wrapper.LoadMoreWrapper
import kotlinx.android.synthetic.main.fragment_message.*
import org.json.JSONObject
import org.xutils.common.Callback
import org.xutils.http.RequestParams
import org.xutils.x
class MessageFragment : Fragment(),View.OnKeyListener {
private var mDataList:ArrayList<UserBean> = ArrayList()
private var userAdapter:UserAdapter? = null
private var recyclerView:RecyclerView? =null
private var swipeRefreshLayout:SwipeRefreshLayout? = null
private var mPageIndex = 1
private var loadmorewrapper:LoadMoreWrapper? = null
private var nothing:ImageView? = null
private val LogTag = "MessageFragment"
private var searchView:SearchView? = null
private var keyword:String = ""
private var nestedScrollView:NestedScrollView? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var view = inflater.inflate(R.layout.fragment_message,container,false)
userAdapter = UserAdapter(context!!,mDataList)
loadmorewrapper = LoadMoreWrapper(userAdapter)
val progressBar = ProgressBar(context)
loadmorewrapper!!.setLoadingView(progressBar)
val textView = TextView(context)
textView.text = "没有更多啦..."
loadmorewrapper!!.setLoadingEndView(textView)
init(view)
initAdapter()
addData()
return view
}
fun init(view:View){
nothing = view.findViewById(R.id.nothing_ID)
searchView = view.findViewById(R.id.search_ID)
nestedScrollView = view.findViewById(R.id.scrollview_ID)
nothing?.setOnClickListener(object :View.OnClickListener{
override fun onClick(v: View?) {
mPageIndex = 1;
addData()
}
})
recyclerView = view.findViewById(R.id.recyclerView)
recyclerView!!.setNestedScrollingEnabled(false)
recyclerView!!.addItemDecoration(DividerItemDecoration(context,DividerItemDecoration.VERTICAL))
searchView?.also {
it.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
mPageIndex = 1;
addData()
return false
}
override fun onQueryTextChange(newText: String): Boolean {
keyword = newText
Log.d("keyWord",newText)
return false
}
})
}
nestedScrollView?.setOnScrollChangeListener(object :NestedScrollView.OnScrollChangeListener{
override fun onScrollChange(
v: NestedScrollView,
scrollX: Int,
scrollY: Int,
oldScrollX: Int,
oldScrollY: Int
) {
if (scrollY >= (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
loadmorewrapper?.setLoadStateNotify(loadmorewrapper!!.LOADING);
mPageIndex++;
addData()
Log.d(LogTag,"pull to bottom")
}
Log.d(LogTag,"scrollY is: "+scrollY)
Log.d(LogTag,"search_ID.height is: "+search_ID.height)
Log.d(LogTag,"big_ID.height is: "+big_ID.height)
if(scrollY>=search_ID.height+big_ID.height){
fix_ID?.visibility = View.VISIBLE
tip_ID?.visibility = View.INVISIBLE
}else{
tip_ID?.visibility = View.VISIBLE
fix_ID?.visibility = View.INVISIBLE
}
}
})
recyclerView!!.addOnScrollListener(object: OnScrollListener() {
override fun onLoadMore() {
loadmorewrapper?.setLoadStateNotify(loadmorewrapper!!.LOADING);
mPageIndex++;
addData()
Log.d(LogTag,"pull to bottom")
}
})
swipeRefreshLayout = view.findViewById(R.id.swipe)
swipeRefreshLayout!!.setColorSchemeResources(R.color.colorPrimary)
swipeRefreshLayout!!.setOnRefreshListener(object :SwipeRefreshLayout.OnRefreshListener{
override fun onRefresh() {
mPageIndex = 1;
addData()
}
})
}
private fun initAdapter(){
recyclerView?.also {
it.layoutManager = LinearLayoutManager(activity)
it.adapter = loadmorewrapper
}
}
private fun addData(){
var url = "http://ip:8080/map"
var requestParams = RequestParams(url);
requestParams.addParameter("page",mPageIndex)
requestParams.addParameter("keyword",keyword)
x.http().post(requestParams,object :Callback.CommonCallback<JSONObject>{
override fun onFinished() {
swipeRefreshLayout!!.isRefreshing = false
}
override fun onSuccess(result: JSONObject) {
Log.d("MessageFragment", "success:" + result.toString())
val list = Gson().fromJson<ArrayList<UserBean>>(result.getJSONArray("data").toString(),
object : TypeToken<ArrayList<UserBean>>() {}.type)
if(mPageIndex==1){
mDataList.clear()
if(list.size>0){
nothing?.visibility = View.INVISIBLE
}else{
nothing?.visibility = View.VISIBLE
}
}
mDataList.addAll(list)
Log.d("MessageFragment","size is :"+mDataList.size)
// initAdapter()
// if (mDataList.size < 52) {
// loadmorewrapper!!.setLoadStateNotify(loadmorewrapper!!.LOADING_COMPLETE)
// } else {
// 设置所有数据加载完成状态,可自定义布局
loadmorewrapper!!.setLoadStateNotify(loadmorewrapper!!.LOADING_END)
// }
loadmorewrapper?.notifyDataSetChanged()
if(list.size>0){
recyclerView!!.scheduleLayoutAnimation()
}
}
override fun onCancelled(cex: Callback.CancelledException?) {
}
override fun onError(ex: Throwable?, isOnCallback: Boolean) {
Log.d("MessageFragment","error:"+ex.toString())
}
})
}
override fun onKey(v: View, keyCode: Int, event: KeyEvent): Boolean {
if(keyCode==KeyEvent.KEYCODE_ENTER&&event.action == KeyEvent.ACTION_DOWN)
{
}
return false
}
}
本文介绍了一个基于安卓平台的应用列表页面实现方案,包括SwipeRefreshLayout下拉刷新、NestedScrollView嵌套滚动视图、RecyclerView适配器及加载更多功能。文中详细展示了如何通过监听滚动事件来实现顶部固定提示栏效果,并利用SearchView进行搜索过滤。
208

被折叠的 条评论
为什么被折叠?



