问题
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.
Any suggestions.
I seem unable to access the seekbar from the activity as it is setup inside the ArrayAdapter to display a value from the database
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.
回答1:
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
ArrayAdapter Snippet
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
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
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);
}
});
回答2:
I had overcame this issue by setting the following attributes to the seekbar
in listview
item layout file. Then I had the click
event of listitem
and seekbar
working.
android:focusable="false"
android:focusableInTouchMode="false"
<SeekBar
android:id="@+id/playback_icon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="0"
android:progressDrawable="@drawable/red_scrubber_progress"
android:thumb="@drawable/red_scrubber_control"
android:focusable="false"
android:focusableInTouchMode="false"/>
来源:https://stackoverflow.com/questions/10340771/using-a-seekbar-within-a-listview