Skip to content

Commit

Permalink
支持动态设置行列数
Browse files Browse the repository at this point in the history
  • Loading branch information
“xiaohaibin committed Mar 18, 2019
1 parent 8f441f3 commit 57ebb9b
Showing 1 changed file with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
Expand Down Expand Up @@ -57,29 +58,14 @@ private void initView(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PageMenuLayout);
if (typedArray != null) {
mRowCount = typedArray.getInteger(R.styleable.PageMenuLayout_pagemenu_row_count, DEFAULT_ROW_COUNT);
mSpanCount = typedArray.getInteger(R.styleable.PageMenuLayout_pagemenu_span_count, DEFAULT_SPAN_COUNT);
mSpanCount = typedArray.getInteger(R.styleable.PageMenuLayout_pagemenu_span_count, DEFAULT_SPAN_COUNT);
typedArray.recycle();
}

}

public void setPageDatas(List<T> datas, PageMenuViewHolderCreator creator) {
if (datas == null) {
datas = new ArrayList<>();
}
int pageSize = mRowCount * mSpanCount;
int pageCount = (int) Math.ceil(datas.size() * 1.0 / pageSize);
List<View> viewList = new ArrayList<>();
for (int index = 0; index < pageCount; index++) {
RecyclerView recyclerView = new RecyclerView(this.getContext());
recyclerView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
recyclerView.setLayoutManager(new GridLayoutManager(this.getContext(), mSpanCount));
EntranceAdapter<T> entranceAdapter = new EntranceAdapter<>(creator, datas, index, pageSize);
recyclerView.setAdapter(entranceAdapter);
viewList.add(recyclerView);
}
PageViewPagerAdapter adapter = new PageViewPagerAdapter(viewList);
mViewPager.setAdapter(adapter);
public void setPageDatas(@NonNull List<T> datas, @NonNull PageMenuViewHolderCreator creator) {
setPageDatas(mRowCount, mSpanCount, datas, creator);
}

/**
Expand All @@ -102,4 +88,46 @@ public void setOnPageListener(ViewPager.OnPageChangeListener pageListener) {
mViewPager.addOnPageChangeListener(pageListener);
}
}

/**
* 设置行数
* @param rowCount
*/
public void setRowCount(int rowCount) {
mRowCount = rowCount;
}

/**
* 设置列数
* @param spanCount
*/
public void setSpanCount(int spanCount) {
mSpanCount = spanCount;
}

public void setPageDatas(int rowCount, int spanCount, @NonNull List<T> datas, @NonNull PageMenuViewHolderCreator creator) {
if (datas == null) {
datas = new ArrayList<>();
}
mRowCount = rowCount;
mSpanCount = spanCount;
if (mRowCount == 0 || mSpanCount == 0) {
return;
}
int pageSize = mRowCount * mSpanCount;
int pageCount = (int) Math.ceil(datas.size() * 1.0 / pageSize);
List<View> viewList = new ArrayList<>();
for (int index = 0; index < pageCount; index++) {
RecyclerView recyclerView = new RecyclerView(this.getContext());
recyclerView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
recyclerView.setLayoutManager(new GridLayoutManager(this.getContext(), mSpanCount));
EntranceAdapter<T> entranceAdapter = new EntranceAdapter<>(creator, datas, index, pageSize);
recyclerView.setAdapter(entranceAdapter);
viewList.add(recyclerView);
}
PageViewPagerAdapter adapter = new PageViewPagerAdapter(viewList);
mViewPager.setAdapter(adapter);
}


}

0 comments on commit 57ebb9b

Please sign in to comment.