我正在尝试制作LazyList EndlessList,但使用分段适配器...这些部分将是将从Internet下载的项目页面。
我尝试使用MergeAdapter或已弃用的SectionedAdapter(由Mark Murphy来)实现它的基本要点。但是我都面临着两个问题。
1)使用SectionedAdapter,它可以很好地加载第一页,但是在第二页上,我遇到了此异常。
Uncaught handler: thread main exiting due to uncaught exception
java.lang.ArrayIndexOutOfBoundsException
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:3572)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2487)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2353)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
经过数小时的研究,我意识到这与错误的
getViewTypeCount()
值有关。但是我不知道如何正确实施它。2)然后,我尝试使用MergeAdapter,但这只会使情况变得更糟,即使Sections的出现间隔是随机的。
我已经写了一个小的示例程序来展示我的实现。
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class LazySectionListActivity extends ListActivity implements
OnScrollListener {
private int mPrevItemCount;
private int nextPrimes = 0;
private final int MAX = 100;
private final SectionAdapter adapter = new SectionAdapter();
// private final MergeAdapter adapter = new MergeAdapter();
private class SectionAdapter extends SectionedAdapter {
@Override
protected View getHeaderView(String caption, int index,
View convertView, ViewGroup parent) {
TextView tv = new TextView(LazySectionListActivity.this);
tv.setText(caption);
return tv;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminate(true);
setContentView(R.layout.main);
getListView().setOnScrollListener(this);
setListAdapter(adapter);
new NextThousandPrimes(nextPrimes).execute();
nextPrimes += MAX;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(firstVisibleItem + visibleItemCount >= totalItemCount
&& mPrevItemCount != totalItemCount) {
new NextThousandPrimes(nextPrimes).execute();
nextPrimes += MAX;
mPrevItemCount = totalItemCount;
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
private class NextThousandPrimes extends AsyncTask<Void, Void, Void> {
private final int start;
private final List<String> primes = new ArrayList<String>();
public NextThousandPrimes(int nextPrimes) {
this.start = nextPrimes;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
setProgressBarIndeterminateVisibility(true);
}
@Override
protected Void doInBackground(Void... params) {
for(int s = start; s < start + MAX; s++)
if(BigInteger.valueOf(s).isProbablePrime(1)) {
primes.add(String.valueOf(s));
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
setProgressBarIndeterminateVisibility(false);
//TextView view = new TextView(LazySectionListActivity.this);//for merge adapter, use the commented code
//view.setText(String.format("%d-%d", start, start + MAX));
// adapter.addView(view);
// adapter.addAdapter(new
// ArrayAdapter<String>(LazySectionListActivity.this,
// android.R.layout.simple_list_item_1,
// primes));
adapter.addSection(String.format("%d-%d", start, start + MAX),
new ArrayAdapter<String>(LazySectionListActivity.this,
android.R.layout.simple_list_item_1,
primes));
adapter.notifyDataSetChanged();
}
}
}
那我想念什么?任何帮助表示赞赏。提前致谢。
最佳答案
如MergeAdapter
文档中所述:
必须先将内容倒入MergeAdapter中,然后才能调用setListAdapter()将MergeAdapter与ListView关联。此限制是必需的,因为Android仅调用一次getViewTypeCount(),并且添加更多视图或适配器会添加更多视图类型。
因此,MergeAdapter
在无限设置中不起作用,因为我们不知道有多少种可能的视图类型。
欢迎您尝试破解MergeAdapter
的副本,在其中连接getViewTypeCount()
以获得一定的价值,您肯定会超出您的需求,然后看是否可行。俗话说,您的里程可能会有所不同... :-)