需要你在ListView中慢速滚动的帮助。当我运行我的应用程序并滚动ListView时,它会在休息时非常慢。
这是我的 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();
}
}
谢谢,=)
答案 0 :(得分:1)
我无法在此代码中发现问题,这很难过,但我在tutorial找到了解决方案。我重写了我的代码,它有所帮助。那么,它现在看起来如何:
CustomAdapter.java 重命名为 RepositoryAdapter.java 并重写一些代码:
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 现在 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);
}
}
}
现在它起作用了,并且工作得很快。谢谢大家的帮助=)
答案 1 :(得分:0)
一切看起来很好以提高性能你可以先设置TypeFace,如果是
的块if(convertview==null}
{
//at the end
holder.repositoryName.setTypeface(textFont);
holder.language.setTypeface(textFont);
holder.branchCount.setTypeface(textFont);
holder.inFavoriteCount.setTypeface(textFont);
}
答案 2 :(得分:0)
如果滚动速度慢,可能是
中的内容public View getView(int position, View convertView, ViewGroup parent)
要慢。为每个可见的列表项调用getView。
这是需要昂贵的数据库/文件系统查询的缓慢部分吗?
tempValues = (ListModel) data.get(position);
我在列表视图中遇到了同样的问题,其中包含从原始文件中计算和缓存预览图像的图像很昂贵。
来解决