问题描述
我有一个使用不同的XML文件来创建视图,使项目出一个ListView。其中一个XML文件中包含的RatingBar。一切都显示,看起来非常好。
我试图附加一个onclick处理到的RatingBar展开新的活动。我的RatingBar是Android的风格:ATTR / ratingBarStyleSmall;所以它只是一个指标(我想小的RatingBar点击将用户带到一个活动,他们可以做不同的评级)。
我的问题是onclick处理的RatingBar从来没有被执行。是什么使得它更有趣的是,我已经用同样的code键使的LinearLayout点击,它工作正常。谁能告诉我为什么?
我的适配器的getView看起来这样的:
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
整型= getItemViewType(位置);
//获取查看这个列表项目
视图V = convertView;
如果(V == NULL){
LayoutInflater VI =(LayoutInflater)的getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
开关(类型){
// ...
案例TYPE_LOOKUP:
V = vi.inflate(R.layout.layout_itemlist_itemlookup,父母,假);
的LinearLayout = VLOOKUP(LinearLayout中)v.findViewById(R.id.itemlist_lookup);
如果(VSTORE!= NULL){
vStore.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
//这个处理程序工作正常
意图intentLaunchLookup =新的意图(ActivityItemList.this,ActivityLookup.class);
startActivity(intentLaunchLookup);
}
});
}
打破;
案例TYPE_SEPARATOR:
V = vi.inflate(R.layout.layout_itemlist_itemseparator,父母,假);
的RatingBar R =(的RatingBar)v.findViewById(R.id.itemlist_rating);
如果(R!= NULL){
r.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
//这个处理程序不会被执行(R IS NOT NULL;所以这应该已创建)
意图intentLaunchRating =新的意图(ActivityItemList.this,ActivityRating.class);
startActivity(intentLaunchRating);
}
});
}
打破;
// ...
}
}
// ...
//返回创建的视图
返回伏;
}
其原因 setOnClickListener()
不工作是,的RatingBar
覆盖的onTouchEvent()
(实际上它的父类, AbsSeekBar
,那样),从来没有让查看
照顾它,所以查看#performClick()
永远不会被调用(这将称为 OnClickListener
)。
有两种可能的解决方法:
从的RatingBar
- 导出并重写
的onTouchEvent()
- 使用
OnTouchListener
代替,就像这样:ratingBar.setOnTouchListener(新OnTouchListener(){ @覆盖 公共布尔onTouch(视图V,MotionEvent事件){ 如果(event.getAction()== MotionEvent.ACTION_UP){ // TODO这里执行你的行动 } 返回true; }
心连心,乔纳斯
I have a ListView that uses different XML files to create Views and make items out of. One of these XML files contains a RatingBar. Everything displays and looks excellent.
I'm trying to attach an onClick handler to the RatingBar to launch a new Activity. My RatingBar is of style ?android:attr/ratingBarStyleSmall; so it's just an indicator (I want the small RatingBar click to take the user to an Activity where they can do various ratings).
My problem is the onClick handler for the RatingBar never gets executed. What makes it more interesting is that I've used the same code to make a LinearLayout clickable and it works fine. Could anyone tell me why?
My Adapter's getView looks as such:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
// get the View for this list item
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (type) {
// ...
case TYPE_LOOKUP:
v = vi.inflate(R.layout.layout_itemlist_itemlookup, parent, false);
LinearLayout vLookup = (LinearLayout)v.findViewById(R.id.itemlist_lookup);
if (vStore != null) {
vStore.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// THIS HANDLER WORKS FINE
Intent intentLaunchLookup = new Intent(ActivityItemList.this, ActivityLookup.class);
startActivity(intentLaunchLookup);
}
});
}
break;
case TYPE_SEPARATOR:
v = vi.inflate(R.layout.layout_itemlist_itemseparator, parent, false);
RatingBar r = (RatingBar)v.findViewById(R.id.itemlist_rating);
if (r != null) {
r.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// THIS HANDLER DOES NOT GET EXECUTED (r IS NOT NULL; SO THIS SHOULD HAVE BEEN CREATED)
Intent intentLaunchRating = new Intent(ActivityItemList.this, ActivityRating.class);
startActivity(intentLaunchRating);
}
});
}
break;
// ...
}
}
// …
// return the created view
return v;
}
The reason for setOnClickListener()
not working is that RatingBar
overrides onTouchEvent()
(actually its super class, AbsSeekBar
, does) and never let View
take care of it, so View#performClick()
is never called (which would have called the OnClickListener
).
Two possible workarounds:
- derive from
RatingBar
and override onTouchEvent()
- use
OnTouchListener
instead, like so:ratingBar.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { // TODO perform your action here } return true; }
HTH,Jonas
这篇关于的RatingBar的onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!