我研究了一个自定义列表视图,它是一个下拉刷新列表视图,但它显示一个奇怪的错误,每当我尝试在我的 Galaxy s3 上运行它时,它就会开始说
I/Choreographer(698):跳过340帧!应用程序可能在其主线程上做了太多工作。
并且列表视图的滚动非常缓慢。附件是在 s3 和 HTC 设备上测试的视频,它在 HTC 上完美运行。任何指示为什么会发生这种情况?
宏达电:http ://www.youtube.com/watch?v= 9o1UXv_-Uco S3:http ://www.youtube.com/watch?v=YjjC07M70Gk
编辑:添加代码
public class MainActivity extends ListActivity implements OnScrollListener {
private LinkedList<String> mListItems;
TmbResultListViewAdapter adapter;
Button editButton;
public static boolean refreshOnDrag = true;
private boolean isloading = false;
private MyTask task;
PullToRefreshListView lv;
private ProgressBar footer;
private TextView statusBar;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRefreshOnPull(true);
setContentView(R.layout.activity_main);
statusBar = (TextView) findViewById(R.id.statusbar);
editButton = (Button) findViewById(R.id.editButton);
lv = (PullToRefreshListView) getListView();
lv.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
// Do work to refresh the list here.
if (refreshOnDrag) {
new GetDataTask().execute();
}
}
});
lv.setOnScrollListener(this);
lv.setScrollingCacheEnabled(false);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
footer = (ProgressBar) inflater.inflate(R.layout.footer, null);
mListItems = new LinkedList<String>();
mListItems.addAll(Arrays.asList(mStrings));
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_1, mListItems);
adapter = new TmbResultListViewAdapter(this);
setListAdapter(adapter);
}
public void edit(View v) {
if (!adapter.showEditControlls) {
adapter.showEditControlls = true;
adapter.notifyDataSetChanged();
editButton.setText("Done");
} else {
adapter.showEditControlls = false;
adapter.notifyDataSetChanged();
editButton.setText("Edit");
}
}
public void setRefreshOnPull(boolean val) {
this.refreshOnDrag = val;
}
private class GetDataTask extends AsyncTask<Void, Void, String[]> {
@Override
protected String[] doInBackground(Void... params) {
// Simulates a background job.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
;
}
adapter.resetItems();
return mStrings;
}
@Override
protected void onPostExecute(String[] result) {
mListItems.addFirst("Added after refresh...");
// Call onRefreshComplete when the list has been refreshed.
((PullToRefreshListView) getListView()).onRefreshComplete();
super.onPostExecute(result);
}
}
private String[] mStrings = { "Abbaye de Belloc",
"Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu",
"Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler" };
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int loadedItems = firstVisibleItem + visibleItemCount;
int offSet = (((int) ((firstVisibleItem - 1) / adapter.getItemPerPage())) * adapter
.getItemPerPage()) + 1;
statusBar.setText(offSet + " of "
+ (offSet + adapter.getItemPerPage() - 1));
if ((loadedItems == totalItemCount) && !isloading) {
if (task == null || (task.getStatus() == AsyncTask.Status.FINISHED)) {
task = new MyTask();
task.execute();
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
((PullToRefreshListView) getListView()).addFooterView(footer);
// adapter.notifyDataSetChanged();
}
@Override
protected Void doInBackground(Void... params) {
isloading = true;
adapter.loadMoreData();
// ProgressBar pb = new ProgressBar(getApplicationContext(), null,
// android.R.attr.progressBarStyleHorizontal);
// LinearLayout linLayout = (LinearLayout)
// findViewById(R.id.linearLayout);
// linLayout.addView(pb);
Log.d("*****", "Loading...");
return null;
}
@Override
protected void onPostExecute(Void result) {
// adapter.notifyDataSetChanged();
isloading = false;
adapter.notifyDataSetChanged();
((PullToRefreshListView) getListView()).removeFooterView(footer);
}
}
@Override
public void onBackPressed() {
finish();
}
}
添加了适配器代码 我已经看到,当我尝试滚动时,跳帧会变得更糟。所以可能我在 getview 方法上做了很多工作
public View getView(final int position, View convertView, ViewGroup parent) {
//View convertView = convertView;
// TmbJSON eventDetail;
if (convertView == null) {
convertView = inflater.inflate(R.layout.tmb_result_listitem, null);
mViewHolder = new viewHolder();
mViewHolder.resultTitle = (TextView) convertView
.findViewById(R.id.resultTitle);
mViewHolder.resultPeriod = (TextView) convertView
.findViewById(R.id.resultPeriod);
mViewHolder.resultVenue = (TextView) convertView
.findViewById(R.id.resultVenue);
mViewHolder.resultDescription = (TextView) convertView
.findViewById(R.id.resultDescription);
mViewHolder.resultIcon = (ImageView) convertView
.findViewById(R.id.resultIcon);
mViewHolder.checkUnCheckIcon = (ImageView) convertView
.findViewById(R.id.checkuncheckicon);
mViewHolder.layout = (LinearLayout) convertView.findViewById(R.id.linearLayout);
mViewHolder.headerTextView = (TextView) convertView.findViewById(R.id.itemheader);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (viewHolder) convertView.getTag();
}
try {
mViewHolder.checkUnCheckIcon.setVisibility(showEditControlls ? ImageView.VISIBLE : ImageView.GONE);
mViewHolder.checkUnCheckIcon.setImageResource(mCheckedState.get(position) ? R.drawable.starschecked : R.drawable.starsunchecked);
mViewHolder.checkUnCheckIcon.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mCheckedState.get(position)) {
mViewHolder.checkUnCheckIcon
.setImageResource(R.drawable.starsunchecked);
mCheckedState.set(position, false);
} else if (!mCheckedState.get(position)) {
mViewHolder.checkUnCheckIcon
.setImageResource(R.drawable.starschecked);
mCheckedState.set(position, true);
}
theAdapter.notifyDataSetChanged();
Toast.makeText(activity.getApplicationContext(), position+"th Event un/checked", Toast.LENGTH_LONG).show();
}
});
String name = masterList.get(position).name;// eventDetail.smartStringWithPath(TmbJSON.arrayListObjectWithStrings("name"));
String detail = masterList.get(position).detail;// eventDetail.smartStringWithPath(TmbJSON.arrayListObjectWithStrings("detail"));
String venue = masterList.get(position).venue;// ((JSONObject)eventDetail.object).getJSONArray("prs").getJSONObject(0).getJSONObject("loc").getString("name");
String period = masterList.get(position).period;// ((JSONObject)eventDetail.object).getJSONArray("prs").getJSONObject(0).getString("ts_z");
mViewHolder.resultTitle.setText(name);
mViewHolder.resultPeriod.setText(period);
mViewHolder.resultVenue.setText(venue);
mViewHolder.resultDescription.setText(detail);
if((position) % (itemPerPage) == 0 && position!=0)
{ mViewHolder.headerTextView.setText((position+1)+" of "+(int)(position+20));
mViewHolder.headerTextView.setVisibility(TextView.VISIBLE);
}
else
{
mViewHolder.headerTextView.setVisibility(TextView.GONE);
}
int forthPos = getCount() - itemPerPage + 4;
int tenthPos = getCount() - itemPerPage + 10;
int sixteenthPos = getCount() - itemPerPage + 16;
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}