Android无限滚动列表双向滚动

6
我正在尝试实现一个无限滚动列表,它将显示日历和事件,并从现在或选定日期开始。它应该可以在过去和未来两个方向上滚动。如果我只需要到达未来(索引增长),则这里的OnScrollListener解决方案似乎非常有效。但是我不知道如何回到过去。
对于我的情况,此解决方案似乎非常浪费。getView被调用了数千次。也许ListView不是解决方案,我可能必须使用更低级别的代码。有什么想法吗?
编辑:getView被调用数千次并不是后一种解决方案的问题。但是,它仍然被错误地调用了太多次。如果我像这样设置选择:
myList.setSelection(Integer.MAX_VALUE/2)
我会得到从零开始的索引的getView调用。例如,我会得到以下类似的getView调用:
getView pos 0
...
getView pos 26

并且,然后。
getView pos 1073741823
...
getView pos 1073741847

哪些是正确的。然后:

getView pos 0
...
getView pos 26

再试一次

这个问题出现在我还没有滚动或触摸屏幕之前。看起来没有太多意义。


1
你发布的第二个链接是正确的解决方案。 - pskink
在测试时,我的适配器的getView在我甚至还没有滚动之前就被调用了2625次。这相当沉重,特别是考虑到我需要加载日历事件,这意味着IO。有什么办法可以将getView调用减少到仅限于可见区域或者稍微大一点的区域吗? - auramo
所以你使用了 https://github.com/commonsguy/cwac-endless,但它效果很差? - pskink
不,我没有使用 cwac。我的印象是它只会朝一个方向无限增长。我错了吗? - auramo
一年过去了,你最终使用了什么,@auramo? - SudoPlz
1个回答

1
这是此任务的实现代码。

EndlessScrollBaseAdapter.java

package com.example.endlessscrollinbothdirections;

import java.util.Map;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

/** A child class shall subclass this Adapter and implement method getDataRow(int position,
 * View convertView, ViewGroup parent), which supplies a View present data in a ListRow.
 * This parent Adapter takes care of displaying ProgressBar in a row or indicating that it
 * has reached the last row. */
public abstract class EndlessScrollBaseAdapter<T> extends BaseAdapter implements
        OnScrollListener {
    private int mVisibleThreshold = 5;
    // the main data structure to save loaded data
    protected Map<Integer, T> mItems;
    protected Context mContext;
    // the serverListSize is the total number of items on the server side,
    // which should be returned from the web request results
    protected int mServerListSize = -1;
    // Two view types which will be used to determine whether a row should be displaying
    // data or a Progressbar
    public static final int VIEW_TYPE_LOADING = 0;
    public static final int VIEW_TYPE_ACTIVITY = 1;
    public static final int VIRTUAL_MIDDLE_OFFSET = Integer.MAX_VALUE / 2;

    public EndlessScrollBaseAdapter(Context context, Map<Integer, T> items) {
        mContext = context;
        mItems = items;
    }

    public void setServerListSize(int serverListSize) {
        this.mServerListSize = serverListSize;
    }

    /** disable click events on indicating rows */
    @Override
    public boolean isEnabled(int position) {
        return getItemViewType(position) == EndlessScrollBaseAdapter.VIEW_TYPE_ACTIVITY;
    }

    /** One type is normal data row, the other type is Progressbar */
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    /** the size of the List plus one, the one is the last row, which displays a
     * Progressbar */
    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    /** return the type of the row, the last row indicates the user that the ListView is
     * loading more data */
    @Override
    public int getItemViewType(int position) {
        return mItems.containsKey(position
                - EndlessScrollBaseAdapter.VIRTUAL_MIDDLE_OFFSET) ? EndlessScrollBaseAdapter.VIEW_TYPE_ACTIVITY
                : EndlessScrollBaseAdapter.VIEW_TYPE_LOADING;
    }

    @Override
    public T getItem(int position) {
        return mItems.get(position - EndlessScrollBaseAdapter.VIRTUAL_MIDDLE_OFFSET);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /** returns the correct view */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (getItemViewType(position) == EndlessScrollBaseAdapter.VIEW_TYPE_LOADING) {
            return getFooterView(position, convertView, parent);
        }
        return getDataRow(position, convertView, parent);
    };

    /** A subclass should override this method to supply the data row.
     *
     * @param position
     * @param convertView
     * @param parent
     * @return */
    public abstract View getDataRow(int position, View convertView, ViewGroup parent);

    /** returns a View to be displayed in the last row.
     *
     * @param position
     * @param convertView
     * @param parent
     * @return */
    public View getFooterView(int position, View convertView, ViewGroup parent) {
        if (position >= mServerListSize && mServerListSize > 0) {
            // the ListView has reached the last row
            TextView tvLastRow = new TextView(mContext);
            tvLastRow.setHint("Reached the last row.");
            tvLastRow.setGravity(Gravity.CENTER);
            return tvLastRow;
        } else {
            TextView tvLastRow = new TextView(mContext);
            tvLastRow.setHint("Loading...\n position: " + position);
            tvLastRow.setGravity(Gravity.CENTER);
            return tvLastRow;
        }
    }

    // Defines the process for actually loading more data based on page
    public abstract void onLoadMore(int virtualPosition);

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
            int totalItemCount) {
        for (int i = -mVisibleThreshold; i < visibleItemCount + mVisibleThreshold; i++) {
            int virtualPosition = firstVisibleItem
                    - EndlessScrollBaseAdapter.VIRTUAL_MIDDLE_OFFSET + i;
            onLoadMore(virtualPosition);
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }
}

EndlessScrollAdapter.java

package com.example.endlessscrollinbothdirections;

import java.util.Map;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class EndlessScrollAdapter extends EndlessScrollBaseAdapter<Integer> {
    public EndlessScrollAdapter(Activity activity, Map<Integer, Integer> list) {
        super(activity, list);
    }

    @Override
    public View getDataRow(int position, View convertView, ViewGroup parent) {
        TextView TextView;
        if (convertView == null) {
            TextView = new TextView(mContext);
        } else {
            TextView = (TextView) convertView;
        }
        TextView.setText("virtualPosition: "
                + (position - EndlessScrollBaseAdapter.VIRTUAL_MIDDLE_OFFSET) + "\n"
                + "row data: "
                + mItems.get(position - EndlessScrollBaseAdapter.VIRTUAL_MIDDLE_OFFSET));
        return TextView;
    }

    @Override
    public void onLoadMore(int virtualPosition) {
        // here you might launch an AsyncTask instead
        if (!mItems.containsKey(virtualPosition)) {
            mItems.put(virtualPosition, virtualPosition);
            notifyDataSetChanged();
        }
    }
}

MainActivity.java

package com.example.endlessscrollinbothdirections;

import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.lvItems);
        Map<Integer, Integer> items = new HashMap<Integer, Integer>();
        EndlessScrollAdapter endlessScrollAdapter = new EndlessScrollAdapter(this, items);
        listView.setAdapter(endlessScrollAdapter);
        listView.setSelection(EndlessScrollBaseAdapter.VIRTUAL_MIDDLE_OFFSET);
        listView.setOnScrollListener(endlessScrollAdapter);
    }
}

activity_main.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lvItems"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接