2013年8月3日星期六

android custom rounded corners TabView List

 

see many application settings interface has rounded corners for a list similar to the following

 

 Here to talk about my

Principle : Inheritance LinearLayout, then set a custom TabAdapter, similar to the listview, add a setAdapter () method , this method is to add subviews , and then set the background selector effect ; also Add style different views , through addview method , and finally we must commit method is called to set the child view background , the background there are three cases , the intermediate term corners are rounded , rounded top and top rounded corners

 

custom TabAdapter abstract class is to inherit to :

 
  
package com.allen.tabview; 

import android.view.View;

/**
* @package:com.allen.tabview
*
@author:Allen
* @email:jaylong1302@163.com
* @data:2013-7-26 下午2:49:51
* @description:适配器
*/
public abstract class TabAdapter {
public abstract int getCount();
public abstract Object getItem(int position);
public abstract View getView(int position);
}
 
 

Next is a custom TabView

 
        
   
package com.allen.tabview; 

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
* @package:com.allen.tabview
*
@author:Allen
* @email:jaylong1302@163.com
* @data:2013-7-26 下午2:48:36
* @description:圆角表格
*/
public class TabView extends LinearLayout {

TabAdapter adapter;
/** 子视图数量 */
int size = 0;

public TabView(Context context) {
super(context);
init();
}

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

void init() {
this.setOrientation(LinearLayout.VERTICAL);
this.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
this.setBackgroundResource(R.drawable.background_view_rounded_container);
}

/** 设置适配器 */
public void setAdapter(TabAdapter adapter) {
this.adapter = adapter;
// 遍历当前的adapter
if (adapter != null) {
size
= adapter.getCount();
for (int i = 0; i < size; i++) {
View child
= adapter.getView(i);
this.addView(child);
}
commit();
}

}

@Override
public void addView(View child) {
// TODO Auto-generated method stub
super.addView(child);
child.setClickable(
true);
}

/** 调用addView之后执行的方法 */
public void commit() {
int len = this.getChildCount();
if (len > 1) {// 多项内容
for (int i = 0; i < len; i++) {
View child
= this.getChildAt(i);
if (i == 0) {// 顶部
child.setBackgroundResource(R.drawable.background_view_rounded_top);
}
else if (i > 0 && i < len - 1) {// 中间
child.setBackgroundResource(R.drawable.background_view_rounded_middle);
}
else if (i == len - 1) {// 底部
child.setBackgroundResource(R.drawable.background_view_rounded_bottom);
}
}
}
else if (len == 1) {// 一项内容
View child = this.getChildAt(0);
child.setBackgroundResource(R.drawable.background_view_rounded_single);
}
}

public interface TabItemClickListener {
void onClick(int position, View v);
}

TabItemClickListener itemClick;

public void setOnItemClickListener(final TabItemClickListener itemClick) {
this.itemClick = itemClick;
// 绑定监听事件
for (int i = 0; i < size; i++) {
final int index = i;
View childView
= this.getChildAt(i);
childView.setOnClickListener(
new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (itemClick != null) {
itemClick.onClick(index, v);
}
}
});
}
}
}
  
   View Code  
 

Finally there is, for the entire outermost rounded background is to use inset, that automatically adds a divider effect

 

entire project on github above , need can clone and fok. . .

没有评论:

发表评论