我有Pager Adapter初始化Fragment列表。问题是片段加载速度很慢。它有几秒的延迟,而不是开始加载数据。 我在AsyncTask doInBackground()中解析了URL中的数据,并在AsyncTask onPosteExecute()中使用了UI
这是我的片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
readBundle(getArguments());
v = inflater.inflate(R.layout.fragment_fragment1, container, false);
new asyncTekstovi().execute(result);
return v;
}
public class asyncTekstovi extends AsyncTask<String, ArrayList<String>, ArrayList<String>> {
String result;
@Override
protected ArrayList<String> doInBackground(String... params)
{
try {
result = Ion.with(getActivity().getApplicationContext())
.load(FragmentNewsReader.this.result)
.asString()
.get();
} catch (Exception e){
e.printStackTrace();
}
String pasusUTekstu="";
if (result != null){
String htmlStringTekst=result.substring(result.indexOf("<!-- BEGIN .shortcode-content -->"), result.length());
int startPositionTekst=htmlStringTekst.indexOf("<div class=\"shortcode-content\">");
int endPositionTekst=startPositionTekst+"<div class=\"shortcode-content\">".length();
while(htmlStringTekst.substring(startPositionTekst,endPositionTekst).indexOf("<!-- END .shortcode-content -->")<0){
endPositionTekst++;
}
htmlStringTekst=htmlStringTekst.substring(startPositionTekst,endPositionTekst);
//System.out.println("vladica:"+htmlStringTekst.length()+htmlStringTekst);
int poc1;
int kraj1;
while((poc1=htmlStringTekst.indexOf("<div id"))>=0){
//System.out.println("poc1="+poc1);
kraj1=poc1+"<div id".length();
while(htmlStringTekst.substring(poc1,kraj1).indexOf(">")<0){
//System.out.println("seckanje:"+htmlStringTekst.substring(poc1,kraj1));
kraj1++;
}
htmlStringTekst=htmlStringTekst.replace(htmlStringTekst.substring(poc1,kraj1),"<p>");
}
while((poc1=htmlStringTekst.indexOf("<p "))>=0){
kraj1=poc1+"<p ".length();
while(htmlStringTekst.substring(poc1,kraj1).indexOf(">")<0){
kraj1++;
}
htmlStringTekst=htmlStringTekst.replace(htmlStringTekst.substring(poc1,kraj1),"<p>");
}
htmlStringTekst=htmlStringTekst.replace("<div id","<p>");
htmlStringTekst=htmlStringTekst.replace("</div>","</p>");
htmlStringTekst=htmlStringTekst.replace(" ","");
while(htmlStringTekst.length()>0){
startPositionTekst=htmlStringTekst.indexOf("<p>");
if(startPositionTekst>=0){
startPositionTekst+="<p>".length();
endPositionTekst=startPositionTekst;
while (htmlStringTekst.substring(startPositionTekst, endPositionTekst).indexOf("</p>") < 0) {
endPositionTekst++;
}
if(pasusUTekstu.indexOf("<img")<0) {
if(pasusUTekstu.length()>5) pasusUTekstu+="\n\n";
pasusUTekstu += htmlStringTekst.substring(startPositionTekst, endPositionTekst); //e sad ja...
}
else {
pasusUTekstu=htmlStringTekst.substring(startPositionTekst,endPositionTekst);
}
htmlStringTekst = htmlStringTekst.substring(endPositionTekst);
pasusUTekstu = pasusUTekstu.replace("<strong>", "");
pasusUTekstu = pasusUTekstu.replace("</strong>", "");
int poc=pasusUTekstu.indexOf("<img");
int zastavica=0;
if(poc>=0) zastavica=1;
while(poc>=0) {
int kraj = poc;
while (pasusUTekstu.substring(poc, kraj).indexOf("/>") < 0) {
kraj++;
}
String link = pasusUTekstu.substring(poc, kraj);
pasusUTekstu = pasusUTekstu.replace(link, "");
poc = pasusUTekstu.indexOf("<img");
}
poc=pasusUTekstu.indexOf("<a href=");
while(poc>=0) {
int kraj = poc;
while (pasusUTekstu.substring(poc, kraj).indexOf(">") < 0) {
kraj++;
}
String link = pasusUTekstu.substring(poc, kraj);
pasusUTekstu = pasusUTekstu.replace(link, "");
poc = pasusUTekstu.indexOf("<a href=");
}
pasusUTekstu=pasusUTekstu.replace("&","&");
pasusUTekstu=pasusUTekstu.replace("–","-");
pasusUTekstu=pasusUTekstu.replace("“","\"");
pasusUTekstu=pasusUTekstu.replace("”","\"");
pasusUTekstu=pasusUTekstu.replace("<p>","");
pasusUTekstu=pasusUTekstu.replace("</a>","");
pasusUTekstu=pasusUTekstu.replace("<br />","");
pasusUTekstu=pasusUTekstu.replace("</p>","");
pasusUTekstu=pasusUTekstu.replace(" ","");
pasusUTekstu=pasusUTekstu.replace("\n\n\n","\n");
//pasusUTekstu=pasusUTekstu.replace("\n\n","\n");
//System.out.println("pocetak je:"+ pasusUTekstu);
if(zastavica==1) {
if(pasusUTekstu.substring(pasusUTekstu.length()-2).equals("\n\n")) pasusUTekstu=pasusUTekstu.substring(0,pasusUTekstu.length()-2);
listaTeksta.add(pasusUTekstu);
pasusUTekstu="";
zastavica=0;
}
}
else break;
}
}
if(pasusUTekstu.length()>5) listaTeksta.add(pasusUTekstu);
return listaTeksta;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
String rezultat = "";
for(int i=0;i<listaSlika.size() ;i++) {
v.findViewById(mLayoutIdArray[i]).setVisibility(View.VISIBLE);
}
for(int i=0;i<result.size();i++) {
textTv[i] = (TextView) v.findViewById(nizTekstaID[i]);
textTv[i].setText(result.get(i));
}
}
protected void onPreExecute() {
}
}
这是我的XML:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vestGlavnaSlikaLayout"
android:layoutDirection="ltr"
android:elevation="10dp">
<ImageView
android:id="@+id/glavnaSlika"
android:layout_width="match_parent"
android:fitsSystemWindows="false"
android:scaleType="fitXY"
android:focusable="false"
android:cropToPadding="false"
android:adjustViewBounds="true"
android:layout_alignParentBottom="false"
android:layout_alignParentRight="false"
android:layout_alignParentEnd="false"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/layouttekstglavnevesti1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:drawable/screen_background_dark_transparent"
android:padding="0dp">
<TextView
android:id="@+id/naslovglavni"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:padding="10dp"
android:textColor="@android:color/background_light"
android:textSize="36sp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lazoutZaNaslove"
android:layout_marginBottom="15dp"
android:layout_marginLeft="@dimen/h"
android:layout_marginRight="@dimen/h"
android:layout_marginTop="-10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/prednaslov"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="@android:color/darker_gray"
android:layout_marginLeft="-40dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/naslov1"
android:textSize="20sp"
android:textColor="@android:color/black"
android:textStyle="normal|bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/podnaslov"
android:textColor="@android:color/black"
android:textSize="15sp"
android:textStyle="normal|bold" />
</LinearLayout>
<include
layout="@layout/activity_slikatext1"
android:id="@+id/includedLayout"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/h"
android:layout_marginRight="@dimen/h"
android:layout_below="@id/lazoutSlikaTekst" />
<include
layout="@layout/activity_slikatext2"
android:id="@+id/includedLayout1"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/h"
android:layout_marginRight="@dimen/h"
android:layout_below="@id/includedLayout" />
<include
layout="@layout/activity_slikatext3"
android:id="@+id/includedLayout2"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/h"
android:layout_marginRight="@dimen/h"
android:layout_below="@id/includedLayout1" />
<include
layout="@layout/activity_slikatext4"
android:id="@+id/includedLayout3"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/h"
android:layout_marginRight="@dimen/h"
android:layout_below="@id/includedLayout2" />
<include
layout="@layout/activity_slikatext5"
android:id="@+id/includedLayout4"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/h"
android:layout_marginRight="@dimen/h"
android:layout_below="@id/includedLayout3" />
答案 0 :(得分:1)
将此代码更改为onViewCreated()方法
new asyncTekstovi().execute(result);