安卓 webview作为recycleview item显示不全解决办法



import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;


import java.util.List;

/**
 * 导航内容直播列表
 */
public class NavContentLiveListAdapter extends RecyclerView.Adapter {

    private List listData;
    private ItemListener.JsonObject itemSelectedListener;
    private int column = 1;
    private Context mContext;

    public NavContentLiveListAdapter(Context context, ItemListener.JsonObject itemSelectedListener, int column) {
        this.itemSelectedListener = itemSelectedListener;
        this.column = column;
        this.mContext = context;
    }

    public void setData(List data) {
        this.listData = data;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == 2) {//2直播
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_playback_list_view, parent, false);
            return new NavListLiveViewHolder(view, itemSelectedListener);
        } else {//1图文
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            WebView mView = new WebView(parent.getContext());
            mView.setLayoutParams(lp);
            return new RichTextViewHolder(mView, itemSelectedListener);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return listData.get(position).get("type").getAsInt();
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        JsonObject item = listData.get(position);
        if (holder instanceof NavListLiveViewHolder)
            ((NavListLiveViewHolder) holder).bind(item);
        else
            ((RichTextViewHolder) holder).bind(item);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List payloads) {
        onBindViewHolder(holder, position);
    }

    @Override
    public int getItemCount() {
        if (listData != null) {
            return listData.size();
        }
        return 0;
    }

    //推荐视频view holder
    class NavListLiveViewHolder extends RecyclerView.ViewHolder {

        private ItemListener.JsonObject itemSelectedListener;
        private ImageView photosIcon;
        private TextView itemShowState, itemShowName;

        NavListLiveViewHolder(View itemView, ItemListener.JsonObject itemSelectedListener) {
            super(itemView);
            photosIcon = itemView.findViewById(R.id.lv_show_cover);
            itemShowState = itemView.findViewById(R.id.lv_show_state);
            itemShowName = itemView.findViewById(R.id.lv_show_name);
            this.itemSelectedListener = itemSelectedListener;
        }

        @SuppressLint("SetTextI18n")
        void bind(final JsonObject jsonObject) {
            JsonObject mediaItem = (JsonObject) jsonObject.get("live");
            Glide.with(mContext)
                    .load(mediaItem.get("cover_img_url").getAsString())
                    .apply(ImageUtils.createOption(4))
                    .into(photosIcon);
            photosIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemSelectedListener != null) {
                        itemSelectedListener.clickItem(mediaItem);
                    }
                }
            });
            itemShowName.setText(mediaItem.get("title").getAsString());
            if (mediaItem.get("status").getAsInt() == 2) {
                itemShowState.setText("LIVE·直播");
                itemShowState.setBackground(mContext.getDrawable(R.drawable.bg_live_state));
            } else if (mediaItem.get("status").getAsInt() == 1) {
                itemShowState.setText("预告");
                itemShowState.setBackground(mContext.getDrawable(R.drawable.bg_live_state2));
            } else if (mediaItem.get("status").getAsInt() == 3) {
                itemShowState.setText("回放");
                itemShowState.setBackground(mContext.getDrawable(R.drawable.bg_live_state3));
            }
        }
    }

    //富文本view holder
    class RichTextViewHolder extends RecyclerView.ViewHolder {

        private JsonObject jsonObject;
        private WebView mWebView;

        RichTextViewHolder(View itemView, ItemListener.JsonObject itemSelectedListener) {
            super(itemView);
            if (itemView instanceof WebView) {
                mWebView = (WebView) itemView;
                mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
                WebSettings mSetting = mWebView.getSettings();
                mSetting.setJavaScriptEnabled(true);
            }
        }

        @SuppressLint("SetTextI18n")
        void bind(final JsonObject mediaItem) {
            this.jsonObject = mediaItem;
            mWebView.loadDataWithBaseURL(null,
                    "" + jsonObject.get("content").getAsString(),
                    "text/html", "utf-8", null);
            mWebView.loadUrl("javascript:App.resize(document.body.getBoundingClientRect().height)");
        }
    }
}

 
  

 

你可能感兴趣的:(安卓界面)