我有三個可滑動的選項卡。在第一個選項卡中,我有一個包含1000多個項目的列表視圖。listView.setAdapter是如此呆滯?
我試圖用Asynctask和Runnable填充列表,我沒有問題。
問題是當我試圖將適配器分配給列表視圖。我注意到無論有多少物品在適配器中,無論它是1還是1000,當它試圖做:listView.setAdapter(適配器)時,它跳過大約30幀...
由於listView.setAdapter(適配器)在第一個選項卡中使用,每當用戶嘗試輸入該選項卡時,UI會凍結幾毫秒。雖然這不是一個嚴重的問題,但我不喜歡它。
問題是:這是正常的,當我們調用listView.setAdapter(適配器)的UI凍結了幾毫秒,並跳過一些幀?有沒有辦法解決這個問題?
我清楚嗎?
Carpet_handler ch; // my db handler
ListView listview;
public ArrayList<CarpetGen> carpetGens;
CarpetAdapter adapter;
boolean loadingMore=false;
int offset;
Context ctx;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_first_page_customers, container, false);
ch=new Carpet_handler(getActivity()); // db handler
listview = (ListView) rootView.findViewById(R.id.listView2);
ctx=this.getActivity();
carpetGens = new ArrayList<CarpetGen>();
offset=0;
adapter = new CarpetAdapter(getActivity(), R.layout.row, carpetGens);
listview.setAdapter(adapter); // adapter is empty first time, runnable fills it
listview.setOnItemClickListener(myClickListener);
//Here is where the magic happens
listview.setOnScrollListener(new OnScrollListener(){
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//what is the bottom iten that is visible
int lastInScreen = firstVisibleItem + visibleItemCount;
//is the bottom item visible & not loading more already ? Load more !
if((lastInScreen == totalItemCount) && !(loadingMore)){
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
}
}
});
return rootView;
}
//Runnable to load the items
private Runnable loadMoreListItems = new Runnable() {
@Override
public void run() {
//Set flag so we cant load new items 2 at the same time
loadingMore = true;
//Reset the array that holds the new items
carpetGens= new ArrayList<CarpetGen>();
carpetGens.addAll(ch.getAllCustomerCarpets1(getActivity().getIntent().getIntExtra("recordid", -1), offset));
offset+=15;
//Done! now continue on the UI thread
if(getActivity()==ctx)
getActivity().runOnUiThread(returnRes);
}
};
//Since we cant update our UI from a thread this Runnable takes care of that!
private Runnable returnRes = new Runnable() {
@Override
public void run() {
//Loop thru the new items and add them to the adapter
if(carpetGens != null && carpetGens.size() > 0){
for(int i=0;i < carpetGens.size();i++)
adapter.add(carpetGens.get(i));
}
adapter.notifyDataSetChanged();
loadingMore = false;
}
};
這裏是我的適配器:
public class CarpetAdapter extends ArrayAdapter<CarpetGen> {
private final Context context;
private final int rowResourceId;
//private final String[] Ids;
private final ArrayList<CarpetGen> Objects;
public CarpetAdapter(Context context, int textViewResourceId, ArrayList<CarpetGen> objects){//Arr[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
this.rowResourceId = textViewResourceId;
this.Objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = convertView;
CarpetGen i = Objects.get(position);
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row, parent, false);
//TextView textView = (TextView) rowView.findViewById(R.id.textView);
//textView.setText(i.Result_String);
holder = new ViewHolder();
holder.title = (TextView) rowView
.findViewById(R.id.textView);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.title.setText(i.Result_String);
return rowView;
}
static class ViewHolder {
TextView title;
} }
更新了第一篇文章,包括我的自定義arrayadapter。 – SMahdiS
我找到了解決方案。這是因爲我使用了自定義文本視圖。 – SMahdiS