当前位置:我的异常网» Android » android关于自定义radiobutton做成的横向单选滑动条
android关于自定义radiobutton做成的横向单选滑动条,答案
www.myexceptions.net 网友分享于:2014-06-22 浏览:29次
android关于自定义radiobutton做成的横向单选滑动条,在线等答案
话不多说,直接贴上代码!问题是现在运行的时候显示的是空白,完全没有显示出我要加载的数据选项!麻烦各位帮我瞅瞅!
这个是自定义混合控件的代码:
package com.ylibrary.view;
import java.util.ArrayList;
import com.example.yuan_library.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class HorizontalListview extends HorizontalScrollView
{
private Context context;
private ArrayList titleList;
private RadioGroup radioGroup;
private ImageView imageView;
private Integer beginIdNum;
private ArrayList radioButtonLists;
private ArrayList radioButtonIdList;
private ArrayList radioButtonWidthList;
public HorizontalListview(Context context)
{
super(context);
this.context=context;
initController();
accordingDataShow();
}
public HorizontalListview(Context context, AttributeSet attrs)
{
super(context, attrs);
this.context=context;
initController();
accordingDataShow();
}
public void initController()
{
LinearLayout ll=(LinearLayout) LayoutInflater.from(context).inflate(R.layout.horizontallistview,null);
radioGroup=(RadioGroup)ll.findViewById(R.id.hlistview_radiogroup);
imageView=(ImageView)ll.findViewById(R.id.hlistview_imageview);
radioButtonLists=new ArrayList();
radioButtonIdList=new ArrayList();
radioButtonWidthList= new ArrayList();
}
private void accordingDataShow()
{
if(titleList == null || titleList.size() == 0)
{
return;
}
else
{
radioButtonIdList.clear();
radioButtonWidthList.clear();
beginIdNum= 666;
for(int index=0;index
{
RadioButton button=new RadioButton(context);
button.setId(beginIdNum++);
radioButtonIdList.add(button.getId());
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
params.setMargins(12, 0, 12, 0);
button.setLayoutParams(params);
button.setBottom(0);
button.setText(titleList.get(index));
button.setTextColor(getResources().getColor(R.color.normalblack));
button.setTextSize(14);
button.setBackgroundDrawable(null);
int a=button.getWidth();
radioButtonWidthList.add(button.getWidth()+24);
radioGroup.addView(button);
radioButtonLists.add(button);
}
radioGroup.check(radioButtonIdList.get(0));
RadioButton button=radioButtonLists.get(0);
button.setTextColor(0xff62f787);
imageView.setLayoutParams(new LinearLayout.LayoutParams(radioButtonWidthList.get(radioButtonIdList.get(0)-666),LinearLayout.LayoutParams.MATCH_PARENT));
imageView.setBottom(radioGroup.getCheckedRadioButtonId());
}
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup rg, int currentButtonId)
{
AnimationSet animationSet =new AnimationSet(true);
TranslateAnimation translateAnimation = null;
int begindistance=0;
int enddistance=0;
int beforeButtonId=rg.getCheckedRadioButtonId();//获取之前选中button的id.
rg.check(currentButtonId);//设定当前选中button的id.
for(int beforenum=666;beforenum
begindistance+=radioButtonWidthList.get(beforenum-666);
for(int distanceNum=beforeButtonId;distanceNum
enddistance+=radioButtonWidthList.get(distanceNum-666);
if(currentButtonId>beforeButtonId)
translateAnimation=new TranslateAnimation(begindistance, enddistance, 0, 0);
if(beforeButtonId > currentButtonId)
translateAnimation=new TranslateAnimation(enddistance, begindistance , 0, 0);
animationSet.addAnimation(translateAnimation);
animationSet.setFillAfter(false);
animationSet.setFillAfter(true);
animationSet.setDuration(300);
imageView.startAnimation(animationSet);
}
});
}
public void setTitleList(ArrayList list)
{
titleList=list;
accordingDataShow();
}
}
这个是混合控件的布局:
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="@color/light_gray"
文章评论