Raf*_*edo 6 android android-recyclerview
我正试图找到一种方法来实现一个体育应用程序的站立表(如NBA游戏时间排名),具有固定的标题,固定的第一列和页脚.我搜索了一下如何获得它,但最好的镜头是这个项目(https://github.com/InQBarna/TableFixHeaders),但它使用自己的视图而不是GridView的Recycler.有谁知道这样的事情或知道如何从它开始(适配器或LayoutManager)?
编辑(添加图片)


Raf*_*edo 11
经过大量的测试和搜索,我实现了自己的实现,结合了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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我用a创建了我的布局LinearLayout,包含我的标题和一个ListViewfor my Activity(或者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>
Run Code Online (Sandbox Code Playgroud)
我列表中的每个项目都是a LinearLayout,带有TextView(固定列)和a HorizontalScrollView.标题和行的布局如下:
<?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>
Run Code Online (Sandbox Code Playgroud)
诀窍是滚动所有借助a EventBus(我使用GreenRobot的一个)来触发水平滚动事件并将所有滚动器移动为一个.我的事件对象包含来自侦听器类的相同数据(也许我可以使用侦听器对象本身?)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
列表适配器类接收要在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;
}
}
Run Code Online (Sandbox Code Playgroud)
在继续之前,我注册MyHorizontalScrollView了EventBus,添加EventBus.getDefault().register(this)到每个版本的构造函数,并添加了接收器方法:
public void onEventMainThread(MainActivity.Event event) {
if (!event.getView().equals(this)) scrollTo(event.getX(), event.getY());
}
Run Code Online (Sandbox Code Playgroud)
这将滚动到收到的位置,如果它本身不是触发滚动事件.
最后,我用我的onCreate()方法设置了所有内容Activity:
@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));
}
Run Code Online (Sandbox Code Playgroud)
(请忽略一些奇怪的颜色,这是为了更好地查看正在发生的事情).
而ta-daa,这是理想的结果!

| 归档时间: |
|
| 查看次数: |
8593 次 |
| 最近记录: |