解决ListView的item含有RadioGroup,滑动错乱问题,从国外大神博借鉴过来的

直接上代码:

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);
                            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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在 Android ListView 实现商品倒计时时,常见的时间错乱问题是由于 ListView 的视图复用机制导致的。当 ListView 滑动时,由于视图的复用,导致原本应该显示的倒计时时间被重置,出现时间错乱问题解决这个问题的方法是在 getView 方法中对倒计时时间进行保存和更新。具体实现如下: 1. 在 Bean 类中添加一个 long 类型的字段用于保存商品的倒计时时间戳: ``` public class GoodsBean { ... private long countdownTime; // 倒计时时间戳 ... // getter 和 setter 方法 } ``` 2. 在 Adapter 的 getView 方法中对倒计时时间进行保存和更新。首先获取当前商品的倒计时时间,如果该商品的倒计时时间戳已经保存,则直接使用保存的时间戳进行倒计时更新;否则,根据商品的截止时间计算倒计时时间戳并保存到商品的 countdownTime 字段中: ``` public View getView(int position, View convertView, ViewGroup parent) { ... GoodsBean goods = getItem(position); long countdownTime = goods.getCountdownTime(); if (countdownTime == 0) { countdownTime = calculateCountdownTime(goods.getEndTime()); // 根据商品的截止时间计算倒计时时间戳 goods.setCountdownTime(countdownTime); // 保存倒计时时间戳 } ... TextView tvCountdown = convertView.findViewById(R.id.tv_countdown); updateCountdown(tvCountdown, countdownTime); // 更新倒计时显示 ... } private void updateCountdown(final TextView tvCountdown, long countdownTime) { ... // 倒计时更新逻辑 ... } ``` 通过这样的方式,可以在 ListView 的视图复用过程中正确地保存和更新商品的倒计时时间,解决倒计时错乱问题
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值