Android_实例_利用RadioButton实现分类筛选
来源:互联网 发布:手机图片编辑软件 编辑:程序博客网 时间:2024/06/02 01:56
本博文为子墨原创,转载请注明出处!
http://blog.csdn.net/zimo2013/article/details/17613229
1.示意图
2.代码实现
/** * CategoryView.java * @author zimo2013 * @see http://blog.csdn.net/zimo2013 * */public class CategoryView extends LinearLayout implementsOnCheckedChangeListener {private LayoutInflater inflater;public CategoryView(Context context) {this(context, null);}public CategoryView(Context context, AttributeSet attrs) {super(context, attrs);inflater = LayoutInflater.from(context);}/**添加方法*/public void add(List<String> list) {if (list.size() > 0) {//加载布局View view = inflater.inflate(R.layout.category_container, null);addView(view);RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.container);RadioButton bt = newRadioButton("全部");radioGroup.addView(bt);// 默认选中radioGroup.check(bt.getId());// 全部for (String str : list) {bt = newRadioButton(str);//实例化新的RadioButtonradioGroup.addView(bt);}//为当前RadioGroup设置监听器radioGroup.setOnCheckedChangeListener(this);}}/**创建RadioButton*/private RadioButton newRadioButton(String text) {RadioButton button = new RadioButton(getContext());RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,RadioGroup.LayoutParams.WRAP_CONTENT);//设置内外边距params.leftMargin = 6;params.rightMargin = 6;button.setLayoutParams(params);button.setPadding(4, 0, 4, 0);//设置背景button.setBackgroundResource(R.drawable.selector_category_bg);//去掉左侧默认的圆点button.setButtonDrawable(android.R.color.transparent);//设置不同状态下文字颜色,通过ColorStateList,对应的selector放在res/color文件目录中,否则没有效果button.setTextColor(getResources().getColorStateList(R.color.selector_category_text));button.setGravity(Gravity.CENTER);button.setText(text);return button;}@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if(mListener != null){mListener.click(group, checkedId);}}/**指定监听器*/public void setOnClickCategoryListener(OnClickCategoryListener l){mListener = l;}private OnClickCategoryListener mListener;/**回掉接口*/public interface OnClickCategoryListener{/**点击事件发生*/public void click(RadioGroup group, int checkedId);}}/** * MainActivity.java 测试 * @author zimo2013 * @see http://blog.csdn.net/zimo2013 * */public class MainActivity extends Activity {private List<String> list = new ArrayList<String>();private List<String> list1 = new ArrayList<String>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);CategoryView categoryView = (CategoryView) findViewById(R.id.category);list.add("内地1");list.add("内地2");list.add("内地3");list.add("内地4");list.add("内地5");list.add("内地6");list.add("内地7");list.add("内地8");list.add("内地9");list1.add("动作");list1.add("喜剧");list1.add("励志");list1.add("动作2");list1.add("喜剧2");list1.add("励志2");list1.add("动作3");list1.add("喜剧3");list1.add("励志3");categoryView.add(list);categoryView.add(list1);categoryView.add(list);//设置自定义监听器categoryView.setOnClickCategoryListener(new OnClickCategoryListener() {//逻辑回掉@Overridepublic void click(RadioGroup group, int checkedId) {RadioButton button = (RadioButton) group.findViewById(checkedId);Toast.makeText(getApplicationContext(), button.getText(), 0).show();}});}}<?xml version="1.0" encoding="utf-8"?><!-- selector_category_text.xml 置于res/color 目录下 --><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:color="@color/white"/> <item android:state_focused="true" android:color="@color/white"/> <item android:color="@color/gray"/></selector><?xml version="1.0" encoding="utf-8"?><!-- selector_category_bg.xml 置于res/drawable 目录下 --><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@color/red"/> <item android:state_focused="true" android:drawable="@color/red"/> <item android:drawable="@color/white"/></selector><?xml version="1.0" encoding="utf-8"?><!-- category_container.xml b布局文件 --><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:orientation="vertical" > <HorizontalScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="4dp" android:layout_marginTop="4dp" android:scrollbars="none" > <RadioGroup android:id="@+id/container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > </RadioGroup> </HorizontalScrollView> <View android:layout_width="match_parent" android:layout_height="2.0px" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:background="#ffcccccc" /></LinearLayout>
代码下载》》
3 0
- Android_实例_利用RadioButton实现分类筛选
- PHPCMS 利用联动菜单实现分类筛选功能
- 利用SVM 实现文本分类的实例
- RadioButton实现小圆点轮播_
- 449_实现筛选菜单
- Android_实现星星控件_学习
- Android_实现星星控件_学习
- RadioButton和HorizontalScrollView实现分类标签
- Android_利用URL实现下载功能
- Android——分类,筛选功能实现
- 利用Radiogroup Radiobutton 实现滑动效果菜单
- android RadioButton利用selector 实现Tab功能
- 利用分类实现功能
- RadioButton实例
- 利用javascript实现页面内容筛选
- android_广播的分类
- Android_聊天_表情
- Android_静态添加Fragment&RadioButton切换
- 优化之drawcall
- 编写软件测试用例应注意的问题
- mtk上实现socket连接
- java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.LoopTag
- 设计模式之迭代模式
- Android_实例_利用RadioButton实现分类筛选
- response,request理解
- 通过路径获取信息的几种方式
- MySQL主从复制与读写分离
- 解析xml格式的string内容并修改节点内容
- Unity3D占用内存太大的解决方法
- 安家了
- Flex框架总结
- UVa10010 - Where's Waldorf?-字符串(8方向搜索)-难度3