在androidstudio中使用java实现web的无限滚动“加载更多”按钮

ymdaylpp  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(282)

到目前为止,我还找不到一篇文章解释如何在androidstudio的java中处理刮取无限滚动页面。也许我不擅长搜索或者别的。
我正在做一个新闻应用程序项目,它将使用网页抓取技术显示新闻,还可以从下一页获取数据。更多信息:如何使用java抓取包含多个页面的网站
但是,现在我面临一个新的问题,因为我的一个新闻来源使用无限滚动的方法为他们的网站->https://www.gameinformer.com/news
那么,有没有办法从这个网站的“加载更多”按钮获取新闻数据?
如果有人需要我的代码,这里是:
gameinformer的主要活动:

package com.example.user.newsapp;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

public class News_GameInformer extends AppCompatActivity {

    Toolbar toolbar;
    private ProgressDialog mProgressDialog;
    private String url = "https://www.gameinformer.com/news";
    private ArrayList<String> mNewsTitleList = new ArrayList<>();
    private ArrayList<String> mNewsDescList = new ArrayList<>();
    private ArrayList<String> mNewsDateList = new ArrayList<>();
    private CardView cardView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news__game_informer);
        new Description().execute();

        toolbar = findViewById(R.id.toolbar_news_gameinformer);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private class Description extends AsyncTask<Void,Void,Void> {
        String desc;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(News_GameInformer.this);
            mProgressDialog.setTitle("Connecting to GameInformer.com");
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(true);
            mProgressDialog.setCancelable(false);
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Document mNewsDocument = Jsoup.connect(url).get();
                Elements mElementDataSize = mNewsDocument.select("article[class=node teaser node--type-article node--promoted node--view-mode-teaser]");
                int mElementSize = mElementDataSize.size();

                for (int i=0;i<mElementSize;i++) {
                    Elements mElementNewsTitle = mNewsDocument.select("h2[class=page-title]").select("span").eq(i);
                    String mNewsTitle = mElementNewsTitle.text();

                    Elements mElementNewsDesc = mNewsDocument.select("div[class=field field--name-field-promo-summary field--type-string field--label-hidden field__item]").eq(i);
                    String mNewsDesc = mElementNewsDesc.text();

                    Elements mElementNewsDate = mNewsDocument.select("div[class=node__submitted author-details]").select("span[class=field field--name-created field--type-created field--label-hidden]").eq(i);
                    String mNewsDate = mElementNewsDate.text();

                    mNewsTitleList.add(mNewsTitle);
                    mNewsDescList.add(mNewsDesc);
                    mNewsDateList.add(mNewsDate);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            RecyclerView mRecyclerView = findViewById(R.id.act_recyclerview_gi_news);
            gameinformer_DataAdapter mDataAdapter = new gameinformer_DataAdapter(News_GameInformer.this, mNewsTitleList, mNewsDescList, mNewsDateList, cardView);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setAdapter(mDataAdapter);

            mProgressDialog.dismiss();

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id==R.id.gamezone) {

            Intent i = new Intent(getApplicationContext(),News_GameZone.class);
            startActivity(i);

        } else if (id==R.id.gameinformer) {

            Intent i = new Intent(getApplicationContext(),News_GameInformer.class);
            startActivity(i);

        } else if (id==R.id.gamespot) {

            Intent i = new Intent(getApplicationContext(),News_GameSpot.class);
            startActivity(i);

        } else if (id==R.id.pcgamer) {

            Intent i = new Intent(getApplicationContext(),News_PCGamer.class);
            startActivity(i);

        } else if (id==android.R.id.home) {

            finish();

        } else if (id==R.id.menuinfo) {

            Intent i = new Intent(getApplicationContext(),Info.class);
            startActivity(i);

        } else if (id==R.id.menupeople) {

            Intent i = new Intent(getApplicationContext(),People.class);
            startActivity(i);

        }

        return super.onOptionsItemSelected(item);
    }

}

数据适配器代码:

package com.example.user.newsapp;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class gameinformer_DataAdapter extends RecyclerView.Adapter<gameinformer_DataAdapter.MyViewHolder> {

    private ArrayList<String> mNewsTitleList = new ArrayList<>();
    private ArrayList<String> mNewsDescList = new ArrayList<>();
    private ArrayList<String> mNewsDateList = new ArrayList<>();
    private Activity mActivity;
    private int lastPosition=-1;
    private CardView cardView;

    public gameinformer_DataAdapter (News_GameInformer activity, ArrayList<String> mNewsTitleList, ArrayList<String> mNewsDescList, ArrayList<String> mNewsDateList, CardView cardView) {
        this.mActivity = activity;
        this.mNewsTitleList = mNewsTitleList;
        this.mNewsDescList = mNewsDescList;
        this.mNewsDateList = mNewsDateList;
        this.cardView = cardView;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView gameinformer_news_title, gameinformer_news_desc, gameinformer_news_date;
        private CardView cardView_layout;

        public MyViewHolder(View view) {
            super(view);
            gameinformer_news_title = view.findViewById(R.id.row_gi_news_title);
            gameinformer_news_desc = view.findViewById(R.id.row_gi_news_desc);
            gameinformer_news_date = view.findViewById(R.id.row_gi_news_date);
            cardView_layout = view.findViewById(R.id.gi_cardview);
        }

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.gameinformer_row_data,parent,false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.gameinformer_news_title.setText(mNewsTitleList.get(position));
        holder.gameinformer_news_desc.setText(mNewsDescList.get(position));
        holder.gameinformer_news_date.setText(mNewsDateList.get(position));
        holder.cardView_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = mNewsTitleList.get(position);
                Intent i = new Intent(mActivity, News_GameInformer_Webview.class);
                i.putExtra("keytitle",title);
                mActivity.startActivity(i);
            }
        });
    }

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

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题