listview中的WebView不断重新加载

2017-01-01 09:09
package com.jsy.xuezhuli.main.personalcenter;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html.ImageGetter;
import android.text.Editable;
import android.text.Html.TagHandler;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jsy.xuezhuli.customview.CusListView;
import com.jsy.xuezhuli.customview.MyWebView;
import com.jsy.xuezhuli.main.JSYApplication;
import com.jsy.xuezhuli.main.R;
import com.jsy.xuezhuli.po.qiuzhi.AnswerItem;
import com.jsy.xuezhuli.po.qiuzhi.MyHtmlTagHandler;
import com.jsy.xuezhuli.po.qiuzhi.QuestionIndexItem;
import com.jsy.xuezhuli.po.qiuzhi.RecommentIndexAnswer;
import com.jsy.xuezhuli.po.qiuzhi.RecommentIndexItem;
import com.jsy.xuezhuli.po.qiuzhi.WatchedEntityIndexItem;
import com.jsy.xuezhuli.po.qiuzhi.Watcher;
import com.jsy.xuezhuli.utils.ACache;
import com.jsy.xuezhuli.utils.BaseUtils;
import com.jsy.xuezhuli.utils.Constant;
import com.jsy.xuezhuli.utils.ConstantUrl;
import com.jsy.xuezhuli.utils.DestroyInterface;
import com.jsy.xuezhuli.utils.HtmlImageGetter;
import com.jsy.xuezhuli.utils.StringUtils;
import com.jsy.xuezhuli.utils.adapter.ViewHolder;
import com.lidroid.xutils.BitmapUtils;

public class QiuZhiSuggestAnswerListAdapter<T> extends BaseAdapter implements Watcher,DestroyInterface{
private RecommentIndexItem question;
private Context mContext;
private List<T> mData;
private BitmapUtils bitmap;
private SimpleDateFormat dateFormat;
private Date today;
private String str_todaytime;
private String[] str_todaytimes;
private String mainURL;
private CusListView listView;
private Drawable defaultDrawable;
private final static String TAG="QiuZhiSuggestAnswerListAdapter";
public QiuZhiSuggestAnswerListAdapter(Context mContext,CusListView listView){
this.mContext = mContext;
this.listView = listView;
bitmap = JSYApplication.getInstance().bitmap;
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
today = new Date(System.currentTimeMillis());
str_todaytime = dateFormat.format(today);
str_todaytimes = str_todaytime.split(" ");
mainURL = ACache.get(mContext.getApplicationContext()).getAsString("MainUrl");
// WatchedEntityIndexItem.getInstance().addWatcher(this);
DisplayMetrics dm = new DisplayMetrics();
((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
Constant.ScreenWith = dm.widthPixels;//宽度
Constant.ScreenHeight = dm.heightPixels ;//高度
defaultDrawable = mContext.getResources().getDrawable(R.drawable.photo);
}
public void setData(List<T> mData){
this.mData = mData;
}
@Override
public int getCount() {
return mData == null ? 0 : mData.size();
}

@Override
public Object getItem(int position) {
return mData.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = ViewHolder.get(mContext, convertView, parent,R.layout.item_qiuzhi_suggest_answer_listview, position);
// final LinearLayout layout_answer = viewHolder.getView(R.id.qiuzhi_item_layout_answer);
// LinearLayout layout_answercontent = viewHolder.getView(R.id.qiuzhi_item_layout_answercontent);
LinearLayout layout_gist = viewHolder.getView(R.id.qiuzhi_item_layout_gist);
RelativeLayout layout_bottom = viewHolder.getView(R.id.qiuzhi_item_layout_bottom);
RelativeLayout layout_author = viewHolder.getView(R.id.qiuzhi_item_layout_author);
TextView tv_likenum = viewHolder.getView(R.id.qiuzhi_item_tv_likenum);
ImageView iv_photo = viewHolder.getView(R.id.qiuzhi_item_iv_photo);
TextView tv_author = viewHolder.getView(R.id.qiuzhi_item_tv_author);
TextView tv_answer = viewHolder.getView(R.id.qiuzhi_item_tv_answer);
TextView tv_answer_ = viewHolder.getView(R.id.qiuzhi_item_tv_answer_);
final WebView tv_gist = viewHolder.getView(R.id.qiuzhi_item_tv_gist);
// TextView tv_gist = viewHolder.getView(R.id.qiuzhi_item_tv_gist);
ImageView tv_gist_ = viewHolder.getView(R.id.qiuzhi_item_tv_gist_);
GridView gv_gallery = viewHolder.getView(R.id.qiuzhi_item_gv_gallery);
TextView tv_time = viewHolder.getView(R.id.qiuzhi_item_tv_time);
TextView tv_commentnum = viewHolder.getView(R.id.qiuzhi_item_tv_commentnum);

try {
final RecommentIndexAnswer answer = (RecommentIndexAnswer) mData.get(position);
if (null == answer) {
layout_author.setVisibility(8);
layout_bottom.setVisibility(8);
layout_gist.setVisibility(8);
tv_answer_.setVisibility(8);
tv_answer.setText(mContext.getResources().getString(R.string.oldAnswer_isDel_orIsShielded));
}else{
//
tv_answer_.setVisibility(0);
layout_author.setVisibility(0);
layout_gist.setVisibility(0);
layout_bottom.setVisibility(0);
tv_likenum.setText(String.valueOf(answer.getLikeCount())+"赞");
if (TextUtils.isEmpty(answer.getIdFlag())) {
tv_author.setText(mContext.getResources().getString(R.string.anonymity));
bitmap.display(iv_photo, "");
}else{
tv_author.setText(answer.getIdFlag());
String url =  mainURL +ConstantUrl.photoURL+"?AccID="+answer.getJiaoBaoHao();
bitmap.display(iv_photo, url);
}
String[] str_times = answer.getRecDate().split("T");
if (str_times[0].equals(str_todaytimes[0])) {
tv_time.setText(str_times[1]);
}else{
tv_time.setText(str_times[0]);
}
tv_commentnum.setText(String.valueOf(answer.getCCount()));
tv_answer.setText(answer.getATitle());  
//
int flag = answer.getFlag();
//
answer.getAContent().replaceAll("\n", "<br>").replaceAll("</br>", "<br>").replaceAll("<br/>", "<br>");
if (flag == 0) {
// tv_gist.removeAllViews();
BaseUtils.loadWeb(mContext, tv_gist, "");
tv_gist_.setImageResource(R.drawable.icon_qiuzhi_nocontent_);
}else if(flag == 1){
BaseUtils.loadWeb(mContext, tv_gist, StringUtils.xml2webview(answer.getAContent()));
Log.e("answerSite",StringUtils.xml2webview(answer.getAContent()));
// tv_gist.loadDataWithBaseURL(null, StringUtils.xml2webview(answer.getAContent()), "text/html", "utf-8", null);
if (TextUtils.isEmpty(answer.getAContent())) {
tv_gist_.setImageResource(R.drawable.icon_qiuzhi_nocontent_);
}else{
tv_gist_.setImageResource(R.drawable.icon_qiuzhi_content_);
}
}else if(flag == 2){

tv_gist_.setImageResource(R.drawable.icon_qiuzhi_gist_);
// tv_gist.loadDataWithBaseURL(null, StringUtils.xml2webview(answer.getAContent()), "text/html", "utf-8", null);
BaseUtils.loadWeb(mContext, tv_gist, StringUtils.xml2webview(answer.getAContent()));
}
// }
// String thumbnail = answer.getThumbnail();
// if (TextUtils.isEmpty(thumbnail)) {
// gv_gallery.setVisibility(8);
// }else{
// gv_gallery.setVisibility(0);
// JSONArray jsonarray = new JSONArray(thumbnail);
// PictureAdapter adapter = new PictureAdapter(mContext,jsonarray);
// gv_gallery.setAdapter(adapter);
// }
}
} catch (Exception e) {
e.printStackTrace();
}
return viewHolder.getConvertView();
}
@Override
public void Destroy() {
if (defaultDrawable != null) {
defaultDrawable = null;
}
// WatchedEntityIndexItem.getInstance().removeWatcher(this);

private String SubNewString(String html,String start,String end){
boolean is = true;
while(is){
try {
if (html.contains(start)) {
int a = html.indexOf(start);//第一个字符串的起始位置
int b = html.indexOf(end);//第二个字符串的起始位置
html = html.substring(0, a)+html.substring(b+end.length(), html.length());//利用substring进行字符串截取
}else{
is = false;
}
} catch (Exception e) {
is = false;
}
}
return html;
}
public RecommentIndexItem getQuestion() {
return question;
}
public void setQuestion(RecommentIndexItem question) {
this.question = question;
}

@Override
public void update(QuestionIndexItem qEntity) {
// TODO Auto-generated method stub

}
@Override
public void update(AnswerItem answer) {
// TODO Auto-generated method stub
if (answer != null) {
for (int i = 0; i < mData.size(); i++) {
RecommentIndexAnswer item = (RecommentIndexAnswer) mData.get(i);
if (item.getTabID() == answer.getTabID()) {
item.setCaiCount(answer.getCaiCount());
item.setCCount(answer.getCCount());
item.setLikeCount(answer.getLikeCount());
mData.set(i, (T) item);
}
}
}
QiuZhiSuggestAnswerListAdapter.this.notifyDataSetChanged();
}

}

webview不断重新加载,关掉webview,其他控件毫无问题