Java 无休止的ListView表现得像一个简单的ListView
我正在为我的ListView使用以下代码 我的Java 无休止的ListView表现得像一个简单的ListView,java,android,eclipse,commonsware-cwac,Java,Android,Eclipse,Commonsware Cwac,我正在为我的ListView使用以下代码 我的碎片活动 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.home, container, false); listView = (
碎片活动
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.home, container, false);
listView = (ListView) view.findViewById(R.id.listView);
listView.setAdapter(new CustomArrayAdapterForHome(mContext,questions));
return view;
}
这是ListView的适配器
@SuppressLint("DefaultLocale")
public class CustomArrayAdapterForHome extends EndlessAdapter
{
private final LayoutInflater inflator;
protected ImageLoader imageLoader;
private DisplayImageOptions options;
private RotateAnimation rotate=null;
private View pendingView = null;
public CustomArrayAdapterForHome(Context ctx,ArrayList<Question> questionList)
{
super( new ArrayAdapter<Question>(ctx, R.layout.question_adapter_layout, questionList));
inflator = mContext.getLayoutInflater();
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.cacheInMemory()
.cacheOnDisc()
.showImageForEmptyUri(R.drawable.user_male)
.displayer(new RoundedBitmapDisplayer(5))
.build();
rotate=new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(600);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
}
class ViewHolder
{
// MY CODE
}
@Override
public int getCount() {
return questions.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final Question question = questions.get(position);
final OpenionUser myUser = question.getUser();
// MY CODE
return view;
}
@Override
protected View getPendingView(ViewGroup parent)
{
View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, null);
pendingView = row.findViewById(android.R.id.text1);
pendingView.setVisibility(View.GONE);
pendingView=row.findViewById(R.id.throbber);
pendingView.setVisibility(View.VISIBLE);
startProgressAnimation();
return(row);
}
private void startProgressAnimation()
{
if (pendingView!=null)
{
pendingView.startAnimation(rotate);
}
}
@Override
protected void appendCachedData()
{
}
@Override
protected boolean cacheInBackground() throws Exception
{
getQuestions();
return true;
}
}
@SuppressLint(“DefaultLocale”)
公共类CustomArrayAdapterForHome扩展了EndlesAdapter
{
私人充气机;
受保护的映像加载程序映像加载程序;
私有显示图像选项;
私有RotateAimation rotate=null;
私有视图pendingView=null;
公共CustomArrayAdapterForHome(上下文ctx、ArrayList问题列表)
{
超级(新阵列适配器(ctx,R.layout.question_adapter_layout,questionList));
充气器=mContext.getLayoutFlater();
imageLoader=imageLoader.getInstance();
选项=新建DisplayImageOptions.Builder()
.cacheInMemory()
.cacheOnDisc()
.showImageForEmptyUri(R.drawable.user_男性)
.显示器(新的圆形位图显示器(5))
.build();
旋转=新的旋转动画(0f,360f,Animation.RELATIVE\u TO\u SELF,
0.5f,动画。相对于自身,
0.5f);
轮换。设定持续时间(600);
rotate.setRepeatMode(动画.重新启动);
rotate.setRepeatCount(Animation.INFINITE);
}
类视图持有者
{
//我的代码
}
@凌驾
public int getCount(){
返回问题。size();
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
最终问题=问题。获取(位置);
final OpenionUser myUser=question.getUser();
//我的代码
返回视图;
}
@凌驾
受保护的视图getPendingView(视图组父视图)
{
View row=LayoutInflater.from(parent.getContext()).flate(R.layout.row,null);
pendingView=row.findViewById(android.R.id.text1);
pendingView.setVisibility(View.GONE);
pendingView=row.findViewById(R.id.throbber);
pendingView.setVisibility(View.VISIBLE);
startProgressAnimation();
返回(行);
}
私有void startProgressAnimation()
{
如果(pendingView!=null)
{
pendingView.startAnimation(旋转);
}
}
@凌驾
受保护的无效appendCachedData()
{
}
@凌驾
受保护的布尔cacheInBackground()引发异常
{
获取问题();
返回true;
}
}
上面的代码的行为就像SimpleListView、cacheInBackground或getPendingView没有被调用一样。此外,我还想添加一个headerView,它也不起作用
我缺少什么?这里的大多数代码不属于
EndlessAdapter
。引述:
它被设计成环绕另一个适配器,在那里你有你的“真实”数据。因此,它遵循Decorator模式,使用新的无止境技术(TM)增强当前适配器
因此,首先,创建一个常规ArrayAdapter
,并获得您想要的所有样式和内容(例如,您的getView()
实现)。然后,创建一个EndlessAdapter
子类,将其添加到“endless”中
值得注意的是:
子类不应覆盖EndlessAdapter
,因为这是添加无休止行为的地方getView()
子类不应重写EndlessAdapter
,因为它是由getCount()
实现本身管理的EndlessAdapter
EndlessAdapter
子类的
或者,由于
EndlessAdapter
,您可能只需要切换到另一个无止境列表解决方案,或者使用您自己的解决方案。好的,谢谢上述建议,我已经开始工作了。有一个小问题。我正在cacheInBackground中做getQuestions()。和适配器。设置运行背景(错误);在我的getQuestion()异步任务中,在我得到修改后的数组readyListener.onItemsReady(问题)之后;很好。但如果我没有得到任何信息,我的数组就不会被修改,cacheInBackground会无限重复。怎么办?@MuhammadUmar:当您到达数据末尾时,应该从cacheInBackground()
返回false
。