自己写的一个自定义的HorizontalScrollView,可以通过list集合的数量创建子项数量,简单的实现一下自定义控件,不足之处望大家见谅。在写自定义控件之前,需要了解自定义控件的方法,可以继承view实现自定义控件,继承现有的控件实现自己的控件。我是用第二个方式实现的简单控件。实现一个类似于tablayout的效果,继承HorizontalScrollView,
public class CustomHScrollView extends HorizontalScrollView {
private List<String> list; //ScrollView的子项
private RadioGroup tabsContainer;
private int smarginLeft;//左边距
private int smarginTop;//上边距
private int smarginRight;//右边距
private int smarginBottom;//下边距
private int stextSize;//Radiobutton的字体大小
private int stextColor;//Radiobutton的字体颜色
private int sbackGround;//radiobutton的背景颜色
private onItemClickListener itemClickListeners;
private RadioButton radioButton;
private Context context;
public CustomHScrollView(Context context) {
super(context); this.context=context;
}
public CustomHScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
init(context,attrs);
}
public CustomHScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context=context;
}
/**
* 初始化设置ScrollView
*
*/
private void init(Context context, AttributeSet attrs){
getValue(context,attrs);
list=new ArrayList<>();//实例化list
setFillViewport(true);//默认使子view可以拉伸来填满整个屏幕
setWillNotDraw(false);//默认不执行OnDraw()方法
//初始化盛放按钮标题的线性布局
tabsContainer = new RadioGroup(context);//radiogroup
tabsContainer.setOrientation(LinearLayout.HORIZONTAL);
tabsContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
addView(tabsContainer);//view 的方法
}
其中添加一个radioGroup来填充scrollView,自定义几个属性,需要在attr文件中添加一下几个自定义属性
<declare-styleable name="CustomScrollView">
<attr name="smarginLeft" format="dimension|reference"/>
<attr name="smarginTop" format="dimension|reference"/>
<attr name="smarginRight" format="dimension|reference"/>
<attr name="smarginBottom" format="dimension|reference"/>
<attr name="stextSize" format="dimension|reference"/>
<attr name="stextColor" format="color|reference"/>
<attr name="sbackGround" format="color|reference"/>
</declare-styleable>
具体的含义在代码中我体现这里就不在描述了,剩下的代码直接贴出来了。
/**
* 设置ScrollView的list
*
* @param
*
* @return int
*/
public void setList(List<String> lists){
this.list=lists;
Bitmap a=null;
ViewGroup.LayoutParams layoutParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
if (list.size()>0){
for (int i=0;i<list.size();i++){
radioButton=new RadioButton(context);
radioButton.setText(list.get(i)+"");
radioButton.setButtonDrawable(new BitmapDrawable(a));
radioButton.setPadding(smarginLeft,smarginTop,smarginRight,smarginBottom);
radioButton.setId(i);
radioButton.setBackgroundResource(sbackGround);
radioButton.setTextSize(stextSize);
ColorStateList csl = getResources().getColorStateList(stextColor);
radioButton.setTextColor( csl);
if (i==0){
radioButton.setChecked(true);
}else {
radioButton.setChecked(false);
}
tabsContainer.addView(radioButton,i,layoutParams);
}
}
//radiogroup的点击事件
tabsContainer.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
itemClickListeners.onitemclick(checkedId);
}
});
}
//设置子项的listview
public List<String> getList(){
return list;
}
//点击事件接口
public interface onItemClickListener{
void onitemclick(int position);
}
//设置点击事件
public void setOnItemClickListener(onItemClickListener itemClickListener){
this.itemClickListeners=itemClickListener;
}
使用方式久不列举了,作者刚学会用过博客写的不好希望大家不要建议。