写此文章仅为记录所学习的过程,供大家参考
这篇文章是为了实现ListView的上拉刷新下拉加载,其中用到了插件有:
implementation 'com.jakewharton:butterknife:5.1.1' compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'
可参考我写过的一篇如何配置Butterknife。
文章中ListView的使用可以参考点击打开链接
好了话不多说,上代码:
首先这是所需要的一些文件

其下为各个页面
item_text
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:orientation="horizontal" android:gravity="center"> <TextView android:id="@+id/item_text_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是第" android:textSize="30sp"/> <TextView android:id="@+id/item_text_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:textSize="30sp"/> <TextView android:id="@+id/item_text_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="条数据" android:textSize="30sp"/> </LinearLayout>
activity_main
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="com.example.asus.texttorefreshlist.MainActivity"> <TextView android:id="@+id/tv_notice_no_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center" android:text="暂无数据" android:textSize="14sp" /> <com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/srl_notice_list" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lv_text" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="#00FFFFFF" android:dividerHeight="12dp" android:paddingTop="15dp" android:overScrollMode="never" android:scrollbars="none" tools:listitem="@layout/item_text" /> </com.scwang.smartrefresh.layout.SmartRefreshLayout> </RelativeLayout>
TextModel
public class TextModel { public int num; public TextModel(int num) { this.num = num; } }
TextAdapter
public class TextAdapter extends BaseAdapter { private Context mContext; private List<TextModel> lists; public TextAdapter(Context context, List<TextModel> dataList) { mContext = context; lists = dataList; } @Override public int getCount() { return lists.size(); } @Override public Object getItem(int position) { return lists.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { LayoutInflater vi = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = vi.inflate(R.layout.item_text, parent, false); holder = new ViewHolder(convertView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if (getCount() != 0) { TextModel model = (TextModel) getItem(position); if (model != null) { holder.itemTextNum.setText(model.num + ""); } } return convertView; } static class ViewHolder { @InjectView(R.id.item_text_up) TextView itemTextUp; @InjectView(R.id.item_text_num) TextView itemTextNum; @InjectView(R.id.item_text_down) TextView itemTextDown; ViewHolder(View view) { ButterKnife.inject(this, view); } } }
MainActivity
public class MainActivity extends AppCompatActivity { @InjectView(R.id.lv_text) ListView lvText; @InjectView(R.id.srl_notice_list) SmartRefreshLayout srlNoticeList; @InjectView(R.id.tv_notice_no_date) TextView tvNoticeNoDate; private List<TextModel> textList; private TextModel textModel; private TextAdapter textAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initVariables(); setContentView(R.layout.activity_main); ButterKnife.inject(this); initView(); initRefresh(); } private void initVariables() { textList = new ArrayList<>(); textAdapter = new TextAdapter(this, textList); } private void initView() { lvText.setAdapter(textAdapter); lvText.setEmptyView(tvNoticeNoDate); textAdapter.notifyDataSetChanged(); } private void initDate() { textModel = new TextModel(1); textList.add(textModel); textModel = new TextModel(2); textList.add(textModel); textModel = new TextModel(3); textList.add(textModel); textModel = new TextModel(4); textList.add(textModel); textModel = new TextModel(5); textList.add(textModel); textModel = new TextModel(6); textList.add(textModel); } //下拉刷新,上拉加载 private void initRefresh() { //设置 Header srlNoticeList.setRefreshHeader(new ClassicsHeader(this).setEnableLastTime(false).setFinishDuration(0)); //设置 Footer srlNoticeList.setRefreshFooter(new ClassicsFooter(this).setFinishDuration(0)); srlNoticeList.setEnableRefresh(true); srlNoticeList.setEnableAutoLoadmore(true); srlNoticeList.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() { @Override public void onLoadmore(RefreshLayout refreshlayout) { refreshlayout.getLayout().postDelayed(new Runnable() { @Override public void run() { RefreshList(false); } }, 1000); } @Override public void onRefresh(RefreshLayout refreshlayout) { refreshlayout.getLayout().postDelayed(new Runnable() { @Override public void run() { RefreshList(true); } }, 1000); } }); } private void RefreshList(Boolean up) { if (up) { initDate(); srlNoticeList.finishRefresh(); } else { textList.clear(); srlNoticeList.finishLoadmore(); } textAdapter.notifyDataSetChanged(); } }
本文章只是为了记录,复用,多谢观看。
Create By 2018,4,12
本文介绍如何在Android应用中实现ListView的上拉刷新和下拉加载功能,使用了Butterknife和SmartRefreshLayout插件,并提供了完整的代码示例。
258

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



