我的ListView使用以下代码。。
我的FragmentActivity
@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;
}
}
上面的代码只是表现为没有调用简单的ListView、cacheInBackground或getPendingView。此外,我也想添加一个headerView,但它也不起作用。
这里面我缺了什么?
这里的大多数代码都不属于EndlessAdapter
。引用文件:
它被设计为围绕另一个适配器,在那里您可以获得"真实"数据。因此,它遵循Decorator模式,使用新的Endless Technology(TM)增强您当前的适配器。
因此,首先,创建一个常规ArrayAdapter
,并获得所需的所有样式和内容(例如,getView()
实现)。然后,创建一个EndlessAdapter
子类,添加"endless"。
注意:
-
EndlessAdapter
子类不应该覆盖getView()
,因为这是添加的无尽行为的地方 -
EndlessAdapter
子类不应覆盖getCount()
,因为它由EndlessAdapter
实现本身管理
您可能希望检查示例应用程序,看看它是如何实现EndlessAdapter
子类的。
或者,由于EndlessAdapter
不适用于头视图,您可能只需要切换到另一个无休止列表解决方案或推出自己的解决方案。