setListData方法 体会

本文详细解析了setListData方法的工作原理,强调该方法用于重新构造数据模型而非修改原有模型。在模型重构过程中,所有先前的选择会被清除并触发选择更改事件。因此,在处理选择更改事件时,需要特别注意检查是否还有有效选择。
        /*
          *setListData方法是重新构造模型,并不是在更改原有的模型,重新构造时会清空选择
         *也要执行一次选择更改事件,所以在选择更改事件中应首先判断是否有选择
         */
package com.example.myexamproject.fragment; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.RecyclerView; import com.example.myexamproject.R; import com.google.gson.Gson; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class NewsFragment extends Fragment { private View rootView; private RecyclerView mRecycleView; //接入api地址 private static String url="http://v.juhe.cn/toutiao/index?key=c35472cf8f1d202fbe25f7e2a1f79f30&type=jiankang"; private NewsListAdapter mListAdapter; private Handler mHandler = new Handler(Looper.myLooper()){ @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); if (msg.what==100){ String data = (String) msg.obj; //Log.d("test","data = "+data); //将json数据转成java bean NewsInfo newsInfo = new Gson().fromJson(data, NewsInfo.class); if(newsInfo != null && newsInfo.getError_code()==0) { if (null != mListAdapter) { mListAdapter.setListData(newsInfo.getResult().getData()); } }else { Toast.makeText(getActivity(),"获取数据失败,请稍后重试",Toast.LENGTH_SHORT).show(); } } } }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment rootView = inflater.inflate(R.layout.fragment_news, container, false); //初始化控件 mRecycleView = rootView.findViewById(R.id.recyclerView); //初始化适配器 mListAdapter = new NewsListAdapter(getActivity()); //设置adapter mRecycleView.setAdapter(mListAdapter); //获取网络数据 getHttpData(); return rootView; } private void getHttpData() { //创建OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient(); //构构造Request对象 Request request = new Request.Builder() .url(url) .get() .build(); //通过OkHttpClient和Request对象来构建Call对象 Call call = okHttpClient.newCall(request); //通过Call对象的enqueue(Callback)方法来执行异步请求 call.enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { Log.d("---------", "onFailure() returned: " + e.toString()); } @Override public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { String data =response.body().string(); //Log.d("--------------", "onResponse: " + response.body().string()); //不能直接在这里操作UI,因为这是耗时操作,要使用Handler Message message = new Message(); message.what = 100; message.obj = data; //发送 mHandler.sendMessage(message); } }); } }
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值