Skip to content

Commit

Permalink
Refactor: notify data change by adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHubZJY committed Aug 6, 2020
1 parent d443586 commit 84ddd1e
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 34 deletions.
29 changes: 27 additions & 2 deletions app/src/main/java/com/zjy/xtableview/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zjy.xtableview;

import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -18,6 +19,7 @@ public class MainActivity extends AppCompatActivity {

private List<TableRowModel<TableRowHeaderModel, TableRowCellModel>> mDataList;
private TableHeaderModel mHeaderModel;
private CustomTableAdapter mAdapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand All @@ -30,9 +32,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
vTableView.setLongPressDragEnable(true);
vTableView.setSwipeEnable(true);

vTableView.setTableAdapter(new CustomTableAdapter(this));
mAdapter = new CustomTableAdapter(this);
mAdapter.bindData(mHeaderModel.getHeaderTitle(), mHeaderModel.getHeaderData(), mDataList);
vTableView.setTableAdapter(mAdapter);

//testNotify();

vTableView.bindData(mHeaderModel.getHeaderTitle(), mHeaderModel.getHeaderData(), mDataList);
}

private void initData() {
Expand Down Expand Up @@ -78,6 +83,26 @@ private void initData() {

}

private void testNotify() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
final TableRowModel<TableRowHeaderModel, TableRowCellModel> model = new TableRowModel<>();
model.setRowHeader(new TableRowHeaderModel("aa", "aa"));
List<TableRowCellModel> childData = new ArrayList<>();
childData.add(TableRowCellModel.createRiseCell("11"));
childData.add(TableRowCellModel.createRiseCell("11%"));
childData.add(TableRowCellModel.createRiseCell("11"));
childData.add(TableRowCellModel.createRiseCell("1"));
childData.add(TableRowCellModel.createFallCell("-11"));
childData.add(TableRowCellModel.createFallCell("-11%"));
model.setRowData(childData);
mAdapter.notifyItemData(4, model);
}
}, 2000);
}


@Override
protected void onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Date: 2020/8/5
* Author: Yang
* Describe:
* Describe: 定制表格样式
*/
public class CustomTableAdapter extends XTableAdapter<String, TableRowModel<TableRowHeaderModel, TableRowCellModel>>{

Expand Down
19 changes: 1 addition & 18 deletions xtableview/src/main/java/com/zjy/xtableview/ITableView.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.zjy.xtableview;

import com.zjy.xtableview.adapter.XTableAdapter;
import com.zjy.xtableview.model.TableRowModel;

import java.util.List;

/**
* Date: 2020/7/21
Expand All @@ -12,25 +9,11 @@
*/
public interface ITableView {

/**
* 绑定表格数据
* @param headerModel 表头数据
* @param dataList 表行数据
*/
<T, H> void bindData(String title, List<T> headerModel, List<H> dataList);

/**
* 更新某一行数据
* @param position 行下标
* @param data 更新的内容
*/
<T extends TableRowModel<?,?>> void notifyItemData(int position, T data);

/**
* 设置数据填充适配器
* @param adapter 适配器 {@link XTableAdapter}.
*/
void setTableAdapter(XTableAdapter<?,?> adapter);
void setTableAdapter(XTableAdapter<?, ?> adapter);

/**
* 设置是否可长按拖动
Expand Down
26 changes: 21 additions & 5 deletions xtableview/src/main/java/com/zjy/xtableview/XTableView.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ public class XTableView extends LinearLayout implements ITableView {

private ScrollHelper mScrollHelper;

private XTableAdapter.TableDataObserver mDataObserver = new XTableAdapter.TableDataObserver() {
@Override
public void onDataChange() {
if (mTableAdapter == null) {
return;
}
bindData(mTableAdapter.getHeader(), mTableAdapter.getColumnHeaderData(), mTableAdapter.getTableData());
}

@Override
public void onItemChange(int position, TableRowModel<?, ?> data) {
notifyItemData(position, data);
}
};

public XTableView(@NonNull Context context) {
this(context, null);
}
Expand Down Expand Up @@ -105,9 +120,8 @@ private void initialize(Context context) {
vTableRv.attachScrollHelper(mScrollHelper);
}

@Override
public <T, H> void bindData(String title, List<T> headerModel, List<H> dataList) {
if (dataList == null || dataList.isEmpty()) {
private <T, H> void bindData(String title, List<T> headerModel, List<H> dataList) {
if (dataList == null) {
return;
}
vHeaderView.bindData(title, headerModel);
Expand All @@ -119,8 +133,7 @@ public <T, H> void bindData(String title, List<T> headerModel, List<H> dataList)
mItemAdapter.notifyDataSetChanged();
}

@Override
public <T extends TableRowModel<?, ?>> void notifyItemData(int position, T data) {
private <T extends TableRowModel<?, ?>> void notifyItemData(int position, T data) {
if (data == null || mItemAdapter == null || mItemAdapter.getItemList() == null) {
return;
}
Expand All @@ -131,9 +144,11 @@ public <T, H> void bindData(String title, List<T> headerModel, List<H> dataList)
@Override
public void setTableAdapter(XTableAdapter<?, ?> adapter) {
mTableAdapter = adapter;
mTableAdapter.registerDataSetObserver(mDataObserver);
if (vHeaderView != null) {
vHeaderView.setTableAdapter(adapter);
}
mTableAdapter.notifyDataSetChanged();
}

private <T> void initRv(final List<T> dataList) {
Expand Down Expand Up @@ -204,5 +219,6 @@ public void setSwipeEnable(boolean enable) {
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mScrollHelper.destroy();
mTableAdapter.unRegisterDataSetObserver();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.zjy.xtableview.model.TableRowModel;

import java.util.List;

/**
* Date: 2020/8/4
* Author: Yang
Expand All @@ -13,6 +15,22 @@
public abstract class XTableAdapter<T, H extends TableRowModel<?, ?>> {

private Context mContext;
/**
* 左上角的表头数据
*/
private String mHeader;
/**
* 列头部数据集合
*/
private List<T> mColumnHeader;
/**
* 每一行的数据集合
*/
private List<H> mTableData;
/**
* 数据监听
*/
private TableDataObserver mObserver;

public XTableAdapter(Context context) {
mContext = context;
Expand All @@ -22,6 +40,41 @@ public Context getContext() {
return mContext;
}

/**
* 更新表格数据
* @param header 左上角的表头数据
* @param columnHeader 列头部数据集合
* @param tableData 每一行的数据集合
*/
public void bindData(String header, List<T> columnHeader, List<H> tableData) {
mHeader = header;
mColumnHeader = columnHeader;
mTableData = tableData;
}

/**
* 更新某一行数据
* @param position 行下标
* @param data 更新的内容
*/
public void notifyItemData(int position, H data) {
if (mObserver != null) {
mObserver.onItemChange(position, data);
}
}

public String getHeader() {
return mHeader;
}

public List<T> getColumnHeaderData() {
return mColumnHeader;
}

public List<H> getTableData() {
return mTableData;
}

/**
* 创建列头部视图
* @param position 列下标
Expand All @@ -36,10 +89,6 @@ public Context getContext() {
*/
public abstract View onCreateTableItem(int position);

// public void onBindData(int position, View view, H rowData) {
// onBindTableItem(position, view, rowData.content.get(position));
// }

/**
* 绑定每一格的视图数据
* @param position 行下标
Expand Down Expand Up @@ -69,4 +118,32 @@ protected int getColor(int resId) {
return getContext().getResources().getColor(resId);
}

public void notifyDataSetChanged() {
if (mObserver != null) {
mObserver.onDataChange();
}
}

public void registerDataSetObserver(TableDataObserver observer) {
mObserver = observer;
}

public void unRegisterDataSetObserver() {
mObserver = null;
}

public interface TableDataObserver {
/**
* 数据源发生变更
*/
void onDataChange();

/**
* 更新某一行数据
* @param position 行下标
* @param data 覆盖的数据
*/
void onItemChange(int position, TableRowModel<?, ?> data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class TableItemView extends ConstraintLayout {

private View vRootView;
private ConstraintLayout vTitleLayout;
private LinearLayout vTitleLayout;
private ScrollLinearLayout vDataLl;
private ScrollHelper mScrollHelper;
private ObjectAnimator mAnimator;
Expand Down Expand Up @@ -67,7 +67,7 @@ public void attachScrollHelper(ScrollHelper helper) {
}

public void setMinHeight(int height) {
vTitleLayout.setMinHeight(height);
vTitleLayout.setMinimumHeight(height);
}

public void setAdapter(XTableAdapter adapter) {
Expand All @@ -79,7 +79,10 @@ public void setAdapter(XTableAdapter adapter) {

public <T extends TableRowModel> void bindData(int position, T rowData) {
if (vTitleLayout.getChildCount() == 0) {
vTitleLayout.addView(mAdapter.onCreateRowHeader(position));
View headerView = mAdapter.onCreateRowHeader(position);
vTitleLayout.addView(headerView);
headerView.getLayoutParams().width = mCellWidth;
headerView.getLayoutParams().height = MATCH_PARENT;
}
mAdapter.onBindRowHeader(position, vTitleLayout, rowData);
List<?> dataList = rowData.rowData;
Expand Down
4 changes: 3 additions & 1 deletion xtableview/src/main/res/layout/table_item_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
android:background="@android:color/white"
android:orientation="horizontal">

<androidx.constraintlayout.widget.ConstraintLayout
<LinearLayout
android:id="@+id/title_layout"
android:layout_width="@dimen/table_view_item_width"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand Down

0 comments on commit 84ddd1e

Please sign in to comment.