您的位置:首页 > 其它

流式布局(实现历史记录功能)

2016-08-04 20:12 330 查看
package com.example.k.flowlayout_test;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
* Created by k on 2016/8/4.
*/
public class DynamicTagFlowLayout extends ViewGroup {
TextView[] button_attay = new TextView[100];
private List<String> mTags = new ArrayList<String>();

public DynamicTagFlowLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public DynamicTagFlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public DynamicTagFlowLayout(Context context) {
super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);

//当前ViewGroup的总高度
int totalHeight= 0;
//所有行中的最大宽度
int maxLineWidth = 0;

//当前行的最大高度
int lineMaxHeight = 0;
//当前行的总宽度
int currentLineWidth = 0;

//每个childView所占用的宽度
int childViewWidthSpace = 0;
//每个childView所占用的高度
int childViewHeightSpace = 0;

int count = getChildCount();
MarginLayoutParams layoutParams;

for(int i = 0; i < count; i++){
View child = getChildAt(i);

if(child.getVisibility() != View.GONE){//只有当这个View能够显示的时候才去测量
//测量每个子View,以获取子View的宽和高
measureChild(child, widthMeasureSpec, heightMeasureSpec);

layoutParams = (MarginLayoutParams) child.getLayoutParams();

childViewWidthSpace = child.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
childViewHeightSpace = child.getMeasuredHeight() + layoutParams.topMargin + layoutParams.bottomMargin;

if(currentLineWidth + childViewWidthSpace > widthSize){//表示如果当前行再加上现在这个子View,就会超出总的规定宽度,需要另起一行
totalHeight += lineMaxHeight;
if(maxLineWidth < currentLineWidth){//如果行的最长宽度发生了变化,更新保存的最长宽度
maxLineWidth = currentLineWidth;
}
currentLineWidth = childViewWidthSpace;//另起一行后,需要重置当前行宽
lineMaxHeight = childViewHeightSpace;
}else{//表示当前行可以继续添加子元素
currentLineWidth += childViewWidthSpace;
if(lineMaxHeight < childViewHeightSpace){
lineMaxHeight = childViewHeightSpace;
}
}
}
}

setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : maxLineWidth, heightMode == MeasureSpec.EXACTLY ? heightSize : totalHeight);

}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//当前是第几行
int currentLine = 1;
//存放每一行的最大高度
List<Integer> lineMaxHeightList = new ArrayList<Integer>();

//每个childView所占用的宽度
int childViewWidthSpace = 0;
//每个childView所占用的高度
int childViewHeightSpace = 0;

//当前行的最大高度
int lineMaxHeight = 0;
//当前行的总宽度
int currentLineWidth = 0;

int count = getChildCount();
MarginLayoutParams layoutParams;

for(int i = 0; i < count; i++){
int cl= 0, ct = 0, cr = 0, cb = 0;
View child = getChildAt(i);
if(child.getVisibility() != View.GONE){//只有当这个View能够显示的时候才去测量

layoutParams = (MarginLayoutParams) child.getLayoutParams();
childViewWidthSpace = child.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
childViewHeightSpace = child.getMeasuredHeight() + layoutParams.topMargin + layoutParams.bottomMargin;

System.out.println("getWidth()---->"+getWidth());

if(currentLineWidth + childViewWidthSpace > getWidth()){//表示如果当前行再加上现在这个子View,就会超出总的规定宽度,需要另起一行
lineMaxHeightList.add(lineMaxHeight);//此时先将这一行的最大高度加入到集合中
//新的一行,重置一些参数
currentLine++;
currentLineWidth = childViewWidthSpace;
lineMaxHeight = childViewHeightSpace;

cl = layoutParams.leftMargin;
if(currentLine > 1){
for(int j = 0; j < currentLine - 1; j++){
ct += lineMaxHeightList.get(j);
}
ct += layoutParams.topMargin ;
}else{
ct = layoutParams.topMargin;
}
}else{//表示当前行可以继续添加子元素
cl = currentLineWidth + layoutParams.leftMargin;
if(currentLine > 1){
for(int j = 0; j < currentLine - 1; j++){
ct += lineMaxHeightList.get(j);
}
ct += layoutParams.topMargin;
}else{
ct = layoutParams.topMargin;
}
currentLineWidth += childViewWidthSpace;
if(lineMaxHeight < childViewHeightSpace){
lineMaxHeight = childViewHeightSpace;
}
}

cr = cl + child.getMeasuredWidth();
cb = ct + child.getMeasuredHeight();

child.layout(cl, ct, cr, cb);

}
}
}

@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}

public void setTags(List<String> tags){
if(tags!= null){
mTags.clear();
mTags.addAll(tags);
//Revival修改处,实现从头插入数据
int i = mTags.size()-1;
for(;0<=i; i--){
TextView tv = new TextView(getContext());
button_attay[i] = tv;
MarginLayoutParams lp = new MarginLayoutParams(MarginLayoutParams.WRAP_CONTENT, MarginLayoutParams.WRAP_CONTENT);
lp.setMargins(10, 10, 10, 10);
//  lp.width = MarginLayoutParams.WRAP_CONTENT;
//  lp.height = MarginLayoutParams.WRAP_CONTENT;
tv.setLayoutParams(lp);
tv.setTextColor(Color.BLUE);
//tv.setBackgroundResource("");
/*
* setPadding一定要在setBackgroundResource后面使用才有效!!!
* http://stackoverflow.com/questions/18327498/setting-padding-for-textview-not-working */
tv.setPadding(15, 15, 15, 15);
//tv.setTextColor(Color.WHITE);

tv.setText(mTags.get(i));

tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null){
listener.onClick(v);
}
}
});

addView(tv);
}
requestLayout();
}
}

private OnTagItemClickListener listener;
public interface OnTagItemClickListener{
public void onClick(View v);
}
public void setOnTagItemClickListener(OnTagItemClickListener l){
listener = l;
}

}

用法:活动
package com.example.k.flowlayout_test;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private DynamicTagFlowLayout dynamicTagFlowLayout;
Button button, empty_button;
EditText input_edit_text;

List<String> tags = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getData();
initView();
}

private void getData() {
String str;
SharedPreferences input = getSharedPreferences("data",MODE_PRIVATE);
int a = input.getInt("value",0);
for(int b = 1;b<=a;b++){
str = input.getString(""+b,"null");
tags.add(str);
}
}

private void initView() {
input_edit_text = (EditText) findViewById(R.id.Edit_Text);
button = (Button) findViewById(R.id.button);
empty_button = (Button) findViewById(R.id.remove_button);
empty_button.setOnClickListener(this);
button.setOnClickListener(this);
dynamicTagFlowLayout = (DynamicTagFlowLayout) findViewById(R.id.dynamic_tag);
dynamicTagFlowLayout.setTags(tags);
dynamicTagFlowLayout.setOnTagItemClickListener(new DynamicTagFlowLayout.OnTagItemClickListener() {
@Override
public void onClick(View v) {
Button b = (Button)v;
Toast.makeText(MainActivity.this, ""+b.getText(), Toast.LENGTH_SHORT).show();
}
});
}

private void initData() {

}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
int a;
SharedPreferences.Editor output_editor = getSharedPreferences("data",MODE_PRIVATE).edit();
SharedPreferences input = getSharedPreferences("data",MODE_PRIVATE);
String str = input_edit_text.getText().toString();
if(!str.equals("")){
a = input.getInt("value",0);
a++;
output_editor.putString(""+a,str);
output_editor.putInt("value",a);
output_editor.commit();
}
break;
case R.id.remove_button:
SharedPreferences.Editor clear_output_editor = getSharedPreferences("data",MODE_PRIVATE).edit();
clear_output_editor.clear();
clear_output_editor.commit();
for(int g = 0;g<tags.size();g++) {
dynamicTagFlowLayout.removeView(dynamicTagFlowLayout.button_attay[g]);
}
tags.clear();
break;
default:
break;
}
}
}
布局:
<com.example.k.flowlayout_test.DynamicTagFlowLayout
android:layout_below="@+id/Edit_Text"
android:id="@+id/dynamic_tag"
android:layout_width="match_parent"
android:layout_height="match_parent"
></com.example.k.flowlayout_test.DynamicTagFlowLayout>



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: