具有固定列和 header 以及可滚动页脚的 RecyclerView

RecyclerView with fixed column and header, plus scrollable footer

我正在尝试找到一种方法来为体育应用程序实现排名 table(例如 NBA 比赛时间排名),具有固定的 header、固定的第一列和页脚。我搜索了一下如何获得它,但最好的镜头是这个项目 (https://github.com/InQBarna/TableFixHeaders),但它使用自己的视图而不是 GridView 的 Recycler。有谁知道这样的事情或知道我如何开始使用它(适配器或布局管理器)?

编辑(添加图像)

您可以实现这样的布局:

<LinearLayout
    ...
    android:orientation="vertical">

    <TextView 
        ... />

    <com.example.TableHeaderView 
        ... />

    <RecyclerView 
        ... />

</LinearLayout>

只有 RecyclerView 会滚动,将您的标题文本视图和 table header 留在屏幕顶部。

根据你的图片,我建议考虑使用 StickyGridHeaders 这样的库。它扩展 GridView 并且可以有自定义 'header' 视图。

备选方案是 StickyListHeaders or HeaderListView,但更关注 ListView

编辑:

经进一步调查,提供的示例 in this tutorial 似乎符合您的要求

经过大量测试和搜索,我自己实现了,将ListView与内部HorizontalScrollView组合在一起。

首先,我扩展了HorizontalScrollView来向我报告滚动事件,添加了一个监听器:

public class MyHorizontalScrollView extends HorizontalScrollView {

    private OnScrollListener listener;

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (listener != null) listener.onScroll(this, l, t);
    }

    public void setOnScrollListener(OnScrollListener listener) {
        this.listener = listener;
    }

    public interface OnScrollListener {
        void onScroll(HorizontalScrollView view, int x, int y);
    }       
}

然后,我使用 LinearLayout 创建布局,其中包含我的 header 和 ActivityListView(或者 Fragment,如果它是你的需要)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/header"
        layout="@layout/header" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

我列表中的每一项都是一个 LinearLayout,带有一个 TextView(固定列)和一个 HorizontalScrollView。 header 和行的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        style="?android:attr/textAppearanceMedium"
        android:background="#f00"
        android:minWidth="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <View
        android:layout_width="1px"
        android:layout_height="match_parent"
        android:background="@android:color/black" />

    <net.rafaeltoledo.example.MyHorizontalScrollView
        android:id="@+id/scroll"
        android:scrollbars="none"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <!-- Childs, or columns -->

        </LinearLayout>

    </net.rafaeltoledo.example.MyHorizontalScrollView>

</LinearLayout>

技巧是滚动所有 在 EventBus(我使用 GreenRobot's 的帮助下)触发水平滚动事件并将所有滚动条作为一个移动。我的事件 object 包含来自监听器 class 的相同数据(也许我可以使用监听器 object 本身?)

public static class Event {

    private final int x;
    private final int y;
    private final HorizontalScrollView view;

    public Event(HorizontalScrollView view, int x, int y) {
        this.x = x;
        this.y = y;
        this.view = view;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public HorizontalScrollView getView() {
        return view;
    }
}

列表适配器 class 接收一个侦听器以在每个项目的 HorizontalScrollView 中设置。

public static class Adapter extends BaseAdapter {

    private final Context context;
    private MyHorizontalScrollView.OnScrollListener listener;

    public Adapter(Context context, MyHorizontalScrollView.OnScrollListener listener) {
        this.context = context;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return 30;
    }

    @Override
    public Object getItem(int position) {
        return new Object();
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.header, parent, false);
            MyHorizontalScrollView scroll = (MyHorizontalScrollView) convertView.findViewById(R.id.scroll);
            scroll.setOnScrollListener(listener);
        }
        return convertView;
    }

    public Context getContext() {
        return context;
    }
}

继续之前,我注册了MyHorizontalScrollView到EventBus,在每个版本的constructor中添加了EventBus.getDefault().register(this),并在其中添加了receiver方法:

public void onEventMainThread(MainActivity.Event event) {
    if (!event.getView().equals(this)) scrollTo(event.getX(), event.getY());
}

它将滚动到接收到的位置,如果不是它本身触发了滚动事件。

最后,我在 ActivityonCreate() 方法中设置了所有内容:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    MyHorizontalScrollView.OnScrollListener listener = new MyHorizontalScrollView.OnScrollListener() {
        @Override
        public void onScroll(HorizontalScrollView view, int x, int y) {
            Log.d("Scroll Event", String.format("Fired! %d %d", x, y));
            EventBus.getDefault().post(new Event(view, x, y));
        }
    };

    ListView listView = (ListView) findViewById(R.id.list);

    ViewGroup header = (ViewGroup) findViewById(R.id.header);
    header.getChildAt(0).setBackgroundColor(Color.WHITE);
    header.setBackgroundColor(Color.BLUE);
    ((MyHorizontalScrollView) header.findViewById(R.id.scroll)).setOnScrollListener(listener);

    listView.setAdapter(new Adapter(this, listener));
    listView.addFooterView(getLayoutInflater().inflate(R.layout.footer, listView, false));
}

(请忽略一些奇怪的颜色,这是为了更好地查看正在发生的事情)。

和ta-daa,这是想要的结果!