Skip to content

Commit

Permalink
支持 SnapColumns 特性
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanNagihong committed Jul 28, 2021
1 parent 83f92be commit 27b9b13
Show file tree
Hide file tree
Showing 16 changed files with 368 additions and 36 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<activity android:name=".lesson6.Lesson6Activity" />
<activity android:name=".lesson7.Lesson7Activity" />
<activity android:name=".lesson8.Lesson8Activity" />
<activity android:name=".lesson9.Lesson9Activity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cn.jingzhuan.tableview.demo.lesson9

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProviders
import cn.jingzhuan.tableview.demo.R
import cn.jingzhuan.tableview.demo.databinding.ActivityLesson9Binding

class Lesson9Activity : AppCompatActivity() {

private lateinit var binding: ActivityLesson9Binding
private lateinit var viewModel: Lesson9ViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = "Lesson 9"
binding = DataBindingUtil.setContentView(this, R.layout.activity_lesson_9)
viewModel = ViewModelProviders.of(this)[Lesson9ViewModel::class.java]

binding.tableView.updateTableSize(15, 1, 3)

viewModel.liveData.observe(this) {
binding.tableView.setHeaderRow(it)
binding.tableView.notifyDataSetChanged()
}

viewModel.fetch(this, 100, 15)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.jingzhuan.tableview.demo.lesson9

import cn.jingzhuan.tableview.demo.R
import cn.jingzhuan.tableview.demo.databinding.LayoutLesson9ViewColumnBinding
import cn.jingzhuan.tableview.element.DataBindingViewColumn
import cn.jingzhuan.tableview.element.Row

class Lesson9ViewColumn: DataBindingViewColumn<LayoutLesson9ViewColumnBinding>() {

override fun layoutId() = R.layout.layout_lesson_9_view_column

override fun onBind(binding: LayoutLesson9ViewColumnBinding, row: Row<*>) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cn.jingzhuan.tableview.demo.lesson9

import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.jingzhuan.tableview.demo.elements.*
import cn.jingzhuan.tableview.element.Column
import io.reactivex.Flowable
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

class Lesson9ViewModel : ViewModel() {

val liveData = MutableLiveData<TitleRow>()
private var disposable: Disposable? = null

fun fetch(context: Context, rowsCount: Int, columnsCount: Int) {
disposable = Flowable.fromCallable { columnsCount }
.map {

val titleColumns = mutableListOf<Column>()
titleColumns.add(TitleHeaderColumn())
for (i in 0 until columnsCount) {
val titleColumn = TitleColumn(i)
when (i) {
0 -> titleColumn.weight = 1
1 -> titleColumn.weight = 1
2 -> titleColumn.weight = 0
}
titleColumn.rightMargin = 10
titleColumns.add(titleColumn)
}
val titleRow = TitleRow(titleColumns)

for (rowIndex in 0 until rowsCount) {
val rowColumns = mutableListOf<Column>()
rowColumns.add(SimpleHeaderColumn("Row${rowIndex + 1}"))
for (columnIndex in 0 until columnsCount) {
if(columnIndex == 1) {
val column = Lesson9ViewColumn()
rowColumns.add(column)
} else {
val column = SimpleColumn("${rowIndex + 1} - ${columnIndex + 1}")
column.leftMargin = 40
column.rightMargin = 10
rowColumns.add(column)
}
}
titleRow.rows.add(SimpleRow(rowColumns))
}

// titleRow.preMeasureAllRows(context)
titleRow
}
.subscribeOn(Schedulers.computation())
.subscribe({
liveData.postValue(it)
}, {

})
}

override fun onCleared() {
super.onCleared()
disposable?.dispose()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import cn.jingzhuan.tableview.demo.lesson5.Lesson5Activity
import cn.jingzhuan.tableview.demo.lesson6.Lesson6Activity
import cn.jingzhuan.tableview.demo.lesson7.Lesson7Activity
import cn.jingzhuan.tableview.demo.lesson8.Lesson8Activity
import cn.jingzhuan.tableview.demo.lesson9.Lesson9Activity
import cn.jingzhuan.tableview.demo.lesson9.Lesson9ViewModel

class MainActivity : AppCompatActivity() {

Expand All @@ -32,6 +34,8 @@ class MainActivity : AppCompatActivity() {
adapter.data.add("Lesson 6 - sticky rows")
adapter.data.add("Lesson 7 - direction lock")
adapter.data.add("Lesson 8 - dynamic value")
adapter.data.add("Lesson 9 - snap columns")

adapter.onItemClick = {
when (it) {
0 -> startActivity(Intent(this, Lesson1Activity::class.java))
Expand All @@ -42,6 +46,7 @@ class MainActivity : AppCompatActivity() {
5 -> startActivity(Intent(this, Lesson6Activity::class.java))
6 -> startActivity(Intent(this, Lesson7Activity::class.java))
7 -> startActivity(Intent(this, Lesson8Activity::class.java))
8 -> startActivity(Intent(this, Lesson9Activity::class.java))
}
}
adapter.notifyDataSetChanged()
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_lesson_9.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<cn.jingzhuan.tableview.TableView
android:id="@+id/table_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</layout>
14 changes: 10 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</FrameLayout>

</layout>
30 changes: 30 additions & 0 deletions app/src/main/res/layout/layout_lesson_9_view_column.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="Lesson 9"
tools:ignore="RtlHardcoded" />

<ImageView
android:id="@+id/image_view"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="4dp"
android:src="@drawable/lion"
tools:ignore="ContentDescription,RtlHardcoded" />

</LinearLayout>

</layout>
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-bin.zip
31 changes: 24 additions & 7 deletions tableview/src/main/java/cn/jingzhuan/tableview/TableView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.support.annotation.ColorInt
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
Expand Down Expand Up @@ -85,12 +86,24 @@ open class TableView @JvmOverloads constructor(

setAdapter(adapter)
setStretchMode(false)
header.layoutManager = RowListLayoutManager(context) { dx, _ ->
headerRow?.layoutManager?.scrollHorizontallyBy(dx) ?: 0
}
main.layoutManager = RowListLayoutManager(context) { dx, _ ->
headerRow?.layoutManager?.scrollHorizontallyBy(dx) ?: 0
}
header.layoutManager = RowListLayoutManager(
context,
onScrollHorizontallyBy = { dx, _ ->
headerRow?.layoutManager?.scrollHorizontallyBy(dx) ?: 0
},
onHorizontalScrollStateChanged = { state, dx ->
headerRow?.layoutManager?.onHorizontalScrollStateChanged(state, dx) ?: false
},
)
main.layoutManager = RowListLayoutManager(
context,
onScrollHorizontallyBy = { dx, _ ->
headerRow?.layoutManager?.scrollHorizontallyBy(dx) ?: 0
},
onHorizontalScrollStateChanged = { state, dx ->
headerRow?.layoutManager?.onHorizontalScrollStateChanged(state, dx) ?: false
},
)
header.addOnScrollListener(scrollListener)
main.addOnScrollListener(scrollListener)
(header as DirectionLockRecyclerView).directionLockEnabled = true
Expand Down Expand Up @@ -132,7 +145,11 @@ open class TableView @JvmOverloads constructor(
}

fun updateTableSize(columns: Int, stickyColumnsCount: Int) {
columnsLayoutManager.updateTableSize(columns, stickyColumnsCount)
updateTableSize(columns, stickyColumnsCount, 0)
}

fun updateTableSize(columns: Int, stickyColumnsCount: Int, snapColumnsCount: Int) {
columnsLayoutManager.updateTableSize(columns, stickyColumnsCount, snapColumnsCount)
}

fun setRowsDividerEnabled(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,19 @@ open class HeaderRow<COLUMN : Column>(columns: List<COLUMN>) : Row<COLUMN>(colum

fun preMeasureAllRows(context: Context) {
val layoutManager = layoutManager ?: return
measure(context, layoutManager.specs)
stickyRows.forEach { it.measure(context, layoutManager.specs) }
rows.forEach { it.measure(context, layoutManager.specs) }
var columnsWidthChanged = false
columnsWidthChanged = columnsWidthChanged or measure(context, layoutManager.specs)
stickyRows.forEach { columnsWidthChanged = columnsWidthChanged or it.measure(context, layoutManager.specs) }
rows.forEach { columnsWidthChanged = columnsWidthChanged or it.measure(context, layoutManager.specs) }
if(columnsWidthChanged) getTableSpecs()?.onColumnsWidthChanged()
}

fun preMeasureRow(context: Context, row: Row<*>) {
val layoutManager = layoutManager ?: return
if (row.measure(context, layoutManager.specs)) row.forceLayout = true
if (row.measure(context, layoutManager.specs)) {
getTableSpecs()?.onColumnsWidthChanged()
row.forceLayout = true
}
}

fun preLayoutRowIfNecessary(context: Context, row: Row<*>) {
Expand Down
19 changes: 10 additions & 9 deletions tableview/src/main/java/cn/jingzhuan/tableview/element/Row.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import cn.jingzhuan.tableview.layoutmanager.ColumnsLayoutManager
import cn.jingzhuan.tableview.layoutmanager.TableSpecs
import cn.jingzhuan.tableview.listeners.OnRowClickListener
import cn.jingzhuan.tableview.listeners.OnRowLongClickListener
import timber.log.Timber
import java.io.ObjectInputStream
import kotlin.math.max
import kotlin.math.min
Expand Down Expand Up @@ -169,8 +170,7 @@ abstract class Row<COLUMN : Column>(var columns: List<COLUMN>) :
for (i in 0 until maxSize) {
if (i == specs.stickyColumnsCount) x = 0
val column = columns[i]
layoutColumn(context, i, column, x, rowHeight, specs)
x = column.columnRight
x = layoutColumn(context, i, column, x, rowHeight, specs)
}
}

Expand All @@ -192,10 +192,9 @@ abstract class Row<COLUMN : Column>(var columns: List<COLUMN>) :
for (i in 0 until specs.stickyColumnsCount) {
if (!specs.isColumnVisible(i)) continue
val column = columns[i]
layoutColumn(context, i, column, x, rowHeight, specs)
x = layoutColumn(context, i, column, x, rowHeight, specs)
if (column is DrawableColumn) column.draw(context, canvas, rowShareElements)
drawColumnsDivider(canvas, column, specs)
x = column.columnRight
}
}

Expand All @@ -212,15 +211,14 @@ abstract class Row<COLUMN : Column>(var columns: List<COLUMN>) :
for (i in startIndex until specs.columnsCount) {
if (!specs.isColumnVisible(i)) continue
val column = columns[i]
layoutColumn(context, i, column, x, rowHeight, specs)
x = layoutColumn(context, i, column, x, rowHeight, specs)
if (column is DrawableColumn) {
if (column.columnLeft > container.scrollX + container.width) break
if (!column.shouldIgnoreDraw(container)) {
column.draw(context, canvas, rowShareElements)
}
}
drawColumnsDivider(canvas, column, specs)
x = column.columnRight
}
}

Expand Down Expand Up @@ -290,10 +288,12 @@ abstract class Row<COLUMN : Column>(var columns: List<COLUMN>) :
x: Int,
rowHeight: Int,
specs: TableSpecs
) {
column.columnLeft = x
): Int {
val snapWidth = if(specs.snapColumnsCount > 0 && index >= specs.stickyColumnsCount) specs.getSnapWidth() else 0
val columnLeft = x - snapWidth
column.columnLeft = columnLeft
column.columnTop = 0
column.columnRight = x + specs.visibleColumnsWidth[index]
column.columnRight = columnLeft + specs.visibleColumnsWidth[index]
column.columnBottom = rowHeight

val top: Int
Expand Down Expand Up @@ -338,6 +338,7 @@ abstract class Row<COLUMN : Column>(var columns: List<COLUMN>) :
if (column is DrawableColumn) {
column.prepareToDraw(context, rowShareElements)
}
return column.columnRight + snapWidth
}

private fun drawColumnsDivider(canvas: Canvas, column: Column, specs: TableSpecs) {
Expand Down
Loading

0 comments on commit 27b9b13

Please sign in to comment.