在卡片回收站视图中添加Web视图

时间:2016-11-30 08:42:57

标签: android iframe webview android-recyclerview

我想要一张带卡片的回收者视图。 在卡片中,我想为iframe放置一个webView。 基本上,我希望使用iframe和recycler视图获得视频列表。 我做了以下操作,我的绑定功能存在问题。 输出没有显示任何内容。

主要活动

blocks = pygame.sprite.RenderPlain(Singlescoop_pink()) 
sprites = pygame.sprite.RenderPlain(tub, *blocks)

recycle_adapter

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();
    }

网址类

    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);
        }
    }

}

activity_main.xml中

    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;
    }

}

card.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>

1 个答案:

答案 0 :(得分:0)

尝试更改这样的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);
    adapter.notifyItemChanged(urlList.size());

    url=new url(url_list[0]);
    urlList.add(url);
    adapter.notifyItemChanged(urlList.size());

    url=new url(url_list[0]);
    urlList.add(url);
    adapter.notifyItemChanged(urlList.size());

    url=new url(url_list[0]);
    urlList.add(url);
    adapter.notifyItemChanged(urlList.size());
}