我在代码中实现此功能时遇到了问题
我想要一些东西,当我点击该项目时,我会获得该网址并在Activity
中打开一个新的WebView
,谢谢。
Beaches.java
package com.novela.minha.novela;
public class Beaches {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Beaches() {
}
public String url;
}
MyAdapter.java
package com.novela.minha.novela;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
Context c;
ArrayList<Beaches> beaches;
public MyAdapter(Context c, ArrayList<Beaches> beaches){
this.c= c;
this.beaches=beaches;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.card,parent,false);
MyHolder holder= new MyHolder(v);
return holder;
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.nameTxt.setText(beaches.get(position).getName());
PicassoClient.downloadimg(c, beaches.get(position).getUrl(),holder.img);
}
@Override
public int getItemCount() {
return beaches.size();
}
}
NoticiasFragment.java
package com.novela.minha.novela;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import com.firebase.client.Firebase;
import java.util.ArrayList;
import java.util.List;
public class NoticiasFragment extends Fragment {
final static String DB_URL = "https:/myendrendnd.firebaseio.com/";
EditText nameeditText, urleditText;
Button btnsave;
RecyclerView recyclerView;
FirebaseClient firebaseClient;
public NoticiasFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_noticias, container, false);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
recyclerView = view.findViewById(R.id.RecyclerNoticias);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
firebaseClient = new FirebaseClient(getActivity(), DB_URL, recyclerView);
firebaseClient.refreshdata();
return recyclerView;
}
private void displayDialog()
{
Dialog d= new Dialog(getActivity());
d.setTitle("SaveData");
d.setContentView(R.layout.customdialog_layout);
nameeditText= (EditText)d.findViewById(R.id.nameEditText);
urleditText=(EditText)d.findViewById(R.id.urlEditText);
btnsave= (Button)d.findViewById(R.id.saveBtn);
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
firebaseClient.savedata(nameeditText.getText().toString(),urleditText.getText().toString());
nameeditText.setText("");
urleditText.setText("");
}
});
d.show();
}
}
FirebaseCliete.java
package com.example.admin.firebaserecyclerviewexample;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.novela.minha.novela.Beaches;
import com.novela.minha.novela.MyAdapter;
import java.util.ArrayList;
public class FirebaseClient {
Context c;
String DB_URL;
RecyclerView recyclerView;
Firebase firebase;
ArrayList<Beaches> beaches= new ArrayList<>();
MyAdapter adapter;
public FirebaseClient(Context c, String DB_URL, RecyclerView recyclerView)
{
this.c= c;
this.DB_URL= DB_URL;
this.recyclerView= recyclerView;
Firebase.setAndroidContext(c);
firebase=new Firebase(DB_URL);
}
public void savedata (String name, String texto, String url)
{
Beaches b= new Beaches();
b.setName(name);
b.setUrl(url);
b.setTexto(texto);
firebase.child("Beaches").push().setValue(b);
}
public void refreshdata()
{
firebase.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
getupdates(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
getupdates(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
public void getupdates(DataSnapshot dataSnapshot){
beaches.clear();
for(DataSnapshot ds :dataSnapshot.getChildren()){
Beaches b= new Beaches();
b.setName(ds.getValue(Beaches.class).getName());
b.setTexto(ds.getValue(Beaches.class).getTexto());
b.setUrl(ds.getValue(Beaches.class).getUrl());
beaches.add(b);
}
if(beaches.size()>0)
{
adapter=new MyAdapter(c, beaches);
recyclerView.setAdapter(adapter);
}else
{
Toast.makeText(c, "No data", Toast.LENGTH_SHORT).show();
}
}
}
MainActivity.java
package com.novela.minha.novela;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.app.Dialog;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText nameeditText,urleditText,textoeditText;
Button btnsave;
com.example.admin.firebaserecyclerviewexample.FirebaseClient firebaseClient;
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
displayDialog();
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
private void displayDialog()
{
Dialog d= new Dialog(this);
d.setTitle("SaveData");
d.setContentView(R.layout.customdialog_layout);
nameeditText= (EditText)d.findViewById(R.id.nameEditText);
textoeditText= (EditText) d.findViewById(R.id.textoEditText);
urleditText=(EditText)d.findViewById(R.id.urlEditText);
btnsave= (Button)d.findViewById(R.id.saveBtn);
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
firebaseClient.savedata(nameeditText.getText().toString(),textoeditText.getText().toString(),urleditText.getText().toString());
nameeditText.setText("");
textoeditText.setText("");
urleditText.setText("");
}
});
d.show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
NoticiasFragment tab1 = new NoticiasFragment();
return tab1;
case 1:
ResumoFragment tab2 = new ResumoFragment();
return tab2;
case 2:
MaisFragment tab3 = new MaisFragment();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle (int position) {
switch (position) {
case 0:
return "NOTICIAS";
case 1:
return "RESUMO";
case 3:
return "MAIS";
}
return null;
}
}
}
MyHolder.Java
package com.novela.minha.novela;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MyHolder extends RecyclerView.ViewHolder {
TextView nameTxt, textoTxt;
ImageView img;
public MyHolder(View itemView) {
super(itemView);
nameTxt= (TextView) itemView.findViewById(R.id.nameTxt);
img=(ImageView) itemView.findViewById(R.id.imageView3);
textoTxt = (TextView) itemView.findViewById(R.id.textoTxt);
}
}
PicassoClient.java
package com.novela.minha.novela;
import android.content.Context;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class PicassoClient {
public static void downloadimg(Context c, String url, ImageView img)
{
if (url!=null && url.length()>0)
{
Picasso.with(c).load(url).placeholder(R.drawable.placeholder).into(img);
}else
{
Picasso.with(c).load(R.drawable.placeholder).into(img);
}
}
}
我想播放在webview中密封的项目的网址
NewsWebView.java
package com.example.minhanovela;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class NewsWebView extends AppCompatActivity {
WebView myWebView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_web_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
progressBar = (ProgressBar)findViewById(R.id.pro);
myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
Bundle b = getIntent().getExtras();
String id = b.getString("id");
myWebView.loadUrl(id);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
setTitle("Loading...");
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
setTitle(view.getTitle());
}
});
}
}
答案 0 :(得分:0)
这是一个基本的例子
static class ViewHolder extends RecyclerView.ViewHolder {
private TextView name;
private ImageView image;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
image = (ImageView) itemView.findViewById(R.id.image);
}
public void bind(final ContentItem item, final OnItemClickListener listener) {
name.setText(item.name);
Picasso.with(itemView.getContext()).load(item.imageUrl).into(image);
itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
//Here's where you get the url from the item like so
Log.d("URL", "The url from the item is: " + item.getUrl)
}
});
}
}`
你可以看到我如何在那里集成Picasso和点击监听器,我使用Log类输出url,你可以使用intent将url发送到其他活动