解决ListView的item含有RadioGroup,滑动错乱问题,从国外大神博借鉴过来的
2016-07-05 22:44
459 查看
直接上代码:
MainActivity
适配器类:
model类:
布局文件:
item.xml
main.xml
githup代码链接:
MainActivity
import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; public class MainActivity extends Activity { /** Called when the activity is first created. */ private ListView listView; private ArrayList<Model> dataList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dataList = new ArrayList<Model>(); setData(); listView = (ListView)findViewById(R.id.ListView01); CustomListAdapter cadapter = new CustomListAdapter(MainActivity.this, dataList); listView.setAdapter(cadapter); } private void setData(){ Model m = new Model("Ik word op de hoogte gehouden van de ontwikkelingen binnen onze organisatie."); dataList.add(m); Model m1 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m1); Model m2 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m2); Model m3 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m3); Model m4 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m4); Model m5 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m5); Model m6 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m6); Model m7 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m7); Model m8 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m8); Model m9 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m9); Model m10 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m10); Model m11 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m11); Model m12 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m12); Model m13 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m13); Model m14 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m14); Model m15 = new Model("Ik sta achter de huidige koers van onze organisatie."); dataList.add(m15); } }
适配器类:
import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; public class CustomListAdapter extends BaseAdapter { LayoutInflater inflater; ArrayList<Model> list; public CustomListAdapter(Context context, ArrayList<Model> data) { // TODO Auto-generated constructor stub inflater = LayoutInflater.from(context); this.list = data; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View v = convertView; ViewHolder holder = null; if (v == null) { v = inflater.inflate(R.layout.item, parent, false); holder = new ViewHolder(v); v.setTag(holder); holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { Integer pos = (Integer) group.getTag(); Model element = list.get(pos); 4000 switch (checkedId) { // set the Model to hold the // answer the user picked case R.id.answer0: element.current = Model.ANSWER_ONE_SELECTED; break; case R.id.answer1: element.current = Model.ANSWER_TWO_SELECTED; break; case R.id.answer2: element.current = Model.ANSWER_THREE_SELECTED; break; case R.id.answer3: element.current = Model.ANSWER_FOUR_SELECTED; break; default: element.current = Model.NONE; // Something was // wrong set to // the default } } }); } else { holder = (ViewHolder) v.getTag(); } holder.group.setTag(new Integer(position)); // I passed the current // position as a tag holder.t.setText(list.get(position).question); // Set the question body if (list.get(position).current != Model.NONE) { RadioButton r = (RadioButton) holder.group.getChildAt(list.get(position).current); r.setChecked(true); } else { holder.group.clearCheck(); // This is required because although the // Model could have the current // position to NONE you could be dealing // with a previous row where // the user already picked an answer. } return v; } class ViewHolder { TextView t = null; RadioGroup group; ViewHolder(View v) { t = (TextView) v.findViewById(R.id.textView1); group = (RadioGroup) v.findViewById(R.id.group_me); } } }
model类:
public class Model { String question; // hold the question int current = NONE; // hold the answer picked by the user, initial is NONE(see below) public static final int NONE = 1000; // No answer selected public static final int ANSWER_ONE_SELECTED = 0; // first answer selected public static final int ANSWER_TWO_SELECTED = 1; // second answer selected public static final int ANSWER_THREE_SELECTED = 2; // third answer selected public static final int ANSWER_FOUR_SELECTED = 3; // forth answer selected public Model(String question) { this.question = question; } }
布局文件:
item.xml
<?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" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioGroup android:id="@+id/group_me" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/answer0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:text="Ans0" /> <RadioButton android:id="@+id/answer1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:text="Ans1" /> <RadioButton android:id="@+id/answer2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:text="Ans2" /> <RadioButton android:id="@+id/answer3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:text="Ans3" /> </RadioGroup> </LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="nihao" /> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/ListView01" ></ListView> </LinearLayout>
githup代码链接:
https://github.com/DevExchanges/RatingBar-ListView
相关文章推荐
- android自定义RadioGroup可以添加多种布局的实现方法
- Android控件系列之RadioButton与RadioGroup使用方法
- Android编程开发之RadioGroup用法实例
- 关于RadioGroup的xml文件
- 去掉radioGroup的图标
- cocos2dx radiogrouop实现
- 探索RadioGroup下可否添加子Layout
- android sdk---radioGroup用法实例
- android sdk --- radioGroup的选择保存学习以及animation学习
- Android 自定义View实现多行RadioGroup单选(多选)
- Android入门--RadioButton事件和RadioButton值判断
- Android入门--RadioGroup 组与onCheckedChanged 事件
- 猜猜我在想什么(RadioButtonID)
- 向左向右(RadioGroup组与onCheckedChanged)
- android 两个videoGroup中同时只能选中一个问题的解决
- Android开发:CompoundButton.onCheckedChangeListener和RadioGroup.onCheckedChangeListener冲突问题
- RadioGroup调用check(id)方法时,OnCheckedChangedListener被多次回调问题
- 使用ViewPager+Fragment+RadioGroup实现类似微信主界面
- 【android】RadioGroup+RadioButton+HorizontalScrollview实现顶部横向菜单栏
- Android在对话框中放置RadioGroup出现的报错