我有一个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>
答案 0 :(得分:0)
如果您想同时选择行项目,请在行项目布局中添加android:descendantFocusability =“blocksDescendants”。拇指进步改变。
list_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
<TextView
android:id="@+id/lbl"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50" />
AdapterSeek.java
LayoutInflater layoutInflter;
/**
*
*/
public AdapterSeek() {
layoutInflter = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getCount()
*/
@Override
public int getCount() {
// TODO Auto-generated method stub
return seekbarval.length;
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItem(int)
*/
@Override
public Integer getItem(int position) {
// TODO Auto-generated method stub
return seekbarval[position];
}
class PlaceHolder {
TextView labell;
SeekBar seek;
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItemId(int)
*/
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
PlaceHolder placeHolder;
if (convertView == null) {
convertView = layoutInflter.inflate(R.layout.list_row, null);
placeHolder = new PlaceHolder();
placeHolder.labell = (TextView) convertView
.findViewById(R.id.lbl);
placeHolder.seek = (SeekBar) convertView
.findViewById(R.id.seekbar);
convertView.setTag(placeHolder);
} else
placeHolder = (PlaceHolder) convertView.getTag();
placeHolder.labell.setText(" No. " + position);
placeHolder.seek.setProgress(seekbarval[position]);
placeHolder.seek
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.e("onStopTrackingTouch",
"" + seekBar.getProgress());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.e("onStartTrackingTouch",
"" + seekBar.getProgress());
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
if (fromUser)
Log.e("onProgressChanged", "" + progress);
}
});
return convertView;
}
}
ActivityView.java
private int[] seekbarval = new int[] { 100, 20, 80, 50, 40, 10, 0, 70, 30,
90, 15, 35, 65, 75, 70, 100, 25, 89 };
/*
* (non-Javadoc)
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testscreen);
list = (ListView) findViewById(R.id.seekbarList);
list.setAdapter(new AdapterSeek());
list.setOnItemClickListener(new OnItemClickListener() {
/*
* (non-Javadoc)
*
* @see
* android.widget.AdapterView.OnItemClickListener#onItemClick(android
* .widget.AdapterView, android.view.View, int, long)
*/
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.e("onItemClick", "" + arg2);
}
});
我试图通过你的追求给你理解...如果需要请留下评论。 拇指更换&amp;我发布拇指时会调用 onStopTrackingTouch 。
答案 1 :(得分:0)
经过一些挖掘和测试后,我发现这不是错误或错误。
首先,用户应该能够毫无问题地滚动列表。因此,如果他将手指放在搜索栏上,但不会将其水平移动,他可以滚动列表,因为搜索栏的事件尚未被触发。这就是为什么它表现得像这样。
其次,根据API的版本,行为会有所不同。我测试了API 7,8和10,然后立即触发 onStartTrackingTouch 。但是在API 16上,第一个是列表滚动。
总之:我想要实现的行为是不良行为。用户应该能够轻松滚动列表。我宁愿想一想,如何改变较低API的行为,但这不是这个问题的主题。