滚动ListView后,搜索栏的进度不会保存。
我在列表视图的自定义行布局中的搜索栏有问题。 为简单起见,请参考下面的教程,在该教程中,我在布局中添加了一个搜索栏,并将“名称”字段更改为一个由搜索栏进度控制的int(“数字”):
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
https://github.com/codepath/android-custom-array-adapter-demo
在更改第一个搜索栏和上下循环视图时,将重置进度。
我在这里没有正确保存它: users.get(PersonPosition).number =进度;?
提前谢谢
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_list);
ListView listView = (ListView) findViewById(R.id.lvUsers);
ArrayList<User> arrayOfUsers = User.getUsers();
listView.setAdapter(new CustomUsersAdapter(this,
arrayOfUsers));
}
}
public class User {
//declare private data instead of public to ensure the privacy of data field of each class
public int number;
public String hometown;
public User(int number, String hometown) {
this.number = number;
this.hometown = hometown;
}
//retrieve user's name
public int getnumber(){
return number;
}
//retrieve user's name
public void setnumber(int number){
this.number= number;
}
//retrieve users' hometown
public String getHometown(){
return hometown;
}
public static ArrayList<User> getUsers() {
ArrayList<User> users = new ArrayList<User>();
users.add(new User(0, "San 1"));
users.add(new User(0, "San 2"));
users.add(new User(0, "San 3"));
users.add(new User(0, "San 4"));
users.add(new User(0, "San 5"));
users.add(new User(0, "San 6"));
users.add(new User(0, "San 7"));
users.add(new User(0, "San 8"));
users.add(new User(0, "San 9"));
users.add(new User(0, "San 10"));
users.add(new User(0, "San 11"));
users.add(new User(0, "San 12"));
return users;
}
public void showItem (String Tag) {
System.out.println("\n" + Tag +
"\nNumber: " + number +
"\nHometown:" + hometown
);
}
}
public class CustomUsersAdapter extends ArrayAdapter<User> {
Context context;
private final ArrayList<User> users;
public CustomUsersAdapter(Context context, ArrayList<User> users) {
super(context, 0, users);
this.context=context;
this.users=users;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_user, parent, false);
}
final User user = getItem(position);
final TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
SeekBar seekBar = convertView.findViewById(R.id.seekBar);
// user.showItem("#############getView()");
tvName.setText(String.valueOf(user.getnumber()));
tvHome.setText(user.getHometown());
seekBar.setProgress (user.getnumber());
final int PersonPosition = position;
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvName.setText(String.valueOf(progress));
// user.setName(progress);
// user.showItem("OnProgressChanged");
users.get(PersonPosition).number = progress;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// Return the completed view to render on screen
return convertView;
}
}
答案 0 :(得分:0)
您将希望在停止跟踪触摸时更新用户,如下所示:
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvName.setText(String.valueOf(progress));
// user.setName(progress);
// user.showItem("OnProgressChanged");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
users.get(PersonPosition).setNumber(seekBar.getProgress());
}
});
您还需要考虑使用view holder pattern。