我在viewpager中有3个页面,每个页面都有ListView。 ListView的数据是从数据库提供的。当我从列表视图的最后一页数据移动再次从数据加载时需要更多时间。看起来很恶心 请解决我的问题..
package searchFragments;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import com.example.newdictionary.R;
import database.DBHelper;
public class SimilerWordFragment extends Fragment {
SimpleAdapter adapter;
ListView listView;
List<HashMap<String, String>> map;
String[] from = { "ENGLISH", "HINDI", "TYPE" };
String word;
ProgressBar bar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.similer_word_fragment, container,
false);
bar = (ProgressBar) v.findViewById(R.id.all_progress_bar);
listView = (ListView) v.findViewById(R.id.similer_word_list_view);
int[] to = { R.id.spelling, R.id.meaning, R.id.type };
map = new ArrayList<HashMap<String, String>>();
adapter = new SimpleAdapter(getActivity(), map,
R.layout.list_view_item, from, to);
word = getActivity().getIntent().getStringExtra("word");
new Task().execute();
return v;
}
class Task extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
bar.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... params) {
DBHelper db = new DBHelper(getActivity());
Cursor c = db.getSuggestion(word);
if (c.moveToFirst()) {
do {
HashMap<String, String> h = new HashMap<String, String>();
h.put(from[0], c.getString(0));
h.put(from[1], c.getString(1));
h.put(from[2], c.getString(2));
map.add(h);
} while (c.moveToNext());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
listView.setAdapter(adapter);
bar.setVisibility(View.GONE);
}
}
}
答案 0 :(得分:2)
尝试添加pager.setOffscreenPageLimit(3);寻呼机是您的ViewPager。