当前位置:   article > 正文

harmony下拉刷新,上啦加载更多组件_harmonyos pulltorefresh

harmonyos pulltorefresh

目录

一、设计思路

二、动手实现


一、设计思路

①、自定义一个组件,加载一个内含Head、ListContainer、Foot的布局文件。

②、监听此布局的触摸事件

③、监听ListContainer是否滑动到顶部或者底部。

④、根据②和③监听事件做相应的显示和隐藏(head、foot)布局即可。

二、动手实现

1、新建类RefreshListContainer继承ComponentContainer

2、新建包含Head、ListContainer、Foot布局文件,refresh_layout.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DependentLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:background_element="$color:dialog_btn_gray_color"
  7. ohos:orientation="vertical"
  8. >
  9. <DependentLayout
  10. ohos:id="$+id:head_layout"
  11. ohos:height="match_content"
  12. ohos:width="match_parent"
  13. ohos:padding="10vp"
  14. >
  15. <Text
  16. ohos:id="$+id:head_text"
  17. ohos:height="match_content"
  18. ohos:width="match_content"
  19. ohos:center_in_parent="true"
  20. ohos:text="下拉刷新"
  21. ohos:text_size="17fp"
  22. />
  23. <Image
  24. ohos:id="$+id:head_image"
  25. ohos:height="40vp"
  26. ohos:width="40vp"
  27. ohos:image_src="$media:up_image"
  28. ohos:left_of="$+id:head_text"
  29. ohos:vertical_center="true"
  30. />
  31. </DependentLayout>
  32. <ListContainer
  33. ohos:id="$+id:list_container"
  34. ohos:height="match_parent"
  35. ohos:width="match_parent"
  36. ohos:background_element="white"
  37. ohos:below="$id:head_layout"
  38. />
  39. <DependentLayout
  40. ohos:id="$+id:foot_layout"
  41. ohos:height="70vp"
  42. ohos:width="match_parent"
  43. ohos:align_parent_bottom="true"
  44. ohos:padding="10vp"
  45. >
  46. <Text
  47. ohos:id="$+id:foot_text"
  48. ohos:height="match_content"
  49. ohos:width="match_content"
  50. ohos:center_in_parent="true"
  51. ohos:text="下拉刷新"
  52. ohos:text_size="17fp"
  53. />
  54. <Image
  55. ohos:id="$+id:foot_image"
  56. ohos:height="40vp"
  57. ohos:width="40vp"
  58. ohos:image_src="$media:up_image"
  59. ohos:left_of="$+id:foot_text"
  60. ohos:vertical_center="true"
  61. />
  62. </DependentLayout>
  63. </DependentLayout>

3、在自定义的类中加载布局,初始化各个组件

  1. private void init() {
  2. mRootLayout = (DependentLayout) LayoutScatter.getInstance(mContext)
  3. .parse(ResourceTable.Layout_refresh_layout, null, true);
  4. HiLog.error(LABEL, "init,rootLayout:%{public}s", mRootLayout);
  5. mHeadLayout = (DependentLayout) mRootLayout.findComponentById(ResourceTable.Id_head_layout);
  6. mFootLayout = (DependentLayout) mRootLayout.findComponentById(ResourceTable.Id_foot_layout);
  7. mListContainer = (ListContainer) mRootLayout.findComponentById(ResourceTable.Id_list_container);
  8. mHeadImage = (Image) mRootLayout.findComponentById(ResourceTable.Id_head_image);
  9. mHeadText = (Text) mRootLayout.findComponentById(ResourceTable.Id_head_text);
  10. mFootImage = (Image) mRootLayout.findComponentById(ResourceTable.Id_foot_image);
  11. mFootText = (Text) mRootLayout.findComponentById(ResourceTable.Id_foot_text);
  12. addComponent(mRootLayout);
  13. }

4、设置监听事件

mRootLayout.setTouchEventListener(this::onTouchEvent);
  1. @Override
  2. public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
  3. int action = touchEvent.getAction();
  4. MmiPoint point = touchEvent.getPointerPosition(touchEvent.getIndex());
  5. HiLog.error(LABEL, "action:%{public}s", action);
  6. switch (action) {
  7. case TouchEvent.PRIMARY_POINT_DOWN:
  8. HiLog.error(LABEL, "onTouchEvent:%{public}s", "PRIMARY_POINT_DOWN");
  9. mStartY = point.getY();
  10. break;
  11. case TouchEvent.PRIMARY_POINT_UP:
  12. HiLog.error(LABEL, "onTouchEvent:%{public}s", "PRIMARY_POINT_UP");
  13. HiLog.error(LABEL, "isPull:%{public}s", isPull);
  14. if (isPull) {
  15. int dis = (int) (mEndY - mStartY);
  16. HiLog.error(LABEL, "dis:%{public}s", dis);
  17. mHeadLayout.setMarginTop(0);
  18. pullLoadingStatus();
  19. isPull = false;
  20. if (this.mRefreshListener != null) {
  21. this.mRefreshListener.pullRefresh();
  22. }
  23. mListContainer.setEnabled(true);
  24. // isLoading = true;
  25. }
  26. if (isUp) {
  27. int dis = (int) (mStartY - mEndY);
  28. HiLog.error(LABEL, "dis:%{public}s", dis);
  29. mFootLayout.setMarginBottom(0);
  30. HiLog.error(LABEL, "mFootLayoutHeight:%{public}s", mFootLayoutHeight);
  31. mListContainer.setMarginBottom(mFootLayoutHeight);
  32. this.mBaseItemProvider.notifyDataSetItemInserted(mBaseItemProvider.getCount()-1);
  33. upLoadingStatus();
  34. isUp = false;
  35. if (this.mRefreshListener != null) {
  36. this.mRefreshListener.upLoadingMore();
  37. }
  38. mListContainer.setEnabled(true);
  39. }
  40. break;
  41. case TouchEvent.POINT_MOVE:
  42. HiLog.error(LABEL, "onTouchEvent:%{public}s", "HOVER_POINTER_MOVE");
  43. mEndY = point.getY();
  44. HiLog.error(LABEL, "onTouchEvent,mEndY:%{public}s,mStartY:%{public}s", mEndY, mStartY);
  45. HiLog.error(LABEL, "mListContainer.canScroll(DRAG_UP):%{public}s", mListContainer.canScroll(DRAG_UP));
  46. HiLog.error(LABEL, "isSocllTop:%{public}s", isSocllTop);
  47. if (!isLoading) {
  48. if (isSocllTop && !isUp) {
  49. if (mEndY - mStartY > 50) {
  50. isPull = true;
  51. if (pullAnimator != null) {
  52. pullAnimator.stop();
  53. mHeadImage.setRotation(0);
  54. }
  55. mHeadLayout.setVisibility(VISIBLE);
  56. mHeadLayout.setMarginTop((int) (mEndY - mStartY) - 50);
  57. if (mEndY - mStartY > 50 && mEndY - mStartY < 150) {
  58. pullDefault();
  59. } else {
  60. pullStatus();
  61. }
  62. mListContainer.setEnabled(false);
  63. }
  64. }
  65. if (!mListContainer.canScroll(DRAG_UP) && !isPull) {
  66. if (mStartY - mEndY > 50) {
  67. isUp = true;
  68. if (upAnimator != null) {
  69. upAnimator.stop();
  70. mFootImage.setRotation(0);
  71. }
  72. HiLog.error(LABEL, "mStartY - mEndY:%{public}s", mStartY - mEndY);
  73. mFootLayout.setVisibility(VISIBLE);
  74. int des = (int) (mStartY - mEndY) - 50;
  75. mListContainer.setMarginBottom(mFootLayoutHeight + des);
  76. mFootLayout.setMarginBottom(des);
  77. HiLog.error(LABEL, "mFootLayoutHeight:%{public}s", mFootLayoutHeight);
  78. this.mBaseItemProvider.notifyDataSetItemInserted(mBaseItemProvider.getCount()-1);
  79. if (mStartY - mEndY > 50 && mStartY - mEndY < 150) {
  80. upDefault();
  81. } else {
  82. upStatus();
  83. }
  84. // mListContainer.setEnabled(true);
  85. }
  86. }
  87. }
  88. break;
  89. default:
  90. }
  91. return true;
  92. }

5、新建回调事件

  1. package com.yan.refreshlistcontainer.listener;
  2. public interface RefreshListener {
  3. public void pullRefresh();
  4. public void upLoadingMore();
  5. }

6.提供方法供外部调用

  1. //下拉刷新和上啦加载更多完成时调用
  2. public void completeLoading() {
  3. if (pullAnimator != null) {
  4. pullAnimator.stop();
  5. }
  6. mHeadLayout.setVisibility(HIDE);
  7. pullDefault();
  8. if (upAnimator != null) {
  9. upAnimator.stop();
  10. }
  11. mFootLayout.setVisibility(HIDE);
  12. mListContainer.setMarginBottom(0);
  13. upDefault();
  14. }
  15. //给ListContainer设置适配器
  16. public void setItemProvider(BaseItemProvider baseItemProvider) {
  17. this.mBaseItemProvider = baseItemProvider;
  18. mListContainer.setItemProvider(this.mBaseItemProvider);
  19. }
  20. //设置上啦和下拉的监听事件
  21. public void setRefreshListener(RefreshListener refreshListener) {
  22. this.mRefreshListener = refreshListener;
  23. }

7、ability使用

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:alignment="center"
  7. ohos:orientation="vertical">
  8. <com.yan.refreshlistcontainer.component.RefreshListContainer
  9. ohos:id="$+id:refresh_list"
  10. ohos:height="match_parent"
  11. ohos:width="match_parent"/>
  12. </DirectionalLayout>
  1. package com.yan.refreshlistcontainer.slice;
  2. import com.yan.refreshlistcontainer.ResourceTable;
  3. import com.yan.refreshlistcontainer.bean.ListBean;
  4. import com.yan.refreshlistcontainer.component.RefreshListContainer;
  5. import com.yan.refreshlistcontainer.listener.RefreshListener;
  6. import com.yan.refreshlistcontainer.provider.RefreshListProvider;
  7. import ohos.aafwk.ability.AbilitySlice;
  8. import ohos.aafwk.content.Intent;
  9. import ohos.eventhandler.EventHandler;
  10. import ohos.eventhandler.EventRunner;
  11. import ohos.eventhandler.InnerEvent;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. public class MainAbilitySlice extends AbilitySlice {
  15. private RefreshListContainer mRefreshList;
  16. private List<ListBean> mData = new ArrayList<>();
  17. private MyEventHandler myHandler;
  18. private RefreshListProvider refreshListProvider;
  19. @Override
  20. public void onStart(Intent intent) {
  21. super.onStart(intent);
  22. super.setUIContent(ResourceTable.Layout_ability_main);
  23. mRefreshList = (RefreshListContainer) findComponentById(ResourceTable.Id_refresh_list);
  24. initList();
  25. EventRunner runner = EventRunner.create(true);
  26. myHandler = new MyEventHandler(runner);
  27. refreshListProvider = new RefreshListProvider(MainAbilitySlice.this, mData);
  28. mRefreshList.setItemProvider(refreshListProvider);
  29. mRefreshList.setRefreshListener(new RefreshListener() {
  30. @Override
  31. public void pullRefresh() {
  32. ListBean mListBean = new ListBean();
  33. mListBean.setAge(999);
  34. mListBean.setName("刷新新增Item");
  35. mData.add(0, mListBean);
  36. myHandler.sendEvent(1000, 3000);
  37. }
  38. @Override
  39. public void upLoadingMore() {
  40. ListBean mListBean = new ListBean();
  41. mListBean.setAge(999);
  42. mListBean.setName("加载更多新增Item");
  43. mData.add(mListBean);
  44. myHandler.sendEvent(1000, 3000);
  45. }
  46. });
  47. }
  48. private class MyEventHandler extends EventHandler {
  49. private MyEventHandler(EventRunner runner) {
  50. super(runner);
  51. }
  52. // 重写实现processEvent方法
  53. @Override
  54. public void processEvent(InnerEvent event) {
  55. super.processEvent(event);
  56. if (event == null) {
  57. return;
  58. }
  59. int eventId = event.eventId;
  60. switch (eventId) {
  61. case 1000:
  62. getUITaskDispatcher().syncDispatch(new Runnable() {
  63. @Override
  64. public void run() {
  65. refreshListProvider.notifyDataChanged();
  66. mRefreshList.completeLoading();
  67. }
  68. });
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. }
  75. private void initList() {
  76. for (int i = 0; i < 100; i++) {
  77. ListBean mListBean = new ListBean();
  78. mListBean.setAge(i);
  79. mListBean.setName("测试" + i);
  80. mData.add(mListBean);
  81. }
  82. }
  83. @Override
  84. public void onActive() {
  85. super.onActive();
  86. }
  87. @Override
  88. public void onForeground(Intent intent) {
  89. super.onForeground(intent);
  90. }
  91. }

至此大概的流程已经完成。

Screenshot_20210707_162751_com.yan.demo.jpg

Screenshot_20210707_162759_com.yan.demo.jpg

Screenshot_20210707_162803_com.yan.demo.jpg

Screenshot_20210707_162821_com.yan.demo.jpg

项目地址:https://gitee.com/WangYan2017/refresh-list-container

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/1008987
推荐阅读
相关标签
  

闽ICP备14008679号