关于RecyclerView中的WebView?

时间:2015-08-07 07:21:11

标签: android webview

我在RecyclerView的第一项中使用WebView,webview加载包含imagevideo的网址,当我点击imagevideo时,recyclerview将滚动到顶部,webview将继续加载,视频将播放,我如何解决它?

一些代码:

public class NewsDetailAdapter extends PageRecyclerAdapter<NewsDetailInfo, ViewHolder> {
private static final boolean DEBUG = true;

public static final int VIEW_TYPE_WEB                = 1;
public static final int VIEW_TYPE_COMMENT_TITLE      = 2;
public static final int VIEW_TYPE_COMMENT            = 3;

private XWebView mWebView;
private OnItemClickListener mItemClickListener;

public NewsDetailAdapter(Context context, XWebView webView, List<NewsDetailInfo> data, OnItemClickListener listener, OnErrorClickListener errorListener) {
    super(context, data, errorListener);
    mWebView = webView;
    mItemClickListener = listener;
}

@Override
public int getListItemViewType(int position) {
    return getItem(position).itemType;
}

@Override
public ViewHolder onCreateHolder(LayoutInflater inflater, ViewGroup parent,
        int viewType) {
    switch (viewType) {
    case VIEW_TYPE_WEB:
        return new WebViewHolder(inflater.inflate(R.layout.layout_webview, parent, false));
    case VIEW_TYPE_COMMENT_TITLE:
        return new SimpleTextHolder(inflater.inflate(R.layout.layout_comment_title, parent, false));
    case VIEW_TYPE_COMMENT:
        return new ListItemHolder(inflater.inflate(R.layout.list_item_comment, parent, false));
    }
    return null;
}

@Override
public void onBindHolder(ViewHolder viewHolder, final int position) {
    if (viewHolder instanceof SimpleTextHolder) {
        SimpleTextHolder holder = (SimpleTextHolder) viewHolder;
        holder.mText.setText(getItem(position).text);
    } else if (viewHolder instanceof ListItemHolder) {
        ListItemHolder holder = (ListItemHolder) viewHolder;
        holder.mUsername.setText(TextUtils.isEmpty(getItem(position).user.username) ? "匿名" : getItem(position).user.username);
        holder.mCommentContent.setText(getItem(position).content);
        holder.mPublishTime.setText(TimeUtils.parseToHumnanizeTime(getItem(position).publishTime));

        holder.itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(getItem(position));
                }
            }
        });
    }
}

class WebViewHolder extends ViewHolder {

    public WebViewHolder(View itemView) {
        super(itemView);
        FrameLayout frameLayout = (FrameLayout) itemView.findViewById(R.id.web_view_container);
        frameLayout.addView(mWebView);
        **itemView.requestFocus();**  // add this code will resolve it;
    }
}

class SimpleTextHolder extends ViewHolder {

    TextView mText;

    public SimpleTextHolder(View itemView) {
        super(itemView);
        mText = (TextView) itemView.findViewById(R.id.title);
    }

}

class ListItemHolder extends ViewHolder {
    ImageView mImageView;
    TextView mUsername;
    TextView mCommentContent;
    TextView mPublishTime;

    public ListItemHolder(View itemView) {
        super(itemView);
        mImageView = (ImageView) itemView.findViewById(R.id.portrait);
        mUsername = (TextView) itemView.findViewById(R.id.username);
        mCommentContent = (TextView) itemView.findViewById(R.id.comment_content);
        mPublishTime = (TextView) itemView.findViewById(R.id.publish_time);
    }
}

public interface OnItemClickListener {
    void onItemClick(NewsDetailInfo info);
}

}

0 个答案:

没有答案