gpt4 book ai didi

android - RatingBar onClick

转载 作者:IT老高 更新时间:2023-10-28 23:02:02 24 4
gpt4 key购买 nike

我有一个 ListView,它使用不同的 XML 文件来创建 View 和制作项目。这些 XML 文件之一包含 RatingBar。一切显示和看起来都很棒。

我正在尝试将 onClick 处理程序附加到 RatingBar 以启动新 Activity 。我的 RatingBar 风格为 ?android:attr/ratingBarStyleSmall;所以它只是一个指标(我希望点击小的 RatingBar 将用户带到他们可以进行各种评分的 Activity )。

我的问题是 RatingBar 的 onClick 处理程序永远不会被执行。更有趣的是,我使用相同的代码使 LinearLayout 可点击,并且效果很好。谁能告诉我为什么?

我的 Adapter 的 getView 如下所示:

@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;
}

最佳答案

setOnClickListener() 不起作用的原因是 RatingBar 覆盖了 onTouchEvent()(实际上是它的父类(super class),AbsSeekBar,确实)并且永远不会让 View 处理它,因此永远不会调用 View#performClick() (它会调用 OnClickListener)。

两种可能的解决方法:

    RatingBar 派生并覆盖 onTouchEvent()
    使用 OnTouchListener 代替,像这样:
    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;    }

In Kotlin

ratingBar.setOnTouchListener(View.OnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_UP) {
// TODO perform your action here
}
return@OnTouchListener true
})

HTH,乔纳斯

关于android - RatingBar onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3443939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com