【问题标题】:Endless ListView behaving like a simple ListView无尽的 ListView 表现得像一个简单的 ListView
【发布时间】:2013-06-28 06:36:48
【问题描述】:

我正在为我的 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 并且它也不起作用。

我错过了什么?

【问题讨论】:

    标签: java android eclipse commonsware-cwac


    【解决方案1】:

    您在此处拥有的大部分代码都不属于EndlessAdapter。引用the documentation:

    它旨在环绕另一个适配器,您可以在其中拥有“真实”数据。因此,它遵循装饰器模式,使用新的 Endless Technology(TM) 扩充您当前的适配器。

    所以,首先,创建一个常规的ArrayAdapter,并获得所有你想要的样式和东西(例如,你的getView() 实现)。然后,创建一个添加“无尽”的EndlessAdapter 子类。

    注意:

    • EndlessAdapter 子类不应覆盖 getView(),因为这是添加了无限行为的地方

    • EndlessAdapter 子类不应覆盖 getCount(),因为它由 EndlessAdapter 实现本身管理

    您可能希望检查 the sample app 以了解它如何实现 EndlessAdapter 子类。

    或者,由于EndlessAdapter will not work with header views,您可能只需要切换到不同的无限列表解决方案或自行推出。

    【讨论】:

    • 好的,感谢您的上述建议,我已经开始工作了。请解决一个小问题。我在 cacheInBackground 中做 getQuestions()。和 adapter.setRunInBackground(false);在我得到修改后的数组 readyListener.onItemsReady(questions); 之后,在我的 getQuestion() 异步任务中工作正常。但是如果我没有得到任何东西,即我的数组没有被修改,cacheInBackground 会无限重复。怎么办?
    • @MuhammadUmar:当您到达数据末尾时,您应该从cacheInBackground() 返回false
    猜你喜欢
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2014-10-21
    相关资源
    最近更新 更多