Skip to content

Commit daed1d6

Browse files
committed
设置数据适配器
1 parent 9deaafc commit daed1d6

File tree

3 files changed

+80
-9
lines changed

3 files changed

+80
-9
lines changed

app/src/main/java/com/example/lvruheng/autoflowlayout/AutoFlowLayout.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.content.res.TypedArray;
55
import android.graphics.drawable.Drawable;
66
import android.util.AttributeSet;
7-
import android.util.Log;
87
import android.view.View;
98
import android.widget.LinearLayout;
109
import java.util.ArrayList;
@@ -13,7 +12,7 @@
1312
/**
1413
*自定义LinearLayout,支持自动换行,指定行数,实现流式布局
1514
*/
16-
public class AutoFlowLayout extends LinearLayout {
15+
public class AutoFlowLayout <T> extends LinearLayout {
1716
/**
1817
* 存储所有的View,按行记录
1918
*/
@@ -67,6 +66,11 @@ public class AutoFlowLayout extends LinearLayout {
6766
* 记录展示的数量
6867
*/
6968
private int mDisplayNumbers;
69+
/**
70+
* 数据适配器
71+
*/
72+
private FlowAdapter<T> mAdapter;
73+
7074
public AutoFlowLayout(Context context) {
7175
super(context);
7276
}
@@ -91,6 +95,16 @@ private void init(Context context, AttributeSet attrs) {
9195
@Override
9296
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
9397
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
98+
setFlowMesure(widthMeasureSpec,heightMeasureSpec);
99+
100+
}
101+
102+
/**
103+
* 流式布局的测量模式
104+
* @param widthMeasureSpec
105+
* @param heightMeasureSpec
106+
*/
107+
private void setFlowMesure(int widthMeasureSpec, int heightMeasureSpec) {
94108
// 获得它的父容器为它设置的测量模式和大小
95109
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
96110
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
@@ -164,6 +178,13 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
164178

165179
@Override
166180
protected void onLayout(boolean changed, int l, int t, int r, int b) {
181+
setFlowLayout();
182+
}
183+
184+
/**
185+
* 流式布局的布局模式
186+
*/
187+
private void setFlowLayout() {
167188
mAllViews.clear();
168189
mLineHeight.clear();
169190

@@ -437,6 +458,22 @@ public List<View> getCheckedViews() {
437458
public View getSelectedView() {
438459
return mSelectedView;
439460
}
461+
462+
/**
463+
* 设置数据适配器
464+
* @param adapter
465+
*/
466+
public void setAdapter(FlowAdapter<T> adapter) {
467+
mAdapter = adapter;
468+
if (mAdapter.getCount() != 0) {
469+
for (int i = 0; i < mAdapter.getCount(); i ++) {
470+
View view = mAdapter.getView(i);
471+
addView(view);
472+
}
473+
requestLayout();
474+
}
475+
}
476+
440477
public interface OnItemClickListener{
441478
void onItemClick(int position,View view);
442479
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.lvruheng.autoflowlayout;
2+
3+
import android.view.View;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
/**
10+
* Created by lvruheng on 2017/8/1.
11+
*/
12+
13+
public abstract class FlowAdapter<T> {
14+
private List<T> mList;
15+
16+
public FlowAdapter(List<T> datas) {
17+
mList = datas;
18+
}
19+
public FlowAdapter(T[] datas) {
20+
mList = new ArrayList<T>(Arrays.asList(datas));
21+
}
22+
23+
public T getItem(int position) {
24+
return mList.get(position);
25+
}
26+
27+
public int getCount() {
28+
return mList == null ? 0 : mList.size();
29+
}
30+
31+
public abstract View getView(int position);
32+
}

app/src/main/java/com/example/lvruheng/autoflowlayout/MainActivity.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ protected void onCreate(Bundle savedInstanceState) {
1818
setContentView(R.layout.activity_main);
1919
mFlowLayout = (AutoFlowLayout) findViewById(R.id.afl_cotent);
2020
mLayoutInflater = LayoutInflater.from(this);
21-
for(int i = 0; i<mData.length; i++){
22-
View item = mLayoutInflater.inflate(R.layout.sub_item, null);
23-
TextView tvAttrTag = (TextView) item.findViewById(R.id.tv_attr_tag);
24-
tvAttrTag.setText(mData[i]);
25-
final String content = mData[i];
26-
mFlowLayout.addView(item);
27-
}
21+
mFlowLayout.setAdapter(new FlowAdapter(mData) {
22+
@Override
23+
public View getView(int position) {
24+
View item = mLayoutInflater.inflate(R.layout.sub_item, null);
25+
TextView tvAttrTag = (TextView) item.findViewById(R.id.tv_attr_tag);
26+
tvAttrTag.setText(mData[position]);
27+
return item;
28+
}
29+
});
2830
mFlowLayout.setOnItemClickListener(new AutoFlowLayout.OnItemClickListener() {
2931
@Override
3032
public void onItemClick(int position,View view) {

0 commit comments

Comments
 (0)