万能适配器多布局上拉下拉刷新

万能适配器

导入依赖
//万能适配器
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'
//SmartRefreshLayout
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0' 

主布局

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/smart_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyler_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </androidx.recyclerview.widget.RecyclerView>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>


</LinearLayout>

OkHttpUtils工具类

public class OkHttpUtils {
   
    private OkHttpClient okHttpClient;
    private Handler handler = new Handler();

    private OkHttpUtils() {
   
        okHttpClient = new OkHttpClient.Builder()
                .writeTimeout(60 * 1000, TimeUnit.MILLISECONDS)
                .readTimeout(60 * 1000, TimeUnit.MILLISECONDS)
                .build();
    }

    private static OkHttpUtils okHttpUtils = new OkHttpUtils();

    public static OkHttpUtils getInstance() {
   
        return okHttpUtils;
    }

    //get请求
    public void doget(String url, final OkhttpCallBack okhttpCallBack) {
   
        Request request = new Request.Builder().get().url(url).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
   
            @Override
            public void onFailure(Call call, IOException e) {
   
                final String message = e.getMessage();
                handler.post(new Runnable() {
   
                    @Override
                    public void run() {
   
                        okhttpCallBack.onError(message);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
   
                final String string = response.body().string();
                handler.post(new Runnable() {
   
                    @Override
                    public void run() {
   
                        okhttpCallBack.onOk(string);
                    }
                });
            }
        });
    }

    //post请求
    public void dopost(String url, HashMap<String, String> map, final OkhttpCallBack okhttpCallBack) {
   
        Set<Map.Entry<String, String>> entries = map.entrySet();
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry<String, String> entry : entries) {
   
            builder.add(entry.getKey(), entry.getValue());
        }
        FormBody formBody = builder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        okHttpClient.newCall
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现上拉刷新、下拉加载的 ListView,可以使用第三方库,如 Google 推荐的 SwipeRefreshLayout,或者自己实现。 下面是自己实现的一种方法: 1. 在布局文件中加入 ListView 和 ProgressBar: ``` <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" /> <ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:visibility="gone" /> </RelativeLayout> ``` 2. 在 Activity 或 Fragment 中初始化 ListView 和适配器: ``` listView = findViewById(R.id.listView); listView.setAdapter(adapter); ``` 3. 给 ListView 设置滚动监听器,当滚动到底部时,执行加载更多的操作: ``` listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { if (view.getLastVisiblePosition() == view.getCount() - 1) { // 滚动到底部,执行加载更多操作 loadMoreData(); } } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } }); ``` 4. 在加载更多的操作中,显示 ProgressBar,加载数据后更新适配器,隐藏 ProgressBar: ``` private void loadMoreData() { progressBar.setVisibility(View.VISIBLE); // 加载数据 // 更新适配器 progressBar.setVisibility(View.GONE); } ``` 5. 实现下拉刷新,可以使用 SwipeRefreshLayout,在布局文件中加入 SwipeRefreshLayout 和 ListView: ``` <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.v4.widget.SwipeRefreshLayout> ``` 6. 在 Activity 或 Fragment 中初始化 SwipeRefreshLayout 和 ListView,并给 SwipeRefreshLayout 设置刷新监听器: ``` swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { // 下拉刷新,重新加载数据 refreshData(); } }); ``` 7. 在刷新数据的操作中,显示 ProgressBar,加载数据后更新适配器,隐藏 ProgressBar 和 SwipeRefreshLayout: ``` private void refreshData() { progressBar.setVisibility(View.VISIBLE); // 加载数据 // 更新适配器 progressBar.setVisibility(View.GONE); swipeRefreshLayout.setRefreshing(false); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值