android关于自定义radiobutton做成的横向单选滑动条,在线等答案

qq_16472815 2014-06-12 05:44:45
话不多说,直接贴上代码!问题是现在运行的时候显示的是空白,完全没有显示出我要加载的数据选项!麻烦各位帮我瞅瞅!

这个是自定义混合控件的代码:
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<String> titleList;
private RadioGroup radioGroup;
private ImageView imageView;
private Integer beginIdNum;
private ArrayList<RadioButton> radioButtonLists;
private ArrayList<Integer> radioButtonIdList;
private ArrayList<Integer> 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<RadioButton>();
radioButtonIdList=new ArrayList<Integer>();
radioButtonWidthList= new ArrayList<Integer>();
}

private void accordingDataShow()
{
if(titleList == null || titleList.size() == 0)
{
return;
}
else
{
radioButtonIdList.clear();
radioButtonWidthList.clear();
beginIdNum= 666;
for(int index=0;index<titleList.size();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<beforeButtonId;beforenum++)
begindistance+=radioButtonWidthList.get(beforenum-666);

for(int distanceNum=beforeButtonId;distanceNum<currentButtonId;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<String> list)
{
titleList=list;
accordingDataShow();
}
}
这个是混合控件的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="@color/light_gray"
android:orientation="vertical" >

<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:fadingEdge="@null"
android:scrollbars="none" >

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/light_gray" >

<RadioGroup
android:id="@+id/hlistview_radiogroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:orientation="horizontal" >


</RadioGroup>

<ImageView
android:id="@+id/hlistview_imageview"
android:layout_width="88dp"
android:layout_height="3dp"
android:layout_alignParentBottom="true"
android:background="@color/green" />
</RelativeLayout>
</HorizontalScrollView>

</LinearLayout>

这个是测试的activity:
package com.demo.activity;

import java.util.ArrayList;

import com.example.yuan_library.R;
import com.ylibrary.view.HorizontalListview;

import android.app.Activity;
import android.os.Bundle;

public class HorizontalListviewDemo extends Activity
{
private HorizontalListview listview;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.horizontallistviewactivity);
listview=(HorizontalListview)findViewById(R.id.listview);
ArrayList<String>list=new ArrayList<String>();
list.add("陈怀雨");
list.add("袁定超");
list.add("袁小龙");
list.add("张梅");
list.add("袁陈成");
list.add("于海涛");
list.add("松本五十六");
list.add("小孩");
listview.setTitleList(list);
onResume();
}
}
这个是测试activity的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.ylibrary.view.HorizontalListview
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>
...全文
911 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
muyannatong512358 2016-05-05
  • 打赏
  • 举报
回复
楼主怎么解决的,求帮助 943554103@qq.com 能不能发我一份,谢谢
m1044248152 2015-08-25
  • 打赏
  • 举报
回复
请问楼主是如何解决的 方便的话 给个demo看下 1044248152@qq.com
IcyFox 2014-06-13
  • 打赏
  • 举报
回复
还有一种方式:

		RadioButton rb = (RadioButton)findViewById(R.id.radioButton1);
		rb.setButtonDrawable(getResources().getDrawable(android.R.color.transparent)) ;
可以试试
IcyFox 2014-06-13
  • 打赏
  • 举报
回复
这个个做不到吧,因为radioButton的按钮和文字本来就是一体的。。 有一种方案可以实现: 把radioButton的text属性设置为空,旁边放一个TextView来当作radioButton的Text,此时按钮和文字是两个控件,都可以独立控制了
qq_16472815 2014-06-13
  • 打赏
  • 举报
回复
楼上的问题解决了!现在改一个问题!请问如何动态隐藏radiobutton中的button按钮,只显示字体!是动态代码的更改方式哈!谢谢!答案正确的,果断一百分!!!望各位亲积极解答哟!
nj_dobetter 2014-06-13
  • 打赏
  • 举报
回复
这是Android的控件,怎么发到J2SE板块了? 友情帮顶
内容概要:本文围绕“基于改进秃鹰算法的微电网群经济优化调度”展开研究,利用Matlab代码实现优化算法的仿真与复现。研究重点在于通过改进的秃鹰搜索算法(Bald Eagle Search Algorithm, BESA)解决微电网群在运行过程中的经济调度问题,提升算法的收敛速度与全局寻优能力,以实现对分布式能源、储能系统及负荷的高效协调管理。文中详细阐述了微电网群的系统架构、数学建模过程、目标函数设计(如运行成本最小化、碳排放降低等),并结合智能优化算法进行求解,验证了改进算法相较于传统方法在调度精度和效率方面的优越性。同时,研究还探讨了算法在多场景下的适应性,为微电网群的智能化、低碳化运行提供了技术支持与实践参考。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事微电网、智能优化算法相关工作的工程技术人员。; 使用场景及目标:① 学习并掌握改进秃鹰算法在复杂优化问题中的应用方法;② 实现微电网群经济调度模型的构建与求解;③ 对比不同智能算法在电力系统优化中的性能表现;④ 为科研论文复现、课题研究或工程项目提供算法支持与代码参考。; 阅读建议:建议读者结合文中提供的Matlab代码逐模块分析,重点关注算法改进策略与调度模型的耦合实现方式,同时可尝试在不同参数设置或场景件下进行仿真实验,以加深对算法性能与调度效果的理解。
内容概要:本文围绕多旋翼无人机姿态估计算法的开发与性能评估展开系统性研究,重点对比了线性与非线性滤波算法在复杂飞行环境下的表现。通过构建基于扩展卡尔曼滤波(EKF)、无迹卡尔曼滤波(UKF)等先进算法的姿态估计器,融合IMU、磁力计、视觉传感器等多源数据,有效提升了姿态解算的精度与鲁棒性。研究涵盖了静态悬停、高速机动及磁干扰等多种典型飞行场景,利用MATLAB/Simulink平台完成仿真实验,并结合实测飞行数据与VICON高精度运动捕捉系统提供的真值进行定量分析。结果表明,非线性滤波器在动态工况下具有显著优势,尤其在抑制漂移和抵抗外部干扰方面优于传统线性方法。文章还提出了传感器融合、自适应建模、冗余配置等一系列降低偏差影响的技术路径,为高可靠性无人机导航系统的设计提供了理论依据与实践指导。; 适合人群:具备控制理论、信号处理及状态估计基础知识,从事无人机导航、控制算法研发或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于开发高精度无人机姿态解算模块;②为多传感器融合算法的设计、实现与验证提供参考案例;③服务于复杂环境下无人系统状态估计的教学演示与科研攻关; 阅读建议:建议结合文中所述MATLAB/Simulink仿真模型与实测数据集进行复现与调试,重点关注EKF与UKF的数学推导、实现细节及关键参数调优过程,深入理解非线性系统建模、状态估计与工程实际之间的平衡与折衷。

80,489

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧