Android - 当我滚出视图时,在ListView中使用WebView的Youtube视频将被删除

时间:2016-02-24 22:22:26

标签: java android webview

我有一个ListView,并在单元格中使用WebView显示youtube视频,但是当我滚动离开youtube视频时,当我向后滚动时,youtube视频会从单元格中删除。

这是我正在使用的代码(Custom ArrayAdapter):

在getView()方法中:

    import android.app.Activity;
    import android.content.Context;
    import android.content.res.Configuration;
    import android.graphics.Color;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    import java.util.ArrayList;

    /**
     * Created by xxx on 21/02/16.
     */
    public class MessageArrayAdapter  extends ArrayAdapter<MessageObj> {


        Context context;
        int layoutResourceId;
        public ArrayList<MessageObj> data = null;

        public MessageArrayAdapter(Context context, int layoutResourceId, ArrayList<MessageObj> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        //WeatherHolder holder = null;
        MessageHolder m = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            //TextView tv = (TextView)row.findViewById(R.id.threadTitle);
            //t = new ThreadObj(tv.getText().toString());

            m = new MessageHolder();
            //holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            m.messageUsernameTV = (TextView)row.findViewById(R.id.messageUsername);
            m.messageDateTV = (TextView)row.findViewById(R.id.messageDate);
            m.userImageLL = (LinearLayout)row.findViewById(R.id.userImage);
            m.messageTV = (TextView)row.findViewById(R.id.message);
            m.messageFlagIV = (ImageView)row.findViewById(R.id.messageFlag);
            m.messageModIV = (ImageView)row.findViewById(R.id.messageMod);
            //m.youtubeWV = (WebView)row.findViewById(R.id.youtubeWV);
            m.youtubeLL = (LinearLayout)row.findViewById(R.id.youtubeLL);

            row.setTag(m);
        }
        else
        {
            m = (MessageHolder)row.getTag();
        }

        MessageObj message = data.get(position);
        m.messageUsernameTV.setText(message.firstname + " " + message.lastname);
        m.messageDateTV.setText(message.date);
        m.messageTV.setText(message.message);

        m.youtubeLL.removeAllViews();

        if(message.youtube.length() > 0){

            String frameVideo = "<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head><body><iframe width='100%'  src='https://www.youtube.com/embed/" + message.youtube + "' frameborder='0' allowfullscreen></iframe></body></html>";

            WebView youtubeWV = new WebView(context);


            youtubeWV.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
            });
            WebSettings webSettings = youtubeWV.getSettings();
            webSettings.setJavaScriptEnabled(true);
            youtubeWV.loadData(frameVideo, "text/html", "utf-8");
            m.youtubeLL.addView(youtubeWV);
        }else{

            m.youtubeLL.setVisibility(View.GONE);
            //m.youtubeWV.setVisibility(View.GONE);
        }



        if(message.countFlags > 0){
            //t.threadFlagIV.setImageResource(R.drawable.flag2);
        }else{
            //t.threadFlagIV.setImageResource(R.drawable.flag1);
        }

        if(message.isMod){
            //t.threadFlagIV.setImageResource(R.drawable.flag2);
        }else{
            //t.threadFlagIV.setImageResource(R.drawable.flag1);
        }

        if(message.userImageId == 1){
            m.userImageLL.setBackgroundColor(Color.parseColor("#000000"));
        }

        return row;
        }

        static class MessageHolder
        {
        //ImageView imgIcon;
        TextView messageUsernameTV;
        TextView messageDateTV;
        LinearLayout userImageLL;
        TextView messageTV;
        ImageView messageFlagIV;
        ImageView messageModIV;
        //WebView youtubeWV;
        LinearLayout youtubeLL;
        }
    }

如果您希望我添加更多代码,我会这样做。

0 个答案:

没有答案