0
我想要有卡片的回收商視圖。 在我想要放置iframe的webView的卡內。 基本上,我想要一個使用iframe和回收站視圖的視頻列表。 我已經做了以下,並在我的綁定函數有問題。 輸出沒有顯示任何內容。在卡片回收商視圖內添加網絡視圖
主要活動
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private recycle_adapter recycleAdapter;
private List<url> urlList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
urlList = new ArrayList<>();
adapter = new recycle_adapter(this,urlList);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
recyclerView.setHasFixedSize(true);
// use a linear layout manager
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
prepareurl();
}
private void prepareurl(){
String[] url_list = new String[]{"<html><body><iframe class=\\\"youtube-player\\\" type=\\\"text/html\\\" width=\\\"400\\\"" +
" height=\\\"200\\\" src=\\\"http://www.youtube.com/embed/bIPcobKMB94\\\" frameborder=\\\"0\\\"></body></html>"};
url url=new url(url_list[0]);
urlList.add(url);
url=new url(url_list[0]);
urlList.add(url);
url=new url(url_list[0]);
urlList.add(url);
url=new url(url_list[0]);
urlList.add(url);
adapter.notifyDataSetChanged();
}
recycle_adapter
public class recycle_adapter extends RecyclerView.Adapter<recycle_adapter.MyViewHolder>{
private Context mContext;
private List<url> urlList;
public recycle_adapter(Context mContext, List<url> urlList) {
this.mContext = mContext;
this.urlList = urlList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
url url=urlList.get(position);
holder.webView.loadData(url.getUrl(),"text/html", "utf-8");
}
@Override
public int getItemCount() {
return 4;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
WebView webView;
public MyViewHolder(View itemView) {
super(itemView);
this.webView = (WebView) itemView.findViewById(R.id.web);
}
}
}
URL類
public class url {
private String urls;
public url() {
}
public url(String urls) {
this.urls = urls;
}
public String getUrl() {
return urls;
}
public void setUrl(String urls) {
this.urls = urls;
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.recycle.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</RelativeLayout>
card.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/card"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#456345">
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="200dp" />
</android.support.v7.widget.CardView>
</LinearLayout>