<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Android自定義View實現標籤流效果的具體程式碼,供大家參考,具體內容如下
Android自定義View實現標籤流效果,一行放不下時會自動換行,使用者可以自己定義單個標籤的樣式,可以選中和取消,可以監聽單個標籤的點選事件,功能還算強大,可以滿足大部分開發需求,值得推薦,效果圖如下:
1.自定義View
定義屬性檔案
<declare-styleable name="FlowTagView"> <attr name="lineSpacing" format="dimension" /> <attr name="tagSpacing" format="dimension" /> <!-- 是否是固定佈局 --> <attr name="isFixed" format="boolean" /> <attr name="columnSize" format="integer" /> </declare-styleable>
FlowTagConfig.java
package com.czhappy.effectdemo.flowtag; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import com.czhappy.effectdemo.R; /** * Description: * User: chenzheng * Date: 2017/2/17 0017 * Time: 10:23 */ public class FlowTagConfig { private static final int DEFAULT_LINE_SPACING = 5;//預設行間距 private static final int DEFAULT_TAG_SPACING = 10;//各個標籤之間的預設距離 private static final int DEFAULT_FIXED_COLUMN_SIZE = 3; //預設列數 private int lineSpacing; private int tagSpacing; private int columnSize; private boolean isFixed; public FlowTagConfig(Context context,AttributeSet attrs){ TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FlowTagView); try { lineSpacing = a.getDimensionPixelSize(R.styleable.FlowTagView_lineSpacing, DEFAULT_LINE_SPACING); tagSpacing = a.getDimensionPixelSize(R.styleable.FlowTagView_tagSpacing, DEFAULT_TAG_SPACING); columnSize = a.getInteger(R.styleable.FlowTagView_columnSize, DEFAULT_FIXED_COLUMN_SIZE); isFixed = a.getBoolean(R.styleable.FlowTagView_isFixed,false); } finally { a.recycle(); } } public int getLineSpacing() { return lineSpacing; } public void setLineSpacing(int lineSpacing) { this.lineSpacing = lineSpacing; } public int getTagSpacing() { return tagSpacing; } public void setTagSpacing(int tagSpacing) { this.tagSpacing = tagSpacing; } public int getColumnSize() { return columnSize; } public void setColumnSize(int columnSize) { this.columnSize = columnSize; } public boolean isFixed() { return isFixed; } public void setIsFixed(boolean isFixed) { this.isFixed = isFixed; } }
FlowTagView.java
package com.czhappy.effectdemo.flowtag; import android.content.Context; import android.database.DataSetObserver; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; /** * Description: * User: chenzheng * Date: 2017/2/17 0017 * Time: 10:23 */ public class FlowTagView extends ViewGroup { private int mLineSpacing;//行間距 private int mTagSpacing;//各個標籤之間的距離 private BaseAdapter mAdapter; private TagItemClickListener mListener; private DataChangeObserver mObserver; public FlowTagView(Context context) { super(context); init(context, null, 0); } public FlowTagView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs, 0); } public FlowTagView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context, attrs, defStyle); } private void init(Context context, AttributeSet attrs, int defStyle) { //獲取屬性 FlowTagConfig config = new FlowTagConfig(context, attrs); mLineSpacing = config.getLineSpacing(); mTagSpacing = config.getTagSpacing(); } private void drawLayout() { if (mAdapter == null || mAdapter.getCount() == 0) { return; } this.removeAllViews(); for (int i = 0; i < mAdapter.getCount(); i++) { View view = mAdapter.getView(i,null,null); final int position = i; view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mListener != null) { mListener.itemClick(position); } } }); this.addView(view); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int wantHeight = 0; int wantWidth = resolveSize(0, widthMeasureSpec); int paddingLeft = getPaddingLeft(); int paddingRight = getPaddingRight(); int paddingTop = getPaddingTop(); int paddingBottom = getPaddingBottom(); int childLeft = paddingLeft; int childTop = paddingTop; int lineHeight = 0; //固定列的數量所需要的程式碼 for (int i = 0; i < getChildCount(); i++) { final View childView = getChildAt(i); LayoutParams params = childView.getLayoutParams(); childView.measure( getChildMeasureSpec(widthMeasureSpec, paddingLeft + paddingRight, params.width), getChildMeasureSpec(heightMeasureSpec, paddingTop + paddingBottom, params.height) ); //獲取單個tag的寬高 int childHeight = childView.getMeasuredHeight(); int childWidth = childView.getMeasuredWidth(); lineHeight = Math.max(childHeight, lineHeight); //超過長度的新起一行 if (childLeft + childWidth + paddingRight > wantWidth) { childLeft = paddingLeft; childTop += mLineSpacing + childHeight; lineHeight = childHeight; } childLeft += childWidth + mTagSpacing; } wantHeight += childTop + lineHeight + paddingBottom; setMeasuredDimension(wantWidth, resolveSize(wantHeight, heightMeasureSpec)); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { //固定列的數量所需要的程式碼 int width = r - l; int paddingLeft = getPaddingLeft(); int paddingTop = getPaddingTop(); int paddingRight = getPaddingRight(); int childLeft = paddingLeft; int childTop = paddingTop; int lineHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View childView = getChildAt(i); if (childView.getVisibility() == View.GONE) { continue; } int childWidth = childView.getMeasuredWidth(); int childHeight = childView.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childLeft + childWidth + paddingRight > width) { childLeft = paddingLeft; childTop += mLineSpacing + lineHeight; lineHeight = childHeight; } childView.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mTagSpacing; } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new LayoutParams(this.getContext(), attrs); } public void setAdapter(BaseAdapter adapter){ if (mAdapter == null){ mAdapter = adapter; if (mObserver == null){ mObserver = new DataChangeObserver(); mAdapter.registerDataSetObserver(mObserver); } drawLayout(); } } public void setItemClickListener(TagItemClickListener mListener) { this.mListener = mListener; } /** * 單擊監聽介面 */ public interface TagItemClickListener { void itemClick(int position); } class DataChangeObserver extends DataSetObserver { @Override public void onChanged() { FlowTagView.this.drawLayout(); } @Override public void onInvalidated() { super.onInvalidated(); } } }
2.測試類
FlowTagActivity.java
package com.czhappy.effectdemo.activity; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import com.czhappy.effectdemo.R; import com.czhappy.effectdemo.adapter.EvaluateAdapter; import com.czhappy.effectdemo.flowtag.FlowTagView; import com.czhappy.effectdemo.model.Evaluate; import java.util.ArrayList; import java.util.List; /** * Description: * User: chenzheng * Date: 2017/2/17 0017 * Time: 11:47 */ public class FlowTagActivity extends AppCompatActivity { private FlowTagView mContainer; private EvaluateAdapter adapter; private List<Evaluate> chooseList = new ArrayList<Evaluate>(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_flowtag); initView(); initData(); } private void initData() { List<Evaluate> list = new ArrayList(); Evaluate e1 = new Evaluate("熱情", "1"); Evaluate e2 = new Evaluate("服務周到", "2"); Evaluate e3 = new Evaluate("一般", "3"); Evaluate e4 = new Evaluate("技術活槓槓的", "4"); Evaluate e5 = new Evaluate("專業精通", "5"); Evaluate e6 = new Evaluate("只會吹牛逼", "6"); Evaluate e7 = new Evaluate("地下第一僅此一家", "7"); list.add(e1); list.add(e2); list.add(e3); list.add(e4); list.add(e5); list.add(e6); list.add(e7); adapter.setItems(list); } private void initView() { mContainer = (FlowTagView) this.findViewById(R.id.container); adapter = new EvaluateAdapter(this); mContainer.setAdapter(adapter); mContainer.setItemClickListener(new FlowTagView.TagItemClickListener() { @Override public void itemClick(int position) { Evaluate e = (Evaluate) adapter.getItem(position); e.is_choosed = !e.is_choosed; if(e.is_choosed){ chooseList.add(e); }else{ chooseList.remove(e); } adapter.notifyDataSetChanged(); } }); } }
EvaluateAdapter.java
package com.czhappy.effectdemo.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.czhappy.effectdemo.R; import com.czhappy.effectdemo.model.Evaluate; import java.util.ArrayList; import java.util.List; /** * Description: * User: chenzheng * Date: 2017/2/17 0017 * Time: 11:43 */ public class EvaluateAdapter extends BaseAdapter { private Context context; private LayoutInflater mInflater; private List<Evaluate> list; public EvaluateAdapter(Context context) { this.context = context; this.mInflater = LayoutInflater.from(context); this.list = new ArrayList<Evaluate>(); } public List<Evaluate> getList(){ return list; } public void setItems(List<Evaluate> list){ this.list = list; notifyDataSetChanged(); } @Override public int getCount() { return list == null ? 0 : list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate( R.layout.evaluate_grid_item, null); holder.evaluate_tv = (TextView)convertView.findViewById(R.id.evaluate_tv); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } final Evaluate ee = (Evaluate) getItem(position); holder.evaluate_tv.setText(ee.getName()); if(ee.is_choosed){ holder.evaluate_tv.setBackgroundResource(R.drawable.bg_round_corner_line_orange); }else{ holder.evaluate_tv.setBackgroundResource(R.drawable.bg_round_corner_line_gray); } return convertView; } private final class ViewHolder { private TextView evaluate_tv; } }
佈局檔案
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.czhappy.effectdemo.flowtag.FlowTagView android:id="@+id/container" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" app:tagSpacing="10dp" app:lineSpacing="10dp"/> </LinearLayout>
bg_round_corner_line_orange.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#ffffff" /> <corners android:radius="5dp" /> <stroke android:width="0.5dp" android:color="#FF6700"/> </shape>
bg_round_corner_line_gray.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#ffffff" /> <corners android:radius="5dp" /> <stroke android:width="0.5dp" android:color="#cccccc"/> </shape>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45