美文网首页
网络加载+Recyclerview

网络加载+Recyclerview

作者: 今天也要努力呀y | 来源:发表于2020-04-01 12:32 被阅读0次

    参考视频:
    https://www.bilibili.com/video/BV1TJ411v75g?p=6

    用到的后台:https://github.com/TrillGates/SOBAndroidMiniWeb

    下载成压缩包,然后启动jar包就行

    bean类时使用gsonFormat自动生成的一个
    代码:

    
    public class MainActivity extends AppCompatActivity {
        private static final String TAG = "MainActivity";
        GetTextItemAdapter mAdapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
    
        private void initView() {
            RecyclerView recyclerView = findViewById(R.id.result_list);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                    outRect.top = 5;
                    outRect.bottom = 5;
                }
            });
            mAdapter = new GetTextItemAdapter();
            recyclerView.setAdapter(mAdapter);
        }
    
        public void loadJson(View view) {
    //实际项目不能这么写
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        URL url = new URL("http://10.0.2.2:9102/get/text");
                        //URL url = new URL("https://api.sunofbeach.net/shop/api/discovery/categories");
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setReadTimeout(10000);
                        connection.setRequestMethod("GET");
                        connection.connect();
                        int responseCode = connection.getResponseCode();
                        if (responseCode == 200) {
                            InputStream inputStream = connection.getInputStream();
                            BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
                            StringBuilder response = new StringBuilder();
                            String json;
                            while ((json = bf.readLine())!=null)
                            {
                                response.append(json);
                            }
                            //Log.d(TAG, ""+line);
                            Gson gson = new Gson();
                            GetTextItem getTextItem = gson.fromJson(response.toString(),GetTextItem.class);
                            upDateUi(getTextItem);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }).start();
    
        }
    
        private void upDateUi(final GetTextItem getTextItem) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                mAdapter.setData(getTextItem);
                }
            });
        }
    }
    
    
    
    public class GetTextItemAdapter extends RecyclerView.Adapter<GetTextItemAdapter.ViewHolder> {
        private List<GetTextItem.DataBean> data = new ArrayList<>();
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_get_text, parent, false);
            return new ViewHolder(inflate);
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            View view = holder.itemView;
            TextView title = view.findViewById(R.id.item_title);
            GetTextItem.DataBean dataBean = data.get(position);
            title.setText(dataBean.getTitle());
            ImageView imageView = view.findViewById(R.id.item_img);
            Glide.with(view.getContext()).load("http://10.0.2.2:9102"+dataBean.getCover()).into(imageView);
        }
    
        @Override
        public int getItemCount() {
            return data.size();
        }
    
        public void setData(GetTextItem getTextItem) {
            data.clear();
            data.addAll(getTextItem.getData());
            notifyDataSetChanged();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:网络加载+Recyclerview

          本文链接:https://www.haomeiwen.com/subject/uebouhtx.html