自定义listView中的SeekBar始终返回最后一个位置

时间:2015-02-05 02:12:24

标签: android listview seekbar

首先,抱歉英语不好。

我在自定义listView中有一个seekBar(在一个片段中),它显示得很好,获得了propper值,但是当我尝试更改它们时,它总是尝试更改最后一个seekBar的值。

这是适配器上的getView方法

public View getView(int position, View convertView, final ViewGroup parent) {
        View v = convertView;

        if(convertView==null){
            LayoutInflater linf=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v=linf.inflate(R.layout.activity_single_option,null);
        }

        soa=getItem(position);

        TextView textView=(TextView)v.findViewById(R.id.singleOptionTextView);
        textView.setText(soa.getTitle());

        SeekBar seekBar=(SeekBar)v.findViewById(R.id.singleOptionSeekBar);
        seekBar.setMax(soa.getSeekBarMax());
        seekBar.setProgress(soa.getValue());

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int prog=0;
            SystemManager systemManager=new SystemManager(activity);
            boolean status;
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                prog=progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d("onStopTrackingTouch",soa.getTitle());
                switch (soa.getTitle()){
                    case "Brillo":
                        systemManager.setBrightness(prog);
                        break;
                    case "WIFI":
                        status = prog == 1;
                        systemManager.toggleWifi(status);
                        break;
                    case "Datos Moviles":
                        status = prog == 1;
                        systemManager.toggleMobileData(status);
                        break;
                    case "Bluetooth":
                        status = prog == 1;
                        systemManager.toggleBluetooth(status);
                        break;
                }

            }
        });

        return v;
    }

我将Log.d放在onStopTrackingTouch方法上,这样我就可以看到我选择了哪个选项,并且始终是最后一个选项。

这是我设置每个listView项目的地方

package com.app.innergy;


import android.os.Bundle;
import android.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.ViewDragHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class CustomFragment extends Fragment {

    ListView list;

    public CustomFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ViewGroup rootView=(ViewGroup)inflater.inflate(R.layout.fragment_custom, container, false);

        ((MenuActivity)getActivity()).getSupportActionBar().hide();
        Toast.makeText(getActivity(),"Recuerda pulsar el botón \"Atrás\" para volver",Toast.LENGTH_LONG).show();

        int wifiStatus=0,bluetoothStatus=0,mobileDataStatus;

        SystemManager systemManager=new SystemManager(getActivity().getApplicationContext());

        if(systemManager.isWifiEnabled()==1)
            wifiStatus=0;
        else if (systemManager.isWifiEnabled()==3)
            wifiStatus=1;

        if(systemManager.isBluetoothEnabled()==10)
            bluetoothStatus=0;
        else if (systemManager.isBluetoothEnabled()==1)
            bluetoothStatus=1;

        if(!systemManager.isMobileDataEnabled())
            mobileDataStatus=0;
        else
            mobileDataStatus=1;

        ((MenuActivity)getActivity()).getmDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        list=(ListView)rootView.findViewById(R.id.listViewCustom);

        final ArrayList<SingleOptionActivity> itemArray=new ArrayList<SingleOptionActivity>();

        SingleOptionActivity soa;

        **soa=new SingleOptionActivity("Brillo",systemManager.getBrightness(),255);
        itemArray.add(soa);
        soa=new SingleOptionActivity("WIFI",wifiStatus,1);
        itemArray.add(soa);
        soa=new SingleOptionActivity("Datos moviles",mobileDataStatus,1);
        itemArray.add(soa);
        soa=new SingleOptionActivity("Bluetooth",bluetoothStatus,1);
        itemArray.add(soa);**

        SingleOptionAdapter singleOptionAdapter=new SingleOptionAdapter(getActivity(),itemArray);

        list.setAdapter(singleOptionAdapter);


        return rootView;
    }



}

这是应用屏幕

enter image description here

在这段代码中,我总是试图改变蓝牙

1 个答案:

答案 0 :(得分:2)

我找到了自己的答案,我需要保存seekBar的位置,所以getView方法就这样结束了。

 public View getView(int position, View convertView, final ViewGroup parent) {
        View v = convertView;
        final int pos=position;

        if(convertView==null){
            LayoutInflater linf=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v=linf.inflate(R.layout.activity_single_option,null);
        }

        soa=getItem(position);

        TextView textView=(TextView)v.findViewById(R.id.singleOptionTextView);
        textView.setText(soa.getTitle());

        SeekBar seekBar=(SeekBar)v.findViewById(R.id.singleOptionSeekBar);
        seekBar.setMax(soa.getSeekBarMax());
        seekBar.setProgress(soa.getValue());

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            SingleOptionActivity soa=getItem(pos);
            int prog=0;
            SystemManager systemManager=new SystemManager(activity);

            boolean status;
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                prog=progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d("onStopTrackingTouch",soa.getTitle());
                switch (soa.getTitle()){
                    case "Brillo":
                        systemManager.setBrightness(prog);
                        break;
                    case "WIFI":
                        status = prog == 1;
                        systemManager.toggleWifi(status);
                        break;
                    case "Datos Moviles":
                        status = prog == 1;
                        systemManager.toggleMobileData(status);
                        break;
                    case "Bluetooth":
                        status = prog == 1;
                        systemManager.toggleBluetooth(status);
                        break;
                }

            }
        });

        return v;
    }