我有一个自定义项目的 ListView(或 GridView,它取决于屏幕大小),每个项目都包含 SeekBar。我正在设置 OnSeekBarChangeListener ,问题是,onStartTrackingTouch 在释放按钮或更改拇指位置后触发(应该在按下后)。我认为 ListView 或 ListView 行正在窃取事件,但我尝试了许多不同的解决方案,但没有任何效果。
我什至不想使用 onStartTrackingTouch。我需要这个工作的原因:当用户按下拇指时,它会改变它的状态(所以用户知道他开始拖动拇指)。现在,拇指只有在它的位置改变时才会改变它的状态。
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.seekbarList);
list.setAdapter(new Adapter());
SeekBar sb = (SeekBar) findViewById(R.id.seekBarOutsideTheList);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
((TextView) findViewById(R.id.outsideListLbl))
.setText("onStop");
Log.d("seekBarOutsideTheList", "onStop");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
((TextView) findViewById(R.id.outsideListLbl))
.setText("onStart");
Log.d("seekBarOutsideTheList", "onStart");
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
((TextView) findViewById(R.id.outsideListLbl))
.setText("onChange");
Log.d("seekBarOutsideTheList", "onChange");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class Adapter extends BaseAdapter {
LayoutInflater layoutInflter;
public Adapter() {
layoutInflter = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
class ViewHolder {
TextView label;
SeekBar seek;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflter.inflate(R.layout.list_row, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.label = (TextView) convertView
.findViewById(R.id.insideListLbl);
viewHolder.seek = (SeekBar) convertView
.findViewById(R.id.seekbarInLIst);
convertView.setTag(viewHolder);
}
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
viewHolder.seek
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
((TextView) findViewById(R.id.insideListLbl))
.setText("onStop");
Log.d("seekBarInsideTheList", "onStop");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
((TextView) findViewById(R.id.insideListLbl))
.setText("onStart");
Log.d("seekBarInsideTheList", "onStart");
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
((TextView) findViewById(R.id.insideListLbl))
.setText("onChange");
Log.d("seekBarInsideTheList", "onChange");
}
});
return convertView;
}
@Override
public int getCount() {
return 1;
}
@Override
public Integer getItem(int arg0) {
return arg0;
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SeekBar outside the list" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<SeekBar
android:id="@+id/seekBarOutsideTheList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/outsideListLbl"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
<ListView
android:id="@+id/seekbarList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
list_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SeekBar inside the list" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<SeekBar
android:id="@+id/seekbarInLIst"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/insideListLbl"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>