Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更改GridLayoutManager中每个偶数行的方向?_Java_Android_Kotlin_Android Recyclerview_Gridlayoutmanager - Fatal编程技术网

Java 如何更改GridLayoutManager中每个偶数行的方向?

Java 如何更改GridLayoutManager中每个偶数行的方向?,java,android,kotlin,android-recyclerview,gridlayoutmanager,Java,Android,Kotlin,Android Recyclerview,Gridlayoutmanager,我有layoutManager=GridLayoutManager(上下文,3) 如何使每条奇数线的方向从左到右,使每条偶数线的方向从右到左 看起来像: 0 -> 1 -> 2 5 <- 4 <- 3 6 -> 7 -> 8 etc... 0->1->2 5 8 等 伙计们,有什么好主意吗?如果你想要从右到左的布局,那么试试这个 您是否尝试在API级别17上使用android:layoutDirection=“rtl”执行代码?免责声明:我正在使用

我有
layoutManager=GridLayoutManager(上下文,3)

如何使每条奇数线的方向从左到右,使每条偶数线的方向从右到左

看起来像:

0 -> 1 -> 2

5 <- 4 <- 3

6 -> 7 -> 8

etc...
0->1->2
5  8
等

伙计们,有什么好主意吗?

如果你想要从右到左的布局,那么试试这个

您是否尝试在API级别17上使用android:layoutDirection=“rtl”执行代码?

免责声明:我正在使用手机,因此未经测试

如果您编写自己的
RecyclerView.Adapter
,则可以重写bind方法,以“按错误的顺序”查找数据:

Pos0:Data0->Pos1:Data1->Pos2:Data2
Pos3:Data5->Pos4:Data4->Pos5:Data3
Pos6:Data 6->Pos7:Data 7->Pos8:Data 8
Pos9:spacer->Pos10:spacer->Pos11:Data9

我假设您正在向适配器传递一些数据集合;我们还需要列数:

class SnakingAdapter(
    private val data: SomeCollection<SomeObject>, 
    private val cols: Int
) : RecyclerView.Adapter<SomeViewHolder> { ... }

请重新阅读我的问题,我只查找GridLayoutManager行,而不是所有应用程序。您是否可以覆盖适配器中的bind方法,以便它按以下顺序查找数据:
0,1,2,5,4,3,6,…
。如果你有6N+ 4项或6N+ 5项,你需要一个假的项目布局来填满开头的空格。
private val twoRows = cols * 2 // left > right & right > left
private val remainder = data.count() % twoRows

// If last row is an odd row => no "spacers"
// If last row is complete => no "spacers"
private val needSpacers = remainder > cols
private val paddedCount = 
    if(needSpacers) (data.count() / twoRows + 1) * twoRows
    else data.count()
private val firstSpacerPosition = data.count() / cols * cols
private val spacerPositions = 
    if(needSpacers) List(paddedCount - data.count()) {  firstSpacerPosition + it }
    else emptyList<Int>()

override fun getItemCount(): Int = paddedCount

override fun getItemViewType(position: Int): Int =
    if(spacerPositions.contains(position)) 2
    else 1

override onCreateViewHolder(parent: ViewGroup, viewType: Int) = when(viewType) { 
    1 => // create a normal item view & ViewHolder
    2 => // create a "spacer" item view & ViewHolder
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position Int) { 
    // If spacer, do nothing
    if(spacerPositions.contains(position)) return

    // If even row, flip the order of the data
    val twoRowPos = position % twoRows
    val rowTwomidpoint = cols / 2 + cols // TODO: extract as a field
    val altPosition = 
      if (twoRowPos >= cols) (position - 2 * (twoRowPos - rowTwoMidpoint)) // TODO: correct for odd cols; should fix for even cols
      else position

    val data = dataSource.get(altPosition)
    // bind data to view holder here
}