From fc92e272a4b049499fd7901a0558f50d8b983f0d Mon Sep 17 00:00:00 2001 From: linminjie <1004644107@qq.com> Date: Tue, 6 Jun 2023 09:45:44 +0800 Subject: [PATCH] =?UTF-8?q?recycleview=20=E7=A7=BB=E5=8A=A8=E8=B0=83?= =?UTF-8?q?=E6=95=B4=20=20=E5=8C=BB=E9=99=A2=E8=AF=A6=E6=83=85=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unionmedtv/activity/HospitalActivity.java | 47 ++++++++++++---------- .../unionmedtv/adapter/HospitalAdapter.java | 4 +- .../unionmedtv/utils/MyGridLayoutManager.java | 1 + app/src/main/res/layout/item_load_more_layout1.xml | 39 ++++++++++++++++++ 4 files changed, 68 insertions(+), 23 deletions(-) create mode 100644 app/src/main/res/layout/item_load_more_layout1.xml diff --git a/app/src/main/java/com/unionmed/unionmedtv/activity/HospitalActivity.java b/app/src/main/java/com/unionmed/unionmedtv/activity/HospitalActivity.java index 6051491..aa2c72e 100644 --- a/app/src/main/java/com/unionmed/unionmedtv/activity/HospitalActivity.java +++ b/app/src/main/java/com/unionmed/unionmedtv/activity/HospitalActivity.java @@ -6,6 +6,7 @@ import android.os.Bundle; import android.os.Handler; import android.text.Editable; import android.text.TextWatcher; +import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.WindowManager; @@ -41,15 +42,17 @@ public class HospitalActivity extends BaseActivity { private HospitalAdapter hospitalAdapter; private int startNum = 1; private int endNum = 10; - private int numberOfColumns=2; - List list=new ArrayList<>(); + private int numberOfColumns = 2; + List list = new ArrayList<>(); private EditText et_search; private Handler handler = new Handler(); private int selectPostion = 0;//当前列表item点击位置 - public static void StartActivity(Context context){ - Intent intent=new Intent(context, HospitalActivity.class); + + public static void StartActivity(Context context) { + Intent intent = new Intent(context, HospitalActivity.class); context.startActivity(intent); } + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -58,12 +61,13 @@ public class HospitalActivity extends BaseActivity { initView(); loadData(1); } - public void initView(){ - et_search=findViewById(R.id.et_search); + + public void initView() { + et_search = findViewById(R.id.et_search); et_search.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean b) { - InputKeyBoardUtils.show(HospitalActivity.this,view,b); + InputKeyBoardUtils.show(HospitalActivity.this, view, b); } }); et_search.addTextChangedListener(new TextWatcher() { @@ -83,26 +87,27 @@ public class HospitalActivity extends BaseActivity { } }); - recyclerView=findViewById(R.id.recyclerView); + recyclerView = findViewById(R.id.recyclerView); recyclerView.requestFocus(); - recyclerView=findViewById(R.id.recyclerView); - MyGridLayoutManager gridLayoutManager= new MyGridLayoutManager(HospitalActivity.this,numberOfColumns); + recyclerView = findViewById(R.id.recyclerView); + MyGridLayoutManager gridLayoutManager = new MyGridLayoutManager(HospitalActivity.this, numberOfColumns); + gridLayoutManager.setUpView(et_search); recyclerView.setLayoutManager(gridLayoutManager); - hospitalAdapter =new HospitalAdapter(list, new OnChildClickListener() { + hospitalAdapter = new HospitalAdapter(list, new OnChildClickListener() { @Override public void onChildClick(View view, int position, T data) { selectPostion = position; - GetHospitalApi.MemberBean memberBean= (GetHospitalApi.MemberBean) data; - if(position==list.size()-1){ - startNum++; + GetHospitalApi.MemberBean memberBean = (GetHospitalApi.MemberBean) data; + if (memberBean.isLast()) { + startNum = startNum + endNum; loadData(0); - }else { - HospitalDetailActivity.StartActivity(HospitalActivity.this,memberBean.getChosCode()); + } else { + HospitalDetailActivity.StartActivity(HospitalActivity.this, memberBean.getChosCode()); } } }); recyclerView.setAdapter(hospitalAdapter); - recyclerView.addItemDecoration(new LinearSpacingItemDecoration(HospitalActivity.this,24)); + recyclerView.addItemDecoration(new LinearSpacingItemDecoration(HospitalActivity.this, 24)); } private Runnable searchRunnable = new Runnable() { @@ -114,9 +119,10 @@ public class HospitalActivity extends BaseActivity { // ... } }; - public void loadData(int type){ + + public void loadData(int type) { EasyHttp.post(HospitalActivity.this) - .api(new GetHospitalApi(et_search.getText().toString(),startNum, CacheUtil.getPkey(),endNum)) + .api(new GetHospitalApi(et_search.getText().toString(), startNum, CacheUtil.getPkey(), endNum)) .request(new OnHttpListener>>() { @Override public void onSucceed(HttpData> stringHttpData) { @@ -136,14 +142,13 @@ public class HospitalActivity extends BaseActivity { memberBean.setLast(true); list.add(memberBean); } - hospitalAdapter.notifyDataSetChanged(); if (type == 0) { recyclerView.requestFocus(); recyclerView.post(new Runnable() { @Override public void run() { - View view = recyclerView.getLayoutManager().findViewByPosition(selectPostion); + View view = recyclerView.getLayoutManager().findViewByPosition(selectPostion - 1); if (view != null) { recyclerView.requestFocus(); view.requestFocus(); diff --git a/app/src/main/java/com/unionmed/unionmedtv/adapter/HospitalAdapter.java b/app/src/main/java/com/unionmed/unionmedtv/adapter/HospitalAdapter.java index 2a54352..86ac694 100644 --- a/app/src/main/java/com/unionmed/unionmedtv/adapter/HospitalAdapter.java +++ b/app/src/main/java/com/unionmed/unionmedtv/adapter/HospitalAdapter.java @@ -32,7 +32,7 @@ public class HospitalAdapter extends BaseDelegateMultiAdapter data, int position) { // 根据数据,自己判断应该返回的类型 - if (position == data.size()-1) { + if (data.get(position).isLast()) { return 0; } else { return 1; @@ -40,7 +40,7 @@ public class HospitalAdapter extends BaseDelegateMultiAdapter + + + + + + + + + \ No newline at end of file