自定义 listView 中的 SeekBar 总是 returns 最后位置
SeekBar in custom listView always returns the last position
首先,抱歉英语不好。
我在自定义 listView 中有一个 seekBar(在一个片段中),它显示良好,获得了适当的值,但是当我尝试更改它们时,它总是尝试更改最后一个 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;
}
}
这是应用程序屏幕
在这段代码中,我一直在尝试更改蓝牙
我自己找到了答案,我需要保存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;
}
首先,抱歉英语不好。
我在自定义 listView 中有一个 seekBar(在一个片段中),它显示良好,获得了适当的值,但是当我尝试更改它们时,它总是尝试更改最后一个 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;
}
}
这是应用程序屏幕
在这段代码中,我一直在尝试更改蓝牙
我自己找到了答案,我需要保存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;
}