在Listview android中的Seekbar中滚动问题

时间:2014-01-02 13:18:21

标签: android listview android-listview scroll seekbar

public class Thirrd extends Activity {

ListView list;
private int[] seekbarval = new int[] { 100, 20, 80, 50, 40, 10, 0, 70, 30,
        90, 15, 35, 65, 75, 70, 100, 25, 89 };

/*
 * (non-Javadoc)
 *
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testscreen);
    list = (ListView) findViewById(R.id.listView1);
    list.setAdapter(new AdapterSeek(Thirrd.this,seekbarval));
    list.setOnItemClickListener(new OnItemClickListener() {

        /*
         * (non-Javadoc)
         *
         * @see
         * android.widget.AdapterView.OnItemClickListener#onItemClick(android
         * .widget.AdapterView, android.view.View, int, long)
         */
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Log.e("onItemClick", "" + arg2);

        }

    });
     } 
 }

适配器类

public class AdapterSeek extends BaseAdapter {
LayoutInflater layoutInflter;

int []ss;
Activity mActivity;
 public AdapterSeek() {

    }

    public AdapterSeek(Thirrd thirrd, int[] seekbarval) {
    // TODO Auto-generated constructor stub
        mActivity=thirrd;
        ss=seekbarval;
        layoutInflter = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getCount()
     */
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return ss.length;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getItem(int)
     */
    @Override
    public Integer getItem(int position) {
        // TODO Auto-generated method stub
        return ss[position];
    }

    class PlaceHolder {
        TextView labell;
        SeekBar seek;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getItemId(int)
     */
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getView(int, android.view.View,
     * android.view.ViewGroup)
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PlaceHolder placeHolder;
        if (convertView == null) {
            convertView = layoutInflter.inflate(R.layout.list_row, null);
            placeHolder = new PlaceHolder();
            placeHolder.labell = (TextView) convertView
                    .findViewById(R.id.textView1);
            placeHolder.seek = (SeekBar) convertView
                    .findViewById(R.id.seekBar1);

            convertView.setTag(placeHolder);
        } 
        else
        {
             placeHolder = (PlaceHolder) convertView.getTag();
        }
        placeHolder.labell.setText(" No. " +position );
        placeHolder.seek.setProgress(ss[position]);
        placeHolder.seek
                .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        Log.e("onStopTrackingTouch",
                                "" + seekBar.getProgress());
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                        Log.e("onStartTrackingTouch",
                                "" + seekBar.getProgress());
                    }

                    @Override
                    public void onProgressChanged(SeekBar seekBar,
                            int progress, boolean fromUser) {
                        if (fromUser)
                            Log.e("onProgressChanged", "" + progress);
                    }
                });

        return convertView;
    }



}

List_row.xml          

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="100dp"
    android:layout_height="wrap_content" />

 </LinearLayout>

当我运行上面的代码并滚动listview时,seekbar拇指位置丢失了我之前放入的状态,这意味着搜索栏不能完美地工作所以任何想法锄头我能解决吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    PlaceHolder placeHolder;

    //declare position
    final int Index = position;

    if (convertView == null) {
        convertView = layoutInflter.inflate(R.layout.list_row, null);
        placeHolder = new PlaceHolder();
        placeHolder.labell = (TextView) convertView
                .findViewById(R.id.textView1);
        placeHolder.seek = (SeekBar) convertView
                .findViewById(R.id.seekBar1);

        convertView.setTag(placeHolder);
    } 
    else
    {
         placeHolder = (PlaceHolder) convertView.getTag();
    }
    placeHolder.labell.setText(" No. " +position );
    placeHolder.seek.setProgress(ss[position]);
    placeHolder.seek
            .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                int progressChanged = 0;

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                    //change array and notify changes
                    int myProgress = progressChanged;
                    int ssPosition = ss.get(Index);
                    int[] newSS =  ss.clone();
                    newSS = ArrayUtils.remove(newSS, ssPosition);
                    newSS = ArrayUtils.add(newSS, myProgress);
                    ss = newSS;
                    notifyDataSetChanged();


                    Log.e("onStopTrackingTouch",
                            "" + seekBar.getProgress());
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    Log.e("onStartTrackingTouch",
                            "" + seekBar.getProgress());
                }

                @Override
                public void onProgressChanged(SeekBar seekBar,
                        int progress, boolean fromUser) {

                    progressChanged = progress;
                     if (fromUser)
                        Log.e("onProgressChanged", "" + progress);
                }
            });

    return convertView;
}

}