当webView作为recyclerView的一个布局,导致点击webView返回顶部的问题。

时间:2022-11-17 23:03:54
public class ArticleAdapter extends RecyclerView.Adapter {

    public static final int HEADER_VIEW = 111;
    public static final int ITEM_VIEW = 222;

    private String mWebUrl;
    private List<String> mList = new ArrayList<>();

    public String getWebUrl() {
        return mWebUrl;
    }

    public void setWebUrl(String webUrl) {
        mWebUrl = webUrl;
    }

    public List<String> getList() {
        return mList;
    }

    public void setList(List<String> list) {
        if (list != null) {
            mList.clear();
            addList(list);
        }
    }

    public void addList(List<String> list) {
        if (list != null) {
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEADER_VIEW;
        } else {
            return ITEM_VIEW;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEADER_VIEW) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_article_header, parent, false);
            HeaderViewHolder holder = new HeaderViewHolder(view, parent.getContext());
            return holder;
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_article_normal, parent, false);
            return new ItemViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder) {
            ((HeaderViewHolder) holder).bindData(mWebUrl);
        } else {
            ((ItemViewHolder) holder).binData(mList.get(position - 1));
        }
    }

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

    /**
     * 文章详情
     */
    class HeaderViewHolder extends RecyclerView.ViewHolder {

        private View mView;
        private Context mContext;
        private WebViewClient mWebViewClient;

        public HeaderViewHolder(View itemView, Context context) {
            super(itemView);
            mView = itemView;
            mContext = context;
        }

        public void bindData(String webUrl) {
            mView.requestFocus();
            WebView webView = (WebView) mView.findViewById(R.id.webView);
            setWebView(webUrl, webView);
        }
    }

    /**
     * 评论item
     */
    class ItemViewHolder extends RecyclerView.ViewHolder {

        private View mView;

        public ItemViewHolder(View itemView) {
            super(itemView);
            this.mView = itemView;
        }

        public void binData(String string) {
            TextView textView = (TextView) mView.findViewById(R.id.tv_title);
            textView.setText(string);
        }
    }

}
 

以上是代码,红色的为解决办法,原因是因为第一次点击需要获取触摸,第二次点击就不会返回顶部,修改自 webView in recyclerView