RecyclerView URL重定向到WebView(WebScraping)



我正在使用webScraping创建一个新闻应用程序,并使用RecyclerView来包含我的新闻行数据。

现在我的问题是,当用户点击其中一条新闻行(可能是RecyclerView onClick?(时,我不知道如何重定向我的新闻URL,因为使用网络抓取,每条新闻都有不同的URL。

[请注意,我不知道如何在onClickListener中实现URL Intent,所以我不会对该部分进行编码]

任何帮助都将不胜感激,谢谢:'(

这是我的MainActivity.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/act_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

这是我的row_data.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/row_news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/row_news_author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>
<TextView
android:id="@+id/row_news_upload_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>

这是我的DataAdapter代码:

package com.example.user.webscraping2;
import android.app.Activity;
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 DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder> {
private ArrayList<String> mNewsTitleList = new ArrayList<>();
private ArrayList<String> mAuthorNameList = new ArrayList<>();
private ArrayList<String> mNewsUploadDateList = new ArrayList<>();
private Activity mActivity;
private int lastPosition = -1;
public DataAdapter(MainActivity activity, ArrayList<String> mNewsTitleList, ArrayList<String> mAuthorNameList, ArrayList<String> mNewsUploadDateList) {
this.mActivity = activity;
this.mNewsTitleList = mNewsTitleList;
this.mAuthorNameList = mAuthorNameList;
this.mNewsUploadDateList = mNewsUploadDateList;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView gamezone_news_title, gamezone_news_author, gamezone_upload_date;
public MyViewHolder(View view) {
super(view);
gamezone_news_title = view.findViewById(R.id.row_news_title);
gamezone_news_author = view.findViewById(R.id.row_news_author);
gamezone_upload_date = view.findViewById(R.id.row_news_upload_date);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_data,parent,false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.gamezone_news_title.setText(mNewsTitleList.get(position));
holder.gamezone_news_author.setText(mAuthorNameList.get(position));
holder.gamezone_upload_date.setText(mNewsUploadDateList.get(position));
}
@Override
public int getItemCount() {
return mNewsTitleList.size();
}
}

这是我的主要活动代码:

package com.example.user.webscraping2;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 MainActivity extends AppCompatActivity {
private ProgressDialog mProgressDialog;
private String url = "https://www.gamezone.com/news/";
private ArrayList<String> mAuthorNameList = new ArrayList<>();
private ArrayList<String> mNewsUploadDateList = new ArrayList<>();
private ArrayList<String> mNewsTitleList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Description().execute();
}
private class Description extends AsyncTask<Void,Void,Void> {
String desc;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("Android Basic Jsoup Tutorial");
mProgressDialog.setMessage("Loading ...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
Document mNewsDocument = Jsoup.connect(url).get();
Elements mElementDataSize = mNewsDocument.select("div[class=td_module_1 td_module_wrap td-animation-stack]");
int mElementSize = mElementDataSize.size();
for (int i=0;i<mElementSize;i++) {
Elements mElementAuthorName = mNewsDocument.select("span[class=td-post-author-name]").select("a").eq(i);
String mAuthorName = mElementAuthorName.text();
Elements mElementNewsUploadDate = mNewsDocument.select("span[class=td-post-date]").select("time").eq(i);
String mNewsUploadDate = mElementNewsUploadDate.text();
Elements mElementNewsTitle = mNewsDocument.select("h3[class=entry-title td-module-title]").select("a").eq(i);
String mNewsTitle = mElementNewsTitle.text();
mAuthorNameList.add(mAuthorName);
mNewsUploadDateList.add(mNewsUploadDate);
mNewsTitleList.add(mNewsTitle);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
RecyclerView mRecyclerView = findViewById(R.id.act_recyclerview);
DataAdapter mDataAdapter = new DataAdapter(MainActivity.this, mNewsTitleList, mAuthorNameList, mNewsUploadDateList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mDataAdapter);
mProgressDialog.dismiss();
}
}
}

这是屏幕截图:

图像

试试这个代码,

holder.gamezone_news_title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = mNewsTitleList.get(position);
Intent mintent = new Intent(mActivity, "your webview load class name");
mintent.putExtra("keytitle", title);
startActivity(mintent);
}
});

下一个webview加载活动下面的代码先写后加载url。

String url = "https://www.gamezone.com/news/";
Bundle mbundle = getIntent().getExtras();
if(mbundle != null){
String str = mbundle.getString("keytitle");
url += str+"/";
url = url.replaceAll(" ", "-");
}

最新更新