recyclerview中的webview无法正常工作

时间:2016-08-08 12:52:57

标签: android webview android-recyclerview android-webview

我在recyclerview中有3个类型的项目视图,最后一个包含完整的webview。当我点击webview(这是一行recyclerview)时,Recyclerview会向上滚动。我该如何解决这个问题。

这是我的布局,包含webview

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/webView">
</WebView>

适配器:

    public class HomePageAdapter extends       RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private String htmlContent;
    private ArrayList<Slider> homePageSliders;
    private ArrayList<HomePageItem> homePageItems;

    private final int VIEW_SLIDER = 0;
    private final int VIEW_HOMEPAGEITEM = 1;
    private final int VIEW_HOMEPAGEHTML = 2;


    private OnItemClickCallBack<HomePageItem> callBack;

    public HomePageAdapter(ArrayList<Slider> homePageSliders, ArrayList<HomePageItem> homePageItems, String htmlContent, OnItemClickCallBack<HomePageItem> callBack)
    {
        this.homePageSliders = homePageSliders;
        this.homePageItems   = homePageItems;
        this.htmlContent     = htmlContent;
        this.callBack        = callBack;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(viewType == VIEW_SLIDER) {
            return new HomePageSliderHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_homepage_slider, parent, false));
        }
        else if(viewType == VIEW_HOMEPAGEITEM)
        {
            return new HomePageItemHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_homepage_item, parent, false));

        }
        else
        {
            return new HomePageHtmlContentHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_homepage_html, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof HomePageSliderHolder)
        {
            HomePageSliderHolder viewHolder = (HomePageSliderHolder) holder;
            viewHolder.setViewPagerAdapter(this.homePageSliders);
        }
        else if(holder instanceof HomePageItemHolder)
        {
            HomePageItemHolder viewHolder = (HomePageItemHolder) holder;

            int newPosition = position -1;
            final HomePageItem homePageItem = this.homePageItems.get(newPosition);

            viewHolder.setImagePath(homePageItem);
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.onItemClick(homePageItem);
                }
            });
        }
        else
        {
            HomePageHtmlContentHolder viewHolder = (HomePageHtmlContentHolder) holder;
            viewHolder.setHtml(htmlContent);
            viewHolder.setWebViewClickListener();
        }
    }

    @Override
    public int getItemCount() {
        return 1 + 1 + homePageItems.size();
    }

    @Override
    public int getItemViewType(int position) {

        if(position == 0)
        {
            return VIEW_SLIDER;
        }
        else if(position < homePageItems.size())
        {
            return VIEW_HOMEPAGEITEM;
        }
        else
        {
            return VIEW_HOMEPAGEHTML;
        }

    }
}

Webview点击监听器:

public void setWebViewClickListener()
    {
        this.webView.setWebViewClient(new WebViewClient(){

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Crashlytics.log(Log.ASSERT, "url : ", url);
                return true;
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

当我调查html响应时,我意识到重定向网址中没有任何主机(例如google://)。然后我通过包含主机替换它,它就像一个魅力。