package com.example.publicapp.pem;
import java.util.ArrayList;
import java.util.List;
import com.example.publicapp.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PemTabHostHead extends LinearLayout{
private Context mContext;
private LayoutInflater inflater;
private View content;
private LinearLayout tabLayout;
private List<PemTab> tabs;
private OnTabChangedListener mOnTabChangedListener;
public PemTabHostHead(Context context) {
super(context);
init(context, null, 0);
}
public PemTabHostHead(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
public PemTabHostHead(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
mContext = context;
inflater = LayoutInflater.from(context);
content = inflater.inflate(R.layout.pem_tab_host_head, this);
tabs = new ArrayList<PemTab>();
tabLayout = (LinearLayout) content.findViewById(R.id.pem_tab_host_head_layout);
}
public void removeAllTabs(){
tabLayout.removeAllViews();
tabs.clear();
}
public void addTab(String title, float weight){
PemTab tab = new PemTab();
tab.setTitle(title);
//TextView newTabView = (TextView) inflater.inflate(R.layout.pem_tab_text_view, null);
TextView newTabView = new TextView(mContext);
initTabView(newTabView, title, weight);
tab.setView(newTabView);
tab.setIndex(tabs.size());
tabs.add(tab);
final int curTabCount = tabs.size();
newTabView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectTab(curTabCount - 1);
if(mOnTabChangedListener != null){
mOnTabChangedListener.onTabChanged(curTabCount - 1);
}
}
});
tabLayout.addView(newTabView);
}
private void initTabView(TextView tabView, String text, float weight) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT);;
params.weight = weight;
tabView.setPadding(5, 5, 5, 5);
tabView.setLayoutParams(params);
tabView.setTextSize(15);
tabView.setSingleLine();
tabView.setGravity(Gravity.CENTER);
tabView.setTextColor(mContext.getResources().getColor(R.color.black));
tabView.setText(text);
tabView.setBackgroundColor(mContext.getResources().getColor(R.color.black));
//tabView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pem_tab_normal));
}
public void selectTab(int tabIndex) {
if(tabIndex < -1 || tabIndex >= tabs.size()){
throw new IndexOutOfBoundsException();
}
for(int i = 0; i < tabs.size(); i++){
if(i == tabIndex){
PemTab selectedTab = tabs.get(i);
TextView tabView = selectedTab.getView();
tabView.setTextColor(android.graphics.Color.parseColor("#1968A6"));
tabView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pem_tab_selected));
//tabView.setBackgroundColor(mContext.getResources().getColor(R.color.pem_gray));
}
else{
PemTab selectedTab = tabs.get(i);
TextView tabView = selectedTab.getView();
tabView.setTextColor(Color.BLACK);
tabView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pem_tab_normal));
//tabView.setBackgroundColor(mContext.getResources().getColor(R.color.whitesmoke));
}
}
//Toast.makeText(mContext, "cur tab " + tabIndex + ", " + selectedTab.getTitle(), Toast.LENGTH_SHORT).show();
}
public void setOnTabChangedListener(OnTabChangedListener onTabChangedListener){
mOnTabChangedListener = onTabChangedListener;
}
public interface OnTabChangedListener{
void onTabChanged(int position);
}
class PemTab{
private String title;
private TextView mView;
private int index;
public PemTab() {
super();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public TextView getView() {
return mView;
}
public void setView(TextView mView) {
this.mView = mView;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
import java.util.ArrayList;
import java.util.List;
import com.example.publicapp.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PemTabHostHead extends LinearLayout{
private Context mContext;
private LayoutInflater inflater;
private View content;
private LinearLayout tabLayout;
private List<PemTab> tabs;
private OnTabChangedListener mOnTabChangedListener;
public PemTabHostHead(Context context) {
super(context);
init(context, null, 0);
}
public PemTabHostHead(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
public PemTabHostHead(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
mContext = context;
inflater = LayoutInflater.from(context);
content = inflater.inflate(R.layout.pem_tab_host_head, this);
tabs = new ArrayList<PemTab>();
tabLayout = (LinearLayout) content.findViewById(R.id.pem_tab_host_head_layout);
}
public void removeAllTabs(){
tabLayout.removeAllViews();
tabs.clear();
}
public void addTab(String title, float weight){
PemTab tab = new PemTab();
tab.setTitle(title);
//TextView newTabView = (TextView) inflater.inflate(R.layout.pem_tab_text_view, null);
TextView newTabView = new TextView(mContext);
initTabView(newTabView, title, weight);
tab.setView(newTabView);
tab.setIndex(tabs.size());
tabs.add(tab);
final int curTabCount = tabs.size();
newTabView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectTab(curTabCount - 1);
if(mOnTabChangedListener != null){
mOnTabChangedListener.onTabChanged(curTabCount - 1);
}
}
});
tabLayout.addView(newTabView);
}
private void initTabView(TextView tabView, String text, float weight) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT);;
params.weight = weight;
tabView.setPadding(5, 5, 5, 5);
tabView.setLayoutParams(params);
tabView.setTextSize(15);
tabView.setSingleLine();
tabView.setGravity(Gravity.CENTER);
tabView.setTextColor(mContext.getResources().getColor(R.color.black));
tabView.setText(text);
tabView.setBackgroundColor(mContext.getResources().getColor(R.color.black));
//tabView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pem_tab_normal));
}
public void selectTab(int tabIndex) {
if(tabIndex < -1 || tabIndex >= tabs.size()){
throw new IndexOutOfBoundsException();
}
for(int i = 0; i < tabs.size(); i++){
if(i == tabIndex){
PemTab selectedTab = tabs.get(i);
TextView tabView = selectedTab.getView();
tabView.setTextColor(android.graphics.Color.parseColor("#1968A6"));
tabView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pem_tab_selected));
//tabView.setBackgroundColor(mContext.getResources().getColor(R.color.pem_gray));
}
else{
PemTab selectedTab = tabs.get(i);
TextView tabView = selectedTab.getView();
tabView.setTextColor(Color.BLACK);
tabView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pem_tab_normal));
//tabView.setBackgroundColor(mContext.getResources().getColor(R.color.whitesmoke));
}
}
//Toast.makeText(mContext, "cur tab " + tabIndex + ", " + selectedTab.getTitle(), Toast.LENGTH_SHORT).show();
}
public void setOnTabChangedListener(OnTabChangedListener onTabChangedListener){
mOnTabChangedListener = onTabChangedListener;
}
public interface OnTabChangedListener{
void onTabChanged(int position);
}
class PemTab{
private String title;
private TextView mView;
private int index;
public PemTab() {
super();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public TextView getView() {
return mView;
}
public void setView(TextView mView) {
this.mView = mView;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
}