Android:从 RecyclerView 打开 PDF
Android: Opening PDFs From a RecyclerView
问题的标题没有完全解释我的问题,所以请允许我详细说明。我有一个应用程序可以将来自 Google Civic API 的 JSON 数据解析为 recyclerview。一个特定的字段是一个 TextView,它将 PDF url 存储在一个字符串中。如何设置 TextView 使其可点击并最好在应用程序中打开 PDF。我查找了解决方案,并尝试使用 WebView 和 PDFReader 等,但我找不到任何提到如何从 recyclerview 执行此操作的解决方案,因此我不确定将此代码放在哪里。由于我正在单击 recyclerView 中的一个项目,我想我可能需要在 recyclerview 适配器中添加代码而不是主要 class.
这是主要代码 class:这个 class 处理 JSON 解析
public class VoterInformation extends AppCompatActivity {
private static final String url = "https://www.googleapis.com/civicinfo/v2/voterinfo?address=2613+Irvington+Ave+San+Bernardino+CA+92407&electionId=2000&officialOnly=false&returnAllAvailableData=false&fields=contests(referendumBallotResponses%2CreferendumBrief%2CreferendumConStatement%2CreferendumEffectOfAbstain%2CreferendumPassageThreshold%2CreferendumProStatement%2CreferendumSubtitle%2CreferendumText%2CreferendumTitle%2CreferendumUrl)&key=AIzaSyB1B5mEKHK8PHDiNcGQ5ZU3fPIH9KWxAcQ";
private TextView refTitle, refSub, refUrl;
private LinearLayoutManager linearLayoutManager;
private List<Refs> refList;
private RecyclerView myrv;
private RecyclerView.Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voter_information);
refTitle = findViewById(R.id.ref_title); //Initialize TextViews
refSub = findViewById(R.id.ref_sub);
myrv = findViewById(R.id.ref_rv); //Initialize recycler view
refList = new ArrayList<>();
adapter = new RefRvAdapter(refList, getApplicationContext());
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
myrv.setHasFixedSize(true);
myrv.setLayoutManager(linearLayoutManager);
myrv.setAdapter(adapter);
TextView pdf_url = findViewById(R.id.ref_url);
pdf_url.setClickable(true);
pdf_url.setMovementMethod(LinkMovementMethod.getInstance());
getData();
}
private void getData(){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("contests");
String pdf_url = array.getJSONObject(0).getString("referendumUrl") ;
for(int i = 0; i < array.length(); i++){
JSONObject jo = array.getJSONObject(i);
Refs refs = new Refs(jo.getString("referendumTitle"),
jo.getString("referendumSubtitle"),
jo.getString("referendumUrl"));
refList.add(refs);
}
adapter = new RefRvAdapter(refList, getApplicationContext());
myrv.setAdapter(adapter);
} catch(JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
这是我的适配器代码 class:
public class RefRvAdapter extends RecyclerView.Adapter<RefRvAdapter.MyViewHolder>{
private Context context;
private List<Refs> refsList;
public RefRvAdapter(){}
public RefRvAdapter(List<Refs> refList, Context context){
this.refsList = refList;
this.context = context;
}
@NonNull
@Override
public RefRvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view;
LayoutInflater mInflater = LayoutInflater.from(context);
view = mInflater.inflate(R.layout.ref_card, viewGroup, false);
final MyViewHolder viewHolder = new MyViewHolder(view);
viewHolder.view_containers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (context, SingleReferendum.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("referendumTitle", refsList.get(viewHolder.getAdapterPosition()).getRefTitle());
i.putExtra("referendumSubtitle", refsList.get(viewHolder.getAdapterPosition()).getRefSub());
i.putExtra("referendumUrl", refsList.get(viewHolder.getAdapterPosition()).getRefUrl());
context.startActivity(i);
}
});
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RefRvAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.refTitle.setText(refsList.get(i).getRefTitle());
myViewHolder.refSub.setText(refsList.get(i).getRefSub());
myViewHolder.refUrl.setText(refsList.get(i).getRefUrl());
}
@Override
public int getItemCount() {
return refsList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView refTitle, refSub, refUrl;
LinearLayout view_containers;
public MyViewHolder(View itemView){
super(itemView);
refTitle = itemView.findViewById(R.id.ref_title);
refSub = itemView.findViewById(R.id.ref_sub);
refUrl = itemView.findViewById(R.id.ref_url);
view_containers = itemView.findViewById(R.id.view_container_ref);
}
}
}
该应用程序可以很好地解析所有数据。我只想使 url 可点击。当用户单击它时,pdf 打开。我是在 RecyclerView 适配器 class 中添加代码,还是在主 activity 中添加代码?
答案:像这样编辑 onBindViewHolder 方法。
@Override
public void onBindViewHolder(@NonNull final RefRvAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.refTitle.setText(refsList.get(i).getRefTitle());
myViewHolder.refSub.setText(refsList.get(i).getRefSub());
myViewHolder.refUrl.setText(refsList.get(i).getRefUrl());
myViewHolder.view_containers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (context, SingleReferendum.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("referendumTitle", refsList.get(myViewHolder.getAdapterPosition()).getRefTitle());
i.putExtra("referendumSubtitle", refsList.get(myViewHolder.getAdapterPosition()).getRefSub());
i.putExtra("referendumUrl", refsList.get(myViewHolder.getAdapterPosition()).getRefUrl());
context.startActivity(i);
}
});
myViewHolder.refUrl.setOnClickListener(new View.OnClickListener(){
@Override //handle pdf clicks
public void onClick(View view) {
String refUrl = refsList.get(myViewHolder.getAdapterPosition()).getRefUrl();
Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse(refUrl));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
如果我没有误解你的意思,只需将方法:onCreateViewHolder
和onBindViewHolder
修改为:
@NonNull
@Override
public RefRvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view;
LayoutInflater mInflater = LayoutInflater.from(context);
view = mInflater.inflate(R.layout.ref_card, viewGroup, false);
final MyViewHolder viewHolder = new MyViewHolder(view);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RefRvAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.refTitle.setText(refsList.get(i).getRefTitle());
myViewHolder.refSub.setText(refsList.get(i).getRefSub());
myViewHolder.refUrl.setText(refsList.get(i).getRefUrl());
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (context, SingleReferendum.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("referendumTitle", refsList.get(myViewHolder.getAdapterPosition()).getRefTitle());
i.putExtra("referendumSubtitle", refsList.get(myViewHolder.getAdapterPosition()).getRefSub());
i.putExtra("referendumUrl", refsList.get(myViewHolder.getAdapterPosition()).getRefUrl());
context.startActivity(i);
}
}
myViewHolder.refUrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String refUrl = refsList.get(myViewHolder.getAdapterPosition()).getRefUrl();
//open pdf here.
}
});
}
试用一下,如果可以反馈。
问题的标题没有完全解释我的问题,所以请允许我详细说明。我有一个应用程序可以将来自 Google Civic API 的 JSON 数据解析为 recyclerview。一个特定的字段是一个 TextView,它将 PDF url 存储在一个字符串中。如何设置 TextView 使其可点击并最好在应用程序中打开 PDF。我查找了解决方案,并尝试使用 WebView 和 PDFReader 等,但我找不到任何提到如何从 recyclerview 执行此操作的解决方案,因此我不确定将此代码放在哪里。由于我正在单击 recyclerView 中的一个项目,我想我可能需要在 recyclerview 适配器中添加代码而不是主要 class.
这是主要代码 class:这个 class 处理 JSON 解析
public class VoterInformation extends AppCompatActivity {
private static final String url = "https://www.googleapis.com/civicinfo/v2/voterinfo?address=2613+Irvington+Ave+San+Bernardino+CA+92407&electionId=2000&officialOnly=false&returnAllAvailableData=false&fields=contests(referendumBallotResponses%2CreferendumBrief%2CreferendumConStatement%2CreferendumEffectOfAbstain%2CreferendumPassageThreshold%2CreferendumProStatement%2CreferendumSubtitle%2CreferendumText%2CreferendumTitle%2CreferendumUrl)&key=AIzaSyB1B5mEKHK8PHDiNcGQ5ZU3fPIH9KWxAcQ";
private TextView refTitle, refSub, refUrl;
private LinearLayoutManager linearLayoutManager;
private List<Refs> refList;
private RecyclerView myrv;
private RecyclerView.Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voter_information);
refTitle = findViewById(R.id.ref_title); //Initialize TextViews
refSub = findViewById(R.id.ref_sub);
myrv = findViewById(R.id.ref_rv); //Initialize recycler view
refList = new ArrayList<>();
adapter = new RefRvAdapter(refList, getApplicationContext());
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
myrv.setHasFixedSize(true);
myrv.setLayoutManager(linearLayoutManager);
myrv.setAdapter(adapter);
TextView pdf_url = findViewById(R.id.ref_url);
pdf_url.setClickable(true);
pdf_url.setMovementMethod(LinkMovementMethod.getInstance());
getData();
}
private void getData(){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("contests");
String pdf_url = array.getJSONObject(0).getString("referendumUrl") ;
for(int i = 0; i < array.length(); i++){
JSONObject jo = array.getJSONObject(i);
Refs refs = new Refs(jo.getString("referendumTitle"),
jo.getString("referendumSubtitle"),
jo.getString("referendumUrl"));
refList.add(refs);
}
adapter = new RefRvAdapter(refList, getApplicationContext());
myrv.setAdapter(adapter);
} catch(JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
这是我的适配器代码 class:
public class RefRvAdapter extends RecyclerView.Adapter<RefRvAdapter.MyViewHolder>{
private Context context;
private List<Refs> refsList;
public RefRvAdapter(){}
public RefRvAdapter(List<Refs> refList, Context context){
this.refsList = refList;
this.context = context;
}
@NonNull
@Override
public RefRvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view;
LayoutInflater mInflater = LayoutInflater.from(context);
view = mInflater.inflate(R.layout.ref_card, viewGroup, false);
final MyViewHolder viewHolder = new MyViewHolder(view);
viewHolder.view_containers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (context, SingleReferendum.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("referendumTitle", refsList.get(viewHolder.getAdapterPosition()).getRefTitle());
i.putExtra("referendumSubtitle", refsList.get(viewHolder.getAdapterPosition()).getRefSub());
i.putExtra("referendumUrl", refsList.get(viewHolder.getAdapterPosition()).getRefUrl());
context.startActivity(i);
}
});
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RefRvAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.refTitle.setText(refsList.get(i).getRefTitle());
myViewHolder.refSub.setText(refsList.get(i).getRefSub());
myViewHolder.refUrl.setText(refsList.get(i).getRefUrl());
}
@Override
public int getItemCount() {
return refsList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView refTitle, refSub, refUrl;
LinearLayout view_containers;
public MyViewHolder(View itemView){
super(itemView);
refTitle = itemView.findViewById(R.id.ref_title);
refSub = itemView.findViewById(R.id.ref_sub);
refUrl = itemView.findViewById(R.id.ref_url);
view_containers = itemView.findViewById(R.id.view_container_ref);
}
}
}
该应用程序可以很好地解析所有数据。我只想使 url 可点击。当用户单击它时,pdf 打开。我是在 RecyclerView 适配器 class 中添加代码,还是在主 activity 中添加代码?
答案:像这样编辑 onBindViewHolder 方法。
@Override
public void onBindViewHolder(@NonNull final RefRvAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.refTitle.setText(refsList.get(i).getRefTitle());
myViewHolder.refSub.setText(refsList.get(i).getRefSub());
myViewHolder.refUrl.setText(refsList.get(i).getRefUrl());
myViewHolder.view_containers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (context, SingleReferendum.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("referendumTitle", refsList.get(myViewHolder.getAdapterPosition()).getRefTitle());
i.putExtra("referendumSubtitle", refsList.get(myViewHolder.getAdapterPosition()).getRefSub());
i.putExtra("referendumUrl", refsList.get(myViewHolder.getAdapterPosition()).getRefUrl());
context.startActivity(i);
}
});
myViewHolder.refUrl.setOnClickListener(new View.OnClickListener(){
@Override //handle pdf clicks
public void onClick(View view) {
String refUrl = refsList.get(myViewHolder.getAdapterPosition()).getRefUrl();
Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse(refUrl));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
如果我没有误解你的意思,只需将方法:onCreateViewHolder
和onBindViewHolder
修改为:
@NonNull
@Override
public RefRvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view;
LayoutInflater mInflater = LayoutInflater.from(context);
view = mInflater.inflate(R.layout.ref_card, viewGroup, false);
final MyViewHolder viewHolder = new MyViewHolder(view);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RefRvAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.refTitle.setText(refsList.get(i).getRefTitle());
myViewHolder.refSub.setText(refsList.get(i).getRefSub());
myViewHolder.refUrl.setText(refsList.get(i).getRefUrl());
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (context, SingleReferendum.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("referendumTitle", refsList.get(myViewHolder.getAdapterPosition()).getRefTitle());
i.putExtra("referendumSubtitle", refsList.get(myViewHolder.getAdapterPosition()).getRefSub());
i.putExtra("referendumUrl", refsList.get(myViewHolder.getAdapterPosition()).getRefUrl());
context.startActivity(i);
}
}
myViewHolder.refUrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String refUrl = refsList.get(myViewHolder.getAdapterPosition()).getRefUrl();
//open pdf here.
}
});
}
试用一下,如果可以反馈。