1

need your help with slow scrolling in ListView. When I run my app and scrolling ListView it doing very slow with breaks.

Here's my logcat:

07-07 14:27:17.682 30190-30190/com.nadolskiy.sergey.githubreader I/Choreographer﹕ Skipped 75 frames! The application may be doing too much work on its main thread. 07-07 14:27:22.550 30190-30190/com.nadolskiy.sergey.githubreader I/Choreographer﹕ Skipped 92 frames! The application may be doing too much work on its main thread. 07-07 14:27:30.116 30190-30190/com.nadolskiy.sergey.githubreader I/Choreographer﹕ Skipped 104 frames! The application may be doing too much work on its main thread. 07-07 14:27:32.023 30190-30190/com.nadolskiy.sergey.githubreader I/Choreographer﹕ Skipped 111 frames! The application may be doing too much work on its main thread. 07-07 14:27:33.828 30190-30190/com.nadolskiy.sergey.githubreader I/Choreographer﹕ Skipped 100 frames! The application may be doing too much work on its main thread. 

custom_listview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="0.2" android:orientation="vertical"> <TextView android:id="@+id/custom_listview_tv_repositoryName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Github-Reader" android:textSize="20dp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/custom_listview_tv_languageLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Language: " android:textSize="14dp" /> <TextView android:id="@+id/custom_listview_tv_language" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Java" android:textSize="14dp" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="0.4" android:orientation="horizontal" android:gravity="center"> <TextView android:id="@+id/custom_listview_tv_branchCount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="15" android:textSize="20dp" android:layout_weight="0.5" android:gravity="right" /> <ImageView android:id="@+id/custom_listview_iv_branch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_branch" android:layout_weight="0.5" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="0.4" android:orientation="horizontal" android:gravity="center"> <TextView android:id="@+id/custom_listview_tv_inFavoriteCount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="31" android:textSize="20dp" android:layout_weight="0.5" android:gravity="right" /> <ImageView android:id="@+id/custom_listview_iv_favorite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_star" android:layout_weight="0.5" /> </LinearLayout> </LinearLayout> 

CustomAdapter.java

public class CustomAdapter extends BaseAdapter implements DialogInterface.OnClickListener { private Activity activity; private ArrayList data; private static LayoutInflater inflater = null; public Resources res; ListModel tempValues = null; int i = 0; Typeface textFont; public CustomAdapter(Activity a, ArrayList d, Resources resLocal) { activity = a; data = d; res = resLocal; textFont = Typeface.createFromAsset(activity.getAssets(), "fonts/DiamondGirl.ttf"); inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { if (data.size() <= 0) return 1; return data.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } public static class ViewHolder { public TextView repositoryName; public TextView language; public TextView branchCount; public TextView inFavoriteCount; } @Override public View getView(int position, View convertView, ViewGroup parent) { View vi = convertView; ViewHolder holder; if (convertView == null) { vi = inflater.inflate(R.layout.custom_listview, null); holder = new ViewHolder(); holder.repositoryName = (TextView) vi.findViewById( R.id.custom_listview_tv_repositoryName); holder.language = (TextView) vi.findViewById( R.id.custom_listview_tv_language); holder.branchCount = (TextView) vi.findViewById( R.id.custom_listview_tv_branchCount); holder.inFavoriteCount = (TextView) vi.findViewById( R.id.custom_listview_tv_inFavoriteCount); vi.setTag(holder); } else { holder = (ViewHolder) vi.getTag(); } ; if (data.size() <= 0) { holder.repositoryName.setText("No Data"); } else { tempValues = null; tempValues = (ListModel) data.get(position); holder.repositoryName.setText(tempValues.getRepositoryName()); holder.language.setText(tempValues.getLanguage()); holder.branchCount.setText(tempValues.getBranchCount()); holder.inFavoriteCount.setText(tempValues.getInFavoriteCount()); vi.setOnClickListener(new OnItemClickListener(position)); } holder.repositoryName.setTypeface(textFont); holder.language.setTypeface(textFont); holder.branchCount.setTypeface(textFont); holder.inFavoriteCount.setTypeface(textFont); return vi; } @Override public void onClick(DialogInterface dialog, int which) { Log.v("Custom Adapter", "Button Clicked"); } private class OnItemClickListener implements View.OnClickListener { private int mPosition; OnItemClickListener(int position) { mPosition = position; } @Override public void onClick(View v) { UserActivity ua = (UserActivity) activity; ua.onItemClick(mPosition); } } 

ListModel.java

public class ListModel { private String repositoryName = ""; private String language = ""; private String branchCount = ""; private String inFavoriteCount = ""; public void setRepositoryName(String repositoryName) { this.repositoryName = repositoryName; } public void setLanguage(String language) { this.language = language; } public void setBranchCount(String branchCount) { this.branchCount = branchCount; } public void setInFavoriteCount(String inFavoriteCount) { this.inFavoriteCount = inFavoriteCount; } public String getRepositoryName() { return repositoryName; } public String getLanguage() { return language; } public String getBranchCount() { return branchCount; } public String getInFavoriteCount() { return inFavoriteCount; } } 

UserActivity.java

public class UserActivity extends AppCompatActivity { ListView list; CustomAdapter adapter; public UserActivity userActivity = null; public ArrayList<ListModel> customListViewValuesArr = new ArrayList<ListModel>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user); userActivity = this; setListData(); Resources res = getResources(); list = (ListView) findViewById(R.id.activitu_user_lv_repositoriesList); adapter = new CustomAdapter(userActivity, customListViewValuesArr, res); list.setAdapter(adapter); } private void setListData() { for (int i = 0; i < 11; i++) { final ListModel sched = new ListModel(); sched.setRepositoryName("Repository №" + i); sched.setLanguage("Java"); sched.setBranchCount("" + (i+5)); sched.setInFavoriteCount("" + (i*12)); customListViewValuesArr.add(sched); } } public void onItemClick(int mPosition) { ListModel tempValues = (ListModel) customListViewValuesArr.get(mPosition); Toast.makeText(userActivity, "" + tempValues.getRepositoryName(), Toast.LENGTH_LONG).show(); } } 

Thanks, =)

6
  • 1
    Try to run by commenting the custom font setup Commented Jul 7, 2015 at 12:09
  • @VaisakhN I tried, but didn't help. Commented Jul 7, 2015 at 12:12
  • Are you using android studio? If then go to memory monitor from Android Tab below there you can see allocated memory from there scroll your listview multiple times and let me know if the allocated memory increasing much more if not let me know which phone you are using Commented Jul 7, 2015 at 12:13
  • @VaisakhN yes, I use Android Studio, link - screen-shot of CPU, my device Motorola Moto G (X1028) Commented Jul 7, 2015 at 12:29
  • what about memory?4th tab Commented Jul 7, 2015 at 12:38

3 Answers 3

1

I cant find problem in this code and this is sad, but I find solution in tutorial. I rewrite my code and it helps. So, how it look now:

CustomAdapter.java rename to RepositoryAdapter.java and rewrite some code:

public class RepositoryAdapter extends BaseAdapter { Context context; Typeface textFont; protected List<Repository> listRepository; LayoutInflater inflater; public RepositoryAdapter(Context context, List<Repository> listRepository){ this.listRepository = listRepository; this.inflater = LayoutInflater.from(context); this.context = context; textFont = Typeface.createFromAsset(context.getAssets(), "fonts/DiamondGirl.ttf"); } public int getCount(){ return listRepository.size(); } @Override public Repository getItem(int position) { return listRepository.get(position); } @Override public long getItemId(int position) { return 0; } public View getView(int position, View convertView, ViewGroup parent){ ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = this.inflater.inflate(R.layout.custom_listview, parent, false); holder.tvRepositoryName = (TextView) convertView.findViewById(R.id.custom_listview_tv_repositoryName); holder.tvLanguage = (TextView) convertView.findViewById(R.id.custom_listview_tv_language); holder.tvBranchCount = (TextView) convertView.findViewById(R.id.custom_listview_tv_branchCount); holder.tvInFavoriteCount = (TextView) convertView.findViewById(R.id.custom_listview_tv_inFavoriteCount); holder.tvRepositoryName.setTypeface(textFont); holder.tvLanguage.setTypeface(textFont); holder.tvBranchCount.setTypeface(textFont); holder.tvInFavoriteCount.setTypeface(textFont); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } Repository repository = listRepository.get(position); holder.tvRepositoryName.setText(repository.getRepositoryName()); holder.tvLanguage.setText(repository.getLanguage()); holder.tvBranchCount.setText(repository.getBranchCount()); holder.tvInFavoriteCount.setText(repository.getInFavoriteCount()); return convertView; } private class ViewHolder { TextView tvRepositoryName; TextView tvLanguage; TextView tvBranchCount; TextView tvInFavoriteCount; } } 

ListModel.java now Repository.java:

public class Repository { private String repositoryName = ""; private String language = ""; private String branchCount = ""; private String inFavoriteCount = ""; public Repository(String repositoryName, String language, String branchCount, String inFavoriteCount) { super(); this.repositoryName = repositoryName; this.language = language; this.branchCount = branchCount; this.inFavoriteCount = inFavoriteCount; } public void setRepositoryName(String repositoryName) { this.repositoryName = repositoryName; } public void setLanguage(String language) { this.language = language; } public void setBranchCount(String branchCount) { this.branchCount = branchCount; } public void setInFavoriteCount(String inFavoriteCount) { this.inFavoriteCount = inFavoriteCount; } public String getRepositoryName() { return repositoryName; } public String getLanguage() { return language; } public String getBranchCount() { return branchCount; } public String getInFavoriteCount() { return inFavoriteCount; } } 

UserActivity.java:

public class UserActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { ArrayList<Repository> arrayRepositories; ListView listViewRepositories; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user); arrayRepositories = new ArrayList<Repository>(); fillList(); listViewRepositories = (ListView) findViewById(R.id.activitu_user_lv_repositoriesList); RepositoryAdapter adapter = new RepositoryAdapter(this, arrayRepositories); listViewRepositories.setAdapter(adapter); listViewRepositories.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id){ Repository selectedRepository = arrayRepositories.get(position); Toast.makeText(this, selectedRepository.getRepositoryName(), Toast.LENGTH_SHORT).show(); } private void fillList() { for (int i = 0; i < 12; i++) { Repository repository = new Repository("Repository #"+i,"Java",""+ (i*5),"" + (i*12)); arrayRepositories.add(repository); } } } 

Now it's works, and work fast. Thanks all for help =)

Sign up to request clarification or add additional context in comments.

Comments

0

Everything looking good to improve performance you can setTypeFace in first if block that is

if(convertview==null} { //at the end holder.repositoryName.setTypeface(textFont); holder.language.setTypeface(textFont); holder.branchCount.setTypeface(textFont); holder.inFavoriteCount.setTypeface(textFont); } 

3 Comments

can you edit your question and provide ListView tags in your activity.
@Mohd Mufiz: the message says "The application may be doing too much work on its main thread. " so it is likely that some operation has to go into a non-gui-background thread
@MohdMufiz sorry, but i don't understand what exactly you want, that i do?
0

If scrolling is to slow it is likely that something in

public View getView(int position, View convertView, ViewGroup parent) 

is to slow. getView is called for every list-item that becomes visible.

Is this the slow part that needs expensive database/file system query?

 tempValues = (ListModel) data.get(position); 

I had the same problem with a list-view that contain images where calculating and caching the preview image from the original was expensive.

This can be solved using a ui synchronized WorkerThread or AsyncTask

1 Comment

yes, i think problem in getView(), because when i comment tempValues it's work very fast. But all elements in listview the same.