gpt4 book ai didi

android - 无限项的无限滚动

转载 作者:IT老高 更新时间:2023-10-28 23:32:12 27 4
gpt4 key购买 nike

我有一个 GridView,其中包含 5x50 内的项目。

我需要向各个方向滚动它们,而不是在到达末尾时停止,而是从顶部/左侧开始。

例如从左到右滚动

滚动前

1 2 3 4 5
6 7 8 9 10

向右滚动后

5 1 2 3 4
10 6 7 8 9

对于自上而下(或自下而上)

滚动到底部之前

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

滚动后

6 7 8 9 10
11 12 13 14 15
1 2 3 4 5

我尝试让它像 GridView 原生滚动一样平滑滚动。

最佳答案

activity_main.xml 中指定了以下 View 层次结构:

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

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

还有一个基本的列表项 - TextView,在 item.xml 内:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>

然后在 Activity 中:

public class MainActivity extends AppCompatActivity {

private static final int spanCount = 5;
private static final int totalItemCount = 15;

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

RecyclerView recyclerView = findViewById(R.id.recycler);
// Shamelessly stolen from devunwired bit.ly/2yCqVIp
recyclerView.addItemDecoration(new GridDividerDecoration(this));
recyclerView.setLayoutManager(new GridLayoutManager(this, spanCount, LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(new MyAdapter(totalItemCount));
}

static class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

private final int totalSizeOfItems;
private boolean hasBeenSetup = false;

MyAdapter(int totalSizeOfItems) {
this.totalSizeOfItems = totalSizeOfItems;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
final int cellSize = parent.getMeasuredWidth() / spanCount;
view.setMinimumWidth(cellSize);
view.setMinimumHeight(cellSize);
setupRecyclerHeightIfNeeded(parent, cellSize);
return new MyViewHolder(view);
}

// We need to perform this operation once, not each time `onCreateViewHolder` is called
private void setupRecyclerHeightIfNeeded(View parent, int cellSize) {
if (hasBeenSetup) return;
hasBeenSetup = true;
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) parent.getLayoutParams();
int numOfRows = (int) (totalItemCount / (double) spanCount);
params.height = numOfRows * cellSize;
new Handler().post(parent::requestLayout);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int pos) {
int position = holder.getAdapterPosition() % totalSizeOfItems;
holder.textView.setText(Integer.toString(position + 1));
}

@Override
public int getItemCount() {
// this will result the list to be "infinite"
return Integer.MAX_VALUE;
}
}

static class MyViewHolder extends RecyclerView.ViewHolder {

TextView textView;

MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView;
}
}
}

在输出时你会得到:

enter image description here

处理水平滚动需要进行少量更改:

  • GridLayoutManager 的方向应更改为 HORIZONTAL
  • 应替换适配器内部适当的宽度/高度 setter

除此之外 - 一切都应该相似。

关于android - 无限项的无限滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47242958/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com