Android爬虫(二)RecyclerView加载网络爬虫数据:文字+图片

上次抓到了数据,这次来做下界面展示,大概长这样

在这里插入图片描述
在这里插入图片描述
gragle里需要引入包

implementation 'com.android.support:recyclerview-v7:28.0.0'

我的主界面包含的用于展示的Fragment
ArticleFragment.class

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.swlgoods.R;
import com.swlgoods.adapter.ArticleAdapter;
import com.swlgoods.base.BaseFragment;
import com.swlgoods.entity.Article;
import com.swlgoods.utils.GetData;
import com.swlgoods.utils.OkHttpUtils;

import java.util.ArrayList;


/*
 *@Author:Swallow
 *@Date:2019/3/12
 * 展示文章
 */
public class ArticleFragment extends BaseFragment {
    private RecyclerView rv_article;

    private Handler handler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what){
                case 1:
                    ArrayList<Article> articles = (ArrayList<Article>) msg.obj;
                    ArticleAdapter adapter = new ArticleAdapter(articles,mContext);
                    rv_article.setLayoutManager(new LinearLayoutManager(mContext));
                    rv_article.setAdapter(adapter);
                    break;
            }
        }
    };

    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.fragment_article,null);
        rv_article = view.findViewById(R.id.rv_article);
        initData();
        return view;
    }

    @Override
    protected void initData() {
        final String url = "https://www.smzdm.com/";
        new Thread(){
            public void run(){
                String html = OkHttpUtils.OkGetArt(url);
                ArrayList<Article> articles = GetData.spiderArticle(html);
                Message message = handler.obtainMessage();
                message.what = 1;
                message.obj = articles;
                handler.sendMessage(message);
            }
        }.start();
        super.initData();
    }
}

适配器ArticleAdapter.class


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.swlgoods.R;
import com.swlgoods.entity.Article;

import java.util.ArrayList;

/*
 *@Author:Swallow
 *@Date:2019/3/21
 * 展示文章信息的RecyclerView的适配器
 */
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHoledr> {
    private ArrayList<Article> articles;
    private Context context;
    private DisplayImageOptions options;

    public ArticleAdapter(ArrayList<Article> articles, Context context) {
        this.articles = articles;
        this.context = context;
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .showImageOnLoading(R.drawable.home)
                .showImageForEmptyUri(R.drawable.home_press)
                .showImageOnFail(R.drawable.setting_press)
                .build();
    }

    static class ViewHoledr extends RecyclerView.ViewHolder{

        ImageView iv_icon;
        TextView tv_title,tv_context,tv_author;

        public ViewHoledr(@NonNull View itemView) {
            super(itemView);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_context = itemView.findViewById(R.id.tv_context);
            tv_author = itemView.findViewById(R.id.tv_author);
        }
    }

    @NonNull
    @Override
    public ArticleAdapter.ViewHoledr onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.articleslist,null);
        return new ArticleAdapter.ViewHoledr(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHoledr viewHoledr, int i) {
        Article article = articles.get(i);

        viewHoledr.tv_title.setText(article.getTitle());
        viewHoledr.tv_context.setText(article.getContext());
        viewHoledr.tv_author.setText(article.getAuthor());

        ImageLoader.getInstance().displayImage(article.getImgUrl(),viewHoledr.iv_icon,options);
    }

    @Override
    public int getItemCount() {
        return articles.size();
    }

}

这里我用了一个加载图片的框架,下载jar包导入工程

implementation files('libs/universal-image-loader-1.9.5.jar')

这个框架需要配置Application类

import android.app.Application;
import android.os.Environment;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.xutils.x;

import java.io.File;

/*
 *@Author:Swallow
 *@Date:2019/3/12
 * 代表整个软件
 * 初始化xUtils3
 */
public class GoodsApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.setDebug(true);
        x.Ext.init(this);

        File cacheFile = new File(Environment.getExternalStorageDirectory().getPath() + "/abc");
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
                .memoryCacheExtraOptions(480,800)
                .threadPoolSize(2)
                .threadPriority(4)
                .memoryCacheSize(2*1024*1024)
                //.diskCache(new UnlimitedDiscCache(cacheFile))
                .diskCacheSize(20*1024*1024)
                .writeDebugLogs()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .build();
        ImageLoader.getInstance().init(configuration);

    }
}

个人觉得recyclerview还是挺好用的,写起来代码没那么多,比ListView简单很多

然后贴一下界面
articleslist.xml

<?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"
    android:layout_width="match_parent"
    android:layout_height="200dp">

    <TextView
        android:id="@+id/tv_title"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="2"
        android:layout_marginHorizontal="12dp"
        android:text="title" />

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_marginLeft="12dp"
        android:layout_centerVertical="true"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/tv_title"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:srcCompat="@drawable/newscenter_press" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/iv_icon"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="12dp"
        android:layout_centerVertical="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_context"
            android:textSize="14sp"
            android:lines="4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="context" />

        <TextView
            android:id="@+id/tv_author"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:text="Swallow" />

    </LinearLayout>
</RelativeLayout>

fragment_article.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_article"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

参考资料
https://blog.csdn.net/qq_38373150/article/details/79433827
https://blog.csdn.net/m0_38044521/article/details/78229225
https://blog.csdn.net/NIJIADALINGE/article/details/80487782

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值