public class RouteInfoGroup extends LinearLayout {
private ArrayList<Integer> tabChildsId = new ArrayList<Integer>();
/**
* 记录当前id,默认id为-1
*/
private int mCheckedId = -1;
private int defaultid;
private CheckedStateTracker mChildOnCheckedChangeListener;
private PassThroughHierarchyChangeListener mPassThroughListener;
private OnItemTabClickListener listener;
public RouteInfoGroup(Context context) {
super(context);
}
public RouteInfoGroup(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public RouteInfoGroup(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void init() {
// radiobutton的监听
mChildOnCheckedChangeListener = new CheckedStateTracker();
// 层次结构的监听
mPassThroughListener = new PassThroughHierarchyChangeListener();
// 设置监听
super.setOnHierarchyChangeListener(mPassThroughListener);
}
/**
* 设置tab数据
*
* @param datas
*/
public void setDataOfTabs(List<AMapNaviPath> datas) {
L.i("TAG2","getChildCount()===>>>"+getChildCount());
L.i("TAG2","datas()===>>>"+datas.size());
int count = 0;
if (datas.size() >= getChildCount()) {
count = getChildCount();
// return;
}else{
count = datas.size();
}
for (int i = 0; i < count; i++) {
L.i("TAG2","data=1111==>>>");
RouteInfoLayout view = (RouteInfoLayout) findViewById(tabChildsId.get(i));
AMapNaviPath data = datas.get(i);
L.i("TAG2","data===>>>"+data);
if (i == 0) {
check(view.getId());
defaultid = view.getId();
}
view.setRouteInfoDistance(data.getAllLength());
view.setRouteInfoTime(data.getAllTime());
view.setRouteInfoTitle(data.getLabels());
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
L.i("TAG2", "onFinishInflate=111==>>>>");
check(mCheckedId);
}
/**
* 改变item的选择状态
*
* @param viewId
* @param checked
* true 为选择,false 为不选择
*/
private void setCheckedStateForView(int viewId, boolean checked) {
L.i("TAG2", "setCheckedStateForView====>>>");
View checkedView = findViewById(viewId);
L.i("TAG2", "setCheckedStateForView===checkedView=>>>" + checkedView);
if (checkedView != null && checkedView instanceof RouteInfoLayout) {
((RouteInfoLayout) checkedView).setChecked(checked);
L.i("TAG2", "setCheckedStateForView===checkedView=111>>>"
+ checkedView);
}
}
public void check(int id) {
// don't even bother
L.i("TAG2", "check=000======>>>" + id);
L.i("TAG2", "check=000=11=====>>>" + mCheckedId);
if (id != -1 && (id == mCheckedId)) {
L.i("TAG2", "check=111======>>>" + id);
return;
}
if (mCheckedId != -1) {
L.i("TAG2", "check=222======>>>" + id);
setCheckedStateForView(mCheckedId, false);
}
if (id != -1) {
L.i("TAG2", "check=333======>>>" + id);
setCheckedStateForView(id, true);
}
setCheckedId(id);
}
/**
* 选择Item后要执行的操作
*
* @param id
*/
private void setCheckedId(int id) {
// 更新当前选择的Item的id
mCheckedId = id;
if (listener != null) {
RouteInfoLayout view = (RouteInfoLayout) findViewById(id);
listener.onItemClick(view, defaultid);
}
}
/**
* 设置点击item的监听
*
* @param l
*/
public void setOnItemTabClickListener(OnItemTabClickListener l) {
this.listener = l;
}
/**
* item选择监听 Title: RechargeValueGroup.java Description:
*
* @author Liusong
* @date 2015-6-26
* @version V1.0
*/
public interface OnItemTabClickListener {
public void onItemClick(RouteInfoLayout view, int defaultid);
}
/**
* 图层变换监听 Title: RechargeValueGroup.java Description:
*
* @author Liusong
* @date 2015-6-26
* @version V1.0
*/
private class PassThroughHierarchyChangeListener implements
ViewGroup.OnHierarchyChangeListener {
private ViewGroup.OnHierarchyChangeListener mOnHierarchyChangeListener;
/**
* {@inheritDoc}
*/
public void onChildViewAdded(View parent, View child) {
if (parent == RouteInfoGroup.this
&& child instanceof RouteInfoLayout) {
int id = child.getId();
// Log.i("TAG2", "onChildViewAdded=111==>>>>" + id);
// generates an id if it's missing
if (id == View.NO_ID) {
id = View.generateViewId();
child.setId(id);
}
tabChildsId.add(id);
((RouteInfoLayout) child)
.setOnClickListener(mChildOnCheckedChangeListener);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
/**
* {@inheritDoc}
*/
public void onChildViewRemoved(View parent, View child) {
if (parent == RouteInfoGroup.this
&& child instanceof RouteInfoLayout) {
((RouteInfoLayout) child).setOnClickListener(null);
tabChildsId.remove(0);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewRemoved(parent, child);
}
}
}
/**
* 每一项Item的点击事件 Title: RechargeValueGroup.java Description:
*
* @author Liusong
* @date 2015-6-26
* @version V1.0
*/
class CheckedStateTracker implements OnClickListener {
@Override
public void onClick(View v) {
check(v.getId());
}
};
}