Skip to content

Commit 9272fdc

Browse files
committed
2021-3-16 15:01:10
1 parent 0fc76be commit 9272fdc

File tree

11 files changed

+429
-181
lines changed

11 files changed

+429
-181
lines changed

app/src/main/java/com/boylab/example/MainActivity.java

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import androidx.appcompat.app.AppCompatActivity;
44

55
import android.os.Bundle;
6-
import android.util.Log;
76
import android.view.View;
7+
import android.widget.Toast;
88

99
import com.boylab.example.bean.Student;
1010
import com.boylab.example.bean.StudentLabel;
@@ -24,19 +24,77 @@ protected void onCreate(Bundle savedInstanceState) {
2424
/**
2525
* 查询所得数据
2626
*/
27-
for (int i = 0; i < 50; i++) {
27+
for (int i = 0; i < 30; i++) {
2828
mTableDatas.add(new Student(i));
2929
}
3030

3131
TableView tableView = findViewById(R.id.tableView);
32+
/**
33+
* 可设置一些参数
34+
*/
3235
tableView.setTableData(new StudentLabel(), mTableDatas);
3336

34-
/*tableView.setmOnItemClickListenter(new TableView.OnItemClickListenter() {
37+
/**
38+
* 点击监听
39+
*/
40+
tableView.setmOnItemClickListenter(new TableView.OnItemClickListenter() {
3541
@Override
3642
public void onItemClick(View item, int position) {
43+
Toast.makeText(MainActivity.this, "onItemClick: "+position, Toast.LENGTH_SHORT).show();
44+
}
45+
});
46+
47+
48+
/**
49+
* 下拉刷新(用的少)
50+
*/
51+
tableView.setOnRefreshListener(new TableView.OnTableRefreshListener() {
52+
@Override
53+
public void onRefresh(TableView mTableView) {
54+
mTableDatas.clear();
55+
56+
for (int i = 0; i < 30; i++) {
57+
mTableDatas.add(new Student(i));
58+
}
59+
60+
//刷新TableView即可
61+
mTableView.notifyDataSetChanged();
62+
63+
//完成刷新
64+
mTableView.finishRefresh();
65+
66+
if (mTableDatas.size() >= 30){
67+
//放开上拉加载
68+
mTableView.setEnableLoadMore(true);
69+
}
70+
}
71+
});
72+
73+
74+
/**
75+
* 上拉加载
76+
*/
77+
tableView.setOnLoadMoreListener(new TableView.OnTableLoadMoreListener() {
78+
@Override
79+
public void onLoadMore(TableView mTableView) {
80+
81+
int updateSize = (mTableDatas.size() < 100) ? 30 : 20;
82+
for (int i = 0; i < updateSize; i++) {
83+
mTableDatas.add(new Student(i));
84+
}
85+
86+
//刷新TableView即可
87+
mTableView.notifyDataSetChanged();
88+
89+
//完成上拉加载
90+
mTableView.finishLoadMore();
3791

92+
if (updateSize < 30){
93+
//关闭上拉加载
94+
mTableView.setEnableLoadMore(false);
95+
}
3896
}
39-
});*/
97+
});
4098

4199
}
42100
}

app/src/main/java/com/boylab/example/bean/Student.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Student(int i) {
2525
/**
2626
* 模拟初始化数据
2727
*/
28-
this.id = Long.valueOf(100 + i * i);
28+
this.id = (long) i;
2929
this.name = "名字" + i;
3030
this.age = i * i;
3131
this.sex = (i % 3 == 0 ? "boy" : "girl");
@@ -157,8 +157,6 @@ public String get(int position) {
157157
/**
158158
* position 对应的值 与 Label的标签对应上即可
159159
*/
160-
position = position % 12;
161-
162160
if (position == 0) {
163161
return String.valueOf(id);
164162
} else if (position == 1) {

app/src/main/java/com/boylab/example/bean/StudentLabel.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
import com.boylab.protocol.ItemRow;
44

55
import java.util.ArrayList;
6-
import java.util.HashMap;
76
import java.util.List;
87

98
public class StudentLabel implements ItemRow {
109

11-
public static final int SIZE = 24;
10+
public static final int SIZE = 13;
1211
private final List<String> label = new ArrayList<String>(){{
1312
add("序号");
1413
add("姓名");
@@ -32,7 +31,6 @@ public int size() {
3231

3332
@Override
3433
public String get(int position) {
35-
position = position % label.size();
3634
if (position < size()){
3735
return label.get(position);
3836
}

app/src/main/res/layout/activity_main.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
8-
8+
99
<com.boylab.scrolltable.TableView
1010
android:id="@+id/tableView"
1111
android:layout_width="match_parent"
1212
android:layout_height="match_parent"
13-
app:rowWidths="@array/WithList"/>
13+
app:contentGravity="center"
14+
app:rowWidths="@array/WithList" />
1415

1516
</RelativeLayout>

app/src/main/res/values/strings.xml

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,20 @@
22
<string name="app_name">ScrollTable</string>
33

44
<integer-array name="WithList">
5-
<item>160</item>
6-
<item>160</item>
7-
<item>160</item>
8-
<item>160</item>
9-
<item>160</item>
10-
<item>160</item>
11-
<item>160</item>
12-
<item>160</item>
13-
<item>160</item>
14-
<item>200</item>
15-
<item>200</item>
16-
<item>200</item>
17-
<item>200</item>
18-
<item>160</item>
19-
<item>160</item>
20-
<item>160</item>
21-
<item>160</item>
22-
<item>160</item>
23-
<item>160</item>
24-
<item>160</item>
25-
<item>160</item>
26-
<item>160</item>
5+
<item>80</item>
6+
<item>80</item>
7+
<item>80</item>
8+
<item>80</item>
9+
<item>80</item>
10+
<item>80</item>
11+
<item>80</item>
12+
<item>80</item>
13+
<item>80</item>
14+
<item>120</item>
15+
<item>120</item>
16+
<item>120</item>
17+
<item>80</item>
18+
<item>80</item>
2719
</integer-array>
2820

2921
</resources>

mylibrary/src/main/java/com/boylab/scrolltable/SynScrollerLayout.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,11 @@
99

1010
import java.util.ArrayList;
1111

12-
/**
13-
* -------- 日期 ---------- 维护人 ------------ 变更内容 --------
14-
* 2018/9/14 16:55 刘泽 新增 类
15-
* 2018/9/14 16:55 刘泽 增加yyy属性
16-
*/
1712
class SynScrollerLayout extends HorizontalScrollView {
1813
private ItemObserverable sObserverable = new ItemObserverable();
1914
private int mPosition = -1;
2015
private View mItemView;
2116
private OnItemClickListener mOnItemClickListener;
22-
/*private int mNomarlColor = getDrawingCacheBackgroundColor();
23-
private int mSelectColor = Color.GRAY;*/
24-
2517

2618
private float mStartX;
2719
private float mStartY;
@@ -38,14 +30,6 @@ public SynScrollerLayout(Context context, AttributeSet attrs, int defStyleAttr)
3830
super(context, attrs, defStyleAttr);
3931
}
4032

41-
/*public void setNomarlColor(int nomarlColor) {
42-
mNomarlColor = nomarlColor;
43-
}
44-
45-
public void setSelectColor(int selectColor) {
46-
mSelectColor = selectColor;
47-
}*/
48-
4933
@Override
5034
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5135
super.onScrollChanged(l, t, oldl, oldt);
@@ -102,7 +86,6 @@ private void actionMove(MotionEvent event) {
10286
float dx = Math.abs(x - mStartX);
10387
if (dx > 50 || dy > 0) {
10488
if (mItemView != null) {
105-
//mItemView.setBackgroundColor(mNomarlColor);
10689
removeCallbacks(mMoveAction);
10790
}
10891
}
@@ -116,17 +99,12 @@ private void actionUp(MotionEvent event) {
11699
float dx = Math.abs(x - mStartX);
117100
if (dx < 50 && dy < 50) {
118101
if (mPosition != -1 && mItemView != null) {
119-
//mItemView.setBackgroundColor(mSelectColor);
120-
121102
postDelayed(mUpAction, 50);
122103
if (mOnItemClickListener != null) {
123104
mOnItemClickListener.onClick(mItemView, mPosition);
124105
}
125106
}
126107
} else {
127-
/*if (mItemView != null) {
128-
mItemView.setBackgroundColor(mNomarlColor);
129-
}*/
130108
mPosition = -1;
131109
mItemView = null;
132110
}
@@ -136,7 +114,6 @@ private void actionUp(MotionEvent event) {
136114
@Override
137115
public void run() {
138116
if (mItemView != null) {
139-
//mItemView.setBackgroundColor(mNomarlColor);
140117
mPosition = -1;
141118
mItemView = null;
142119
removeCallbacks(mUpAction);
@@ -147,7 +124,6 @@ public void run() {
147124
@Override
148125
public void run() {
149126
if (mItemView != null) {
150-
//mItemView.setBackgroundColor(mSelectColor);
151127
}
152128
}
153129
};
@@ -201,8 +177,6 @@ private void notifiItemView(int l, int t, int oldl, int oldt) {
201177
for (OnItemScrollView itemScrollView : mItemScrollViews) {
202178
itemScrollView.OnScroll(l, t, oldl, oldt);
203179
}
204-
205-
206180
}
207181
}
208182

mylibrary/src/main/java/com/boylab/scrolltable/TableParams.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
import android.graphics.Color;
44

55
import com.boylab.protocol.ItemGravity;
6+
import com.boylab.utils.DensityUtil;
67

78
import java.util.HashMap;
89
import java.util.Set;
910

1011
class TableParams {
1112

13+
private DensityUtil densityUtil;
1214
public static final int TEXT_SIZE = 20;
1315
public static final int PADDING = 0;
14-
public static final int HEIGHT = 80;
15-
public static int WIDTH = 160;
16+
public static final int HEIGHT = 40;
17+
public static int WIDTH = 60;
1618

1719
//显示格式
1820
private int height = HEIGHT;
@@ -29,10 +31,8 @@ class TableParams {
2931

3032
private ItemGravity itemGravity = ItemGravity.CENTER;
3133

32-
public TableParams() {
33-
}
34-
3534
public TableParams(int backgroundColor) {
35+
densityUtil = DensityUtil.newInstance();
3636
this.height = HEIGHT;
3737
this.textSize = 20;
3838
this.textColor = Color.BLACK;
@@ -46,7 +46,7 @@ public TableParams(int backgroundColor) {
4646
}
4747

4848
public int getHeight() {
49-
return height;
49+
return densityUtil.dp2px(height);
5050
}
5151

5252
public void setHeight(int height) {
@@ -59,9 +59,9 @@ public HashMap<Integer, Integer> getItemWidth() {
5959

6060
public int getWidth(int column) {
6161
if (itemWidth.containsKey(column)){
62-
return itemWidth.get(column);
62+
return densityUtil.dp2px(itemWidth.get(column));
6363
}
64-
return WIDTH;
64+
return densityUtil.dp2px(WIDTH);
6565
}
6666

6767
@Deprecated

0 commit comments

Comments
 (0)