超好用的滑动tab控件

来源:互联网 发布:python加减乘除函数 编辑:程序博客网 时间:2024/05/18 00:26

因为项目需要,做了这么个控件,分享给大家!
首先看效果图:
这里写图片描述

然后看代码,就一个类,很简单的

/** *  * @author jhone  * @time 2016-09-29 * */public class SliderView extends RelativeLayout {    private Context context;    public SliderView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        // TODO Auto-generated constructor stub        init(context);    }    public SliderView(Context context, AttributeSet attrs) {        this(context, attrs, 0);        // TODO Auto-generated constructor stub    }    public SliderView(Context context) {        this(context, null);        // TODO Auto-generated constructor stub    }    private android.widget.LinearLayout.LayoutParams params_ll_slider;    private LinearLayout ll_slider,ll_content;    private View slider;//滑块    private void init(Context context) {        // TODO Auto-generated method stub        this.context=context;        setBackgroundResource(R.drawable.slider_bg);        params_ll_slider=new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT,android.widget.LinearLayout.LayoutParams.MATCH_PARENT);        ll_slider=new LinearLayout(context);        ll_slider.setLayoutParams(params_ll_slider);        ll_slider.setOrientation(LinearLayout.HORIZONTAL);        android.widget.LinearLayout.LayoutParams params_slider=new android.widget.LinearLayout.LayoutParams(0,android.view.ViewGroup.LayoutParams.MATCH_PARENT);        params_slider.weight=1;        slider=new View(context);        slider.setLayoutParams(params_slider);        slider.setBackgroundResource(R.drawable.slider_view);        ll_slider.addView(slider);        ll_content=new LinearLayout(context);        ll_content.setLayoutParams(params_ll_slider);        ll_content.setOrientation(LinearLayout.HORIZONTAL);        addView(ll_slider);        addView(ll_content);    }    private ArrayList<TextView> childs;    public void setTitles(final ArrayList<String> titles){        childs=new ArrayList<TextView>();        ll_slider.setWeightSum(titles.size());        TextView child=null;        android.widget.LinearLayout.LayoutParams params=new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT,android.widget.LinearLayout.LayoutParams.MATCH_PARENT,1.0f);        ColorStateList colorStateList = getResources().getColorStateList(R.color.slider_textcolor);          for (int i = 0; i < titles.size(); i++) {            child=new TextView(context);            child.setLayoutParams(params);            child.setGravity(Gravity.CENTER);            child.setText(titles.get(i));            child.setTextColor(colorStateList);            child.setTextSize(16);            child.setOnClickListener(new OnClickListener() {                @Override                public void onClick(View view) {                    // TODO Auto-generated method stub                    doAnimal(view);                }            });            childs.add(child);            ll_content.addView(child);        }        requestLayout();        mSelectedItem=childs.get(0);        mSelectedItem.setEnabled(false);    }    private View mSelectedItem;    protected void doAnimal(final View view) {        // TODO Auto-generated method stub        TranslateAnimation animation = new TranslateAnimation(mSelectedItem.getLeft(),                view.getLeft(), 0, 0);        animation.setInterpolator(new AccelerateDecelerateInterpolator());        animation.setFillAfter(true);        animation.setDuration(400);        animation.setAnimationListener(new AnimationListener() {            @Override            public void onAnimationStart(Animation arg0) {                // TODO Auto-generated method stub            }            @Override            public void onAnimationRepeat(Animation arg0) {                // TODO Auto-generated method stub            }            @Override            public void onAnimationEnd(Animation arg0) {                // TODO Auto-generated method stub                //这里用这种方式而不用mSelectedItem和view来设置是有原因的,试下就知道,后者效果非常不友好(连续点的时候)                for (int i = 0; i < childs.size(); i++) {                    childs.get(i).setEnabled(true);                }                mSelectedItem.setEnabled(false);            }        });        slider.startAnimation(animation);        mSelectedItem=view;        if (sliderOnItemClick!=null) {            sliderOnItemClick.onItemClick(((TextView)view).getText().toString());        }    }    private SliderOnItemClick sliderOnItemClick;    public interface SliderOnItemClick{        void onItemClick(String title);    }    public void setOnItemClick(SliderOnItemClick sliderOnItemClick) {        // TODO Auto-generated method stub        this.sliderOnItemClick=sliderOnItemClick;    }    //设置当前选中的项    public void setCurrentItem(int index){        doAnimal(childs.get(index));    }}   

布局文件也看下吧

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#CCCCCC" >    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="60dp"        android:background="#56BC72"        android:padding="12dp" >        <com.example.test.SliderView            android:id="@+id/sliderView"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerInParent="true" />    </RelativeLayout></RelativeLayout>

然后是调用代码

public class TestFragment extends Fragment {    private View view;    @Override    public View onCreateView(LayoutInflater inflater,            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        // TODO Auto-generated method stub        view = inflater.inflate(R.layout.fragmet_test, null);        //1.找到控件        final SliderView sliderView = (SliderView) view                .findViewById(R.id.sliderView);        //2.设置titles        final ArrayList<String> titles = new ArrayList<String>();        titles.add("专家");        titles.add("相关研报");        titles.add("资讯");        sliderView.setTitles(titles);        //3.设置点击事件        sliderView.setOnItemClick(new SliderOnItemClick() {            @Override            public void onItemClick(String title) {                // TODO Auto-generated method stub                Toast.makeText(getActivity(), title, 0).show();            }        });        final Handler handler = new Handler();        handler.postDelayed(new Runnable() {            @Override            public void run() {                // TODO Auto-generated method stub                //可用这个方法配合viewPage或者直接响应点击事件达到切换fragment页面效果                sliderView.setCurrentItem(new Random().nextInt(titles.size()));                handler.postDelayed(this, 2000);            }        }, 3000);        return view;    }}

源码下载:https://github.com/jhone666/sliderTabView

0 0