在ListView中使用SeekBar [英] Using a SeekBar within a ListView
问题描述
我在列表视图中有一个搜索栏,它被禁用以阻止用户在滚动列表时更改值.如果列表项被长按,我希望启用它,允许用户更改它,然后再次禁用它.
I have a seekbar in a listview, it is disabled to stop the user from changing the value whilst scrolling the list. I wish to enable it if the list item is long clicked, allow the user to change it and then disable it again.
任何建议.
我似乎无法从活动中访问搜索栏,因为它是在ArrayAdapter中设置的,用于显示数据库中的值
I seem unable to access the seekbar from the activity as it is setup inside the ArrayAdapter to display a value from the database
由于搜索栏当前处于禁用状态,因此我可以触发列表项的click事件和long click事件.
I am able to fire a both a click event and a long click event for the list item, as the seekbar is currently disabled.
推荐答案
解决方案
在ArrayAdapter中,我将enabled和focusable都设置为false,并添加了SeekBar侦听器,将属性设置为false允许我使用列表项onItemClicked侦听器.在onItemCLickListener内部,我检索了搜索栏,将属性设置为true,这意味着它可以向上或向下滑动.调整之后,我随后将其禁用.下面的代码
Solution
In the ArrayAdapter I set both enabled and focusable to false and added the SeekBar listener, setting the attributes to false allowed me to use the list item onItemClicked listener. Inside the onItemCLickListener I retreived the seekbar, set the attributes to true, this meant it could be slid up or down. I then disabled it after the adjustment had been made. code below
此代码位于创建列表项的内部,其中包含搜索栏
this code is inside the creation of the list item, in which the seekbar is housed
seekBar.setClickable(false);
seekBar.setFocusable(false);
seekBar.setEnabled(false);
/* attach listener */
attachProgressUpdatedListener(seekBar, positionOfItemInList);
AttachProgressUpdatedListener
此方法将侦听器附加到arrayAdapter类内部的搜索栏
AttachProgressUpdatedListener
this method attaches the listener to the seekbar, inside the arrayAdapter class
private void attachProgressUpdatedListener(SeekBar seekBar,
final int position) {
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
int task_id = (Integer) seekBar.getTag();
TaskHandler taskHandler = new TaskHandler(DBAdapter
.getDBAdapterInstance(getContext()));
taskHandler.updateTaskProgress(task_id, progress);
mList.get(position).setProgress(progress);
//need to fire an update to the activity
notifyDataSetChanged();
seekBar.setEnabled(false);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// empty as onStartTrackingTouch listener not being used in
// current implementation
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// empty as onProgressChanged listener not being used in
// current implementation
}
});
}
OnItemCLickListener
此片段来自包含列表视图的活动.
OnItemCLickListener
this snipped is from the activity which houses the list view.
taskListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
SeekBar sb = (SeekBar) view.findViewById(R.id.seek);
sb.setFocusable(true);
sb.setEnabled(true);
}
});
这篇关于在ListView中使用SeekBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!