Skip to content

Commit

Permalink
支持分页加载模式,毫无烦恼解决卡顿问题.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellen2018 committed Jul 11, 2019
1 parent ab34cd5 commit 6bacde7
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 30 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".table2.Table2Activity">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridLayout;
import android.widget.TextView;

import com.ellen.supertableview.R;
Expand Down Expand Up @@ -50,7 +51,7 @@ protected MyItemViewHolder createTableItemViewHolder(int row, int column) {

@Override
protected void bindTableItemViewHolder(MyItemViewHolder myItemViewHolder, int row, int column) {
myItemViewHolder.tv.setText(String.valueOf(row));
myItemViewHolder.tv.setText("("+row+","+column+")");
}

@Override
Expand All @@ -60,7 +61,12 @@ public int getTableColumn() {

@Override
public int getTableRow() {
return rowNumber;
return 200;
}

@Override
public void bindAdapter() {
super.bindAdapter();
}

public static class XItemViewHolder extends XYItemViewHolder{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.widget.Toast;

import com.ellen.supertableview.R;
import com.ellen.tableview.supertableview.PagingMode;
import com.ellen.tableview.supertableview.TableClick;
import com.ellen.tableview.supertableview.TableItemView;
import com.ellen.tableview.supertableview.TableView;
Expand Down Expand Up @@ -49,19 +50,20 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
View view = inflater.from(getActivity()).inflate(R.layout.fragment_table, container, false);
tableView = view.findViewById(R.id.tableView);
superTableViewAdapter = new TableAdapter(getActivity(), xTitles, itemTitles);
superTableViewAdapter.setRowNumber(aa);
PagingMode pagingMode = new PagingMode(true,20,10,2);
tableView.setPagingMode(pagingMode);
tableView.setTableViewAdapter(superTableViewAdapter);
tableView.setOnVScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
Log.e("执行没?","ok");
if(scrollY+tableView.getHeight() >= (tableView.getRowNumber()-5)*tableView.getItemHeight()){
for(int i=0;i<10;i++) {
superTableViewAdapter.addRow();
}
}
}
});
// tableView.setOnVScrollChangeListener(new View.OnScrollChangeListener() {
// @Override
// public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
// Log.e("执行没?","ok");
// if(scrollY+tableView.getHeight() >= (tableView.getRowNumber()-5)*tableView.getItemHeight()){
// for(int i=0;i<10;i++) {
// superTableViewAdapter.addRow();
// }
// }
// }
// });
initView();
return view;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.ellen.tableview.supertableview;

public class PagingMode {

/**
* 分页的方向
*/
private boolean isVer;
/**
* 第一次加载的数据个数
*/
private int firstSize;
/**
* 每次分页加载的个数
*/
private int addSize;

private int loadBeforeSize = 1;

public int getLoadBeforeSize() {
return loadBeforeSize;
}

public void setLoadBeforeSize(int loadBeforeSize) {
this.loadBeforeSize = loadBeforeSize;
}

public PagingMode(boolean isVer, int firstSize, int addSize, int loadBeforeSize) {
this.isVer = isVer;
this.firstSize = firstSize;
this.addSize = addSize;
this.loadBeforeSize = loadBeforeSize;
}

public boolean isVer() {
return isVer;
}

public void setVer(boolean ver) {
isVer = ver;
}

public int getFirstSize() {
return firstSize;
}

public void setFirstSize(int firstSize) {
this.firstSize = firstSize;
}

public int getAddSize() {
return addSize;
}

public void setAddSize(int addSize) {
this.addSize = addSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.ellen.tableview.R;
import com.ellen.tableview.supertableview.adapter.TableViewAdapter;
import com.ellen.tableview.supertableview.adapter.superadapter.SuperTableAdapter;
import com.ellen.tableview.supertableview.adapter.superadapter.TableHorizontalScrollView;

import java.util.ArrayList;
Expand Down Expand Up @@ -59,6 +60,7 @@ public class TableView extends RelativeLayout {
//
private OnScrollChangeListener onHScrollChangeListener;
private OnScrollChangeListener onVScrollChangeListener;
private PagingMode pagingMode = null;

//列数
private int columnNumber = 10;
Expand Down Expand Up @@ -222,6 +224,14 @@ public void setOnTouchListener(OnTouchListener onTouchListener) {
horizontalScrollView_x.setOnTouchListener(onTouchListener);
}

public PagingMode getPagingMode() {
return pagingMode;
}

public void setPagingMode(PagingMode pagingMode) {
this.pagingMode = pagingMode;
}

public int getColumnNumber() {
return columnNumber;
}
Expand Down Expand Up @@ -371,10 +381,6 @@ public void setTableViewAdapter(TableViewAdapter tableViewAdapter) {
this.tableViewAdapter.setCloumn(columnNumber);
this.tableViewAdapter.setGridLayout(gridLayoutTable);
this.tableViewAdapter.setTableView(this);
setColumnNumber(tableViewAdapter.getTableColumn());
setRowNumber(tableViewAdapter.getTableRow());
getGridLayoutY().setRowCount(tableViewAdapter.getTableRow());
getGridLayoutX().setColumnCount(tableViewAdapter.getTableColumn());
this.tableViewAdapter.bindAdapter();
View xyView = tableViewAdapter.createXYView();
if (xyView == null) {
Expand Down Expand Up @@ -420,16 +426,40 @@ public void onClick(View v) {
setItemOnClick(view, -1, i);
}
}
for (int i = 0; i < tableViewAdapter.getItemCount(); i++) {
int itemCountCopy = 0;
if (pagingMode != null) {
if (pagingMode.isVer()) {
itemCountCopy = pagingMode.getFirstSize() * tableViewAdapter.getTableColumn();
setColumnNumber(tableViewAdapter.getTableColumn());
setRowNumber(pagingMode.getFirstSize());
getGridLayoutY().setRowCount(pagingMode.getFirstSize());
getGridLayoutX().setColumnCount(tableViewAdapter.getTableColumn());
} else {
itemCountCopy = pagingMode.getFirstSize() * tableViewAdapter.getTableRow();
Log.e("首次加载",pagingMode.getFirstSize()+"");
Log.e("预加载",tableViewAdapter.getTableRow()+"");
setColumnNumber(pagingMode.getFirstSize());
setRowNumber(tableViewAdapter.getTableRow());
getGridLayoutY().setRowCount(tableViewAdapter.getTableRow());
getGridLayoutX().setColumnCount(pagingMode.getFirstSize());
}
} else {
itemCountCopy = tableViewAdapter.getItemCount();
setColumnNumber(tableViewAdapter.getTableColumn());
setRowNumber(tableViewAdapter.getTableRow());
getGridLayoutY().setRowCount(tableViewAdapter.getTableRow());
getGridLayoutX().setColumnCount(tableViewAdapter.getTableColumn());
}

for (int i = 0; i < itemCountCopy; i++) {
itemCount++;
View view = null;
if(gridLayoutTable.getOrientation() == GridLayout.VERTICAL){
if (gridLayoutTable.getOrientation() == GridLayout.VERTICAL) {
//竖直方向
Log.e("执行了吗","ok");
final int column = getRow(itemCount - 1, columnNumber);
final int row = getColumn(itemCount - 1, getRow(itemCount - 1, columnNumber), columnNumber);
int row = (itemCount-1) % rowNumber;
int column = (itemCount-1) / rowNumber;
view = tableViewAdapter.createItemView(i, row, column);
}else {
} else {
//水平方向
final int row = getRow(itemCount - 1, columnNumber);
final int column = getColumn(itemCount - 1, getRow(itemCount - 1, columnNumber), columnNumber);
Expand Down Expand Up @@ -477,6 +507,17 @@ public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int
if (onVScrollChangeListener != null) {
onVScrollChangeListener.onScrollChange(v, scrollX, scrollY, oldScrollX, oldScrollY);
}
if (pagingMode != null && pagingMode.isVer()) {
TableView tableView = TableView.this;
if(scrollY+tableView.getHeight() > (tableView.getRowNumber()-pagingMode.getLoadBeforeSize())*tableView.getItemHeight()) {
if (tableView.getRowNumber() < tableViewAdapter.getTableRow()) {
for (int i = 0; i < pagingMode.getAddSize(); i++) {
SuperTableAdapter superTableAdapter = (SuperTableAdapter) tableViewAdapter;
superTableAdapter.addRow();
}
}
}
}
}
});
horizontalScrollView = view.findViewById(R.id.horizontalScrollView);
Expand All @@ -488,6 +529,18 @@ public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int
if (onHScrollChangeListener != null) {
onHScrollChangeListener.onScrollChange(v, scrollX, scrollY, oldScrollX, oldScrollY);
}
//分页加载模式
if (pagingMode != null && !pagingMode.isVer()) {
TableView tableView = TableView.this;
if(scrollX+tableView.getWidth()> (tableView.getColumnNumber()-pagingMode.getLoadBeforeSize())*tableView.getItemWidth()) {
if (tableView.getColumnNumber() < tableViewAdapter.getTableColumn()) {
for (int i = 0; i < pagingMode.getAddSize(); i++) {
SuperTableAdapter superTableAdapter = (SuperTableAdapter) tableViewAdapter;
superTableAdapter.addColumn();
}
}
}
}
}
});
horizontalScrollView_x.setOnScrollChangeListener(new OnScrollChangeListener() {
Expand All @@ -497,6 +550,7 @@ public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int
if (onHScrollChangeListener != null) {
onHScrollChangeListener.onScrollChange(v, scrollX, scrollY, oldScrollX, oldScrollY);
}
//分页加载模式
}
});
mapColumn = new HashMap<>();
Expand All @@ -517,10 +571,10 @@ public int getColumn(int position, int row, int cloumn) {
public void addItem(final View view) {
int row = 0;
int column = 0;
if(gridLayoutTable.getOrientation() == GridLayout.VERTICAL){
column = getRow(itemCount - 1, columnNumber);
row = getColumn(itemCount - 1, getRow(itemCount - 1, columnNumber), columnNumber);
}else {
if (gridLayoutTable.getOrientation() == GridLayout.VERTICAL) {
row = (itemCount-1) % rowNumber;
column = (itemCount-1) / rowNumber;
} else {
row = getRow(itemCount - 1, columnNumber);
column = getColumn(itemCount - 1, getRow(itemCount - 1, columnNumber), columnNumber);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ protected void bindItemViewHolder(ItemViewHolder itemViewHolder, int row, int co

@Override
public void bindAdapter() {
getTableView().getGridLayoutTable().setOrientation(GridLayout.VERTICAL);
getTableView().hideYAxis();
setOrientationV(false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ellen.tableview.supertableview.adapter.superadapter.y;

import android.view.View;
import android.widget.GridLayout;

import com.ellen.tableview.supertableview.adapter.superadapter.ItemViewHolder;
import com.ellen.tableview.supertableview.adapter.superadapter.SuperTableAdapter;
Expand Down Expand Up @@ -45,6 +46,7 @@ protected void bindItemViewHolder(ItemViewHolder itemViewHolder, int row, int co
public void bindAdapter() {
getTableView().hideXAxis();
setOrientationV(true);
getTableView().getGridLayoutTable().setOrientation(GridLayout.VERTICAL);
}

@Override
Expand Down

0 comments on commit 6bacde7

Please sign in to comment.