PagerSwitch tab样式加上下拉刷新(一)

PagerSwitch需要实现在选中项的同时viewPager也同时变化。

public class PagerSwitch extends LinearLayout implements OnClickListener{
	
	private String[]itemNames;                       //tab标题字符串
	private List<TextView> itemTitles;
	private List<View> itemBottoms;                  //底下的一条杠
	
	private int curSelect;                           //当前选中项
	private PagerSwitchListener pagerSwitchListener;

	public PagerSwitch(Context context, AttributeSet attrs) {
		super(context, attrs);
			
		itemTitles = new ArrayList<TextView>();
		itemBottoms = new ArrayList<View>();
	}
	
	

	public void setPagerSwitchListener(PagerSwitchListener pagerSwitchListener) {
		this.pagerSwitchListener = pagerSwitchListener;
	}



	/**
	 * 初始化
	 * 
	 * @param itemName
	 */
	public void init(String[] itemName){
		this.itemNames = itemName;
		int itemWidth = WorkApplication.getInstance().getScreenWidth() / itemNames.length;     //得到每个项的宽
		
		for(int i = 0 ; i < itemName.length ; i ++){
			RelativeLayout item = (RelativeLayout)LayoutInflater.from(getContext()).inflate(R.layout.pager_switch_item, null);
			TextView title = (TextView)item.findViewById(R.id.titletext);
			View bottom = (View)item.findViewById(R.id.bottomView);
			View right = (View)item.findViewById(R.id.rightSp);
			
			if(i == itemName.length - 1){
				right.setVisibility(View.GONE);      //最右边的那个的小竖杠可以去掉
			}
			bottom.setVisibility(View.GONE);
			title.setText(itemName[i]);
			item.setTag(i);
			item.setOnClickListener(this);
			
			addView(item);
			android.view.ViewGroup.LayoutParams params = item.getLayoutParams();
			params.width = itemWidth;
			item.setLayoutParams(params);
			
			itemTitles.add(title);
			itemBottoms.add(bottom);
		}
	}
	
	/**
	 * 设置选中
	 * 
	 * @param position
	 */
	public void setSelect(int position){
		for(int i = 0 ; i < itemNames.length ; i ++){
			if(position == i){
				itemTitles.get(i).setTextColor(getContext().getResources().getColor(R.color.main_bg_color));
				itemBottoms.get(i).setVisibility(View.VISIBLE);
			}else{
				itemTitles.get(i).setTextColor(getContext().getResources().getColor(android.R.color.black));
				itemBottoms.get(i).setVisibility(View.GONE);
			}
		}
		
		curSelect = position;
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		int tag = (Integer)arg0.getTag();
		if(tag == curSelect){
			return;
		}
		
		setSelect(tag);
		if(pagerSwitchListener != null){
			pagerSwitchListener.pagerChanged(tag);     //public interface PagerSwitchListener{
		}                                                  //    public void   pagerChanged(int position);  
	}                                                          //}   
	
	
}

pager_switch_item布局


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    >
    
    <TextView 
        android:id="@+id/titletext"
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:textColor="@android:color/black"
    	android:text="测试"
    	android:layout_marginTop="10dp"
    	android:layout_centerHorizontal="true"
    	android:textSize="16sp"
        />
    
    <View 
        android:id="@+id/bottomView"
        android:layout_width="wrap_content"
    	android:layout_height="1dp"
    	android:background="@color/main_bg_color"
    	android:layout_alignLeft="@id/titletext"
    	android:layout_alignRight="@id/titletext"
    	android:layout_marginTop="38dp"
        />                    
    <!-- 底下的小红杠 -->
    <View 
        android:layout_width="match_parent"
    	android:layout_height="1dp"
    	android:background="@color/gray_list"
    	android:layout_alignParentBottom="true"
        />
    <!-- 底下的长灰杠 -->
    <View 
        android:id="@+id/rightSp"
        android:layout_width="1dp"
    	android:layout_height="match_parent"
    	android:background="@color/gray_list"
    	android:layout_alignParentRight="true"
    	android:layout_marginTop="13dp"
    	android:layout_marginBottom="13dp"
        />
    <!-- 小竖杠 -->
</RelativeLayout>







posted @ 2015-09-19 10:11  LeeJuly  阅读(186)  评论(0编辑  收藏  举报