RatingBar onClick [英] RatingBar onClick

查看:18
本文介绍了RatingBar onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我的 Adapter 的 getView 如下所示:

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {int type = getItemViewType(位置);//获取此列表项的视图视图 v = 转换视图;如果(v == null){LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);开关(类型){//...案例 TYPE_LOOKUP:v = vi.inflate(R.layout.layout_itemlist_itemlookup, parent, false);LinearLayout vLookup = (LinearLayout)v.findViewById(R.id.itemlist_lookup);如果(vStore!= null){vStore.setOnClickListener(new View.OnClickListener() {公共无效 onClick(查看 v){//这个处理程序工作正常Intent intentLaunchLookup = new Intent(ActivityItemList.this, ActivityLookup.class);startActivity(intentLaunchLookup);}});}休息;案例 TYPE_SEPARATOR:v = vi.inflate(R.layout.layout_itemlist_itemseparator, parent, false);RatingBar r = (RatingBar)v.findViewById(R.id.itemlist_rating);如果(r!= null){r.setOnClickListener(new View.OnClickListener() {公共无效 onClick(查看 v){//这个处理程序没有被执行(r 不为空;所以应该已经创建了)Intent intentLaunchRating = new Intent(ActivityItemList.this, ActivityRating.class);startActivity(intentLaunchRating);}});}休息;//...}}//…//返回创建的视图返回 v;}

解决方案

setOnClickListener()不起作用的原因是RatingBar覆盖了onTouchEvent() (实际上是它的超类,AbsSeekBar)并且永远不会让 View 处理它,所以 View#performClick() 是从不调用(它会调用 OnClickListener).

两种可能的解决方法:

    RatingBar 派生并覆盖 onTouchEvent()
    使用 OnTouchListener 代替,像这样:<上一页>ratingBar.setOnTouchListener(new OnTouchListener() {@覆盖public boolean onTouch(View v, MotionEvent 事件) {if (event.getAction() == MotionEvent.ACTION_UP) {//TODO 在这里执行你的操作}返回真;}

在科特林中

ratingBar.setOnTouchListener(View.OnTouchListener { v, event ->if (event.action == MotionEvent.ACTION_UP) {//TODO 在这里执行你的操作}返回@OnTouchListener 真})

HTH,乔纳斯

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

In Kotlin

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

HTH, Jonas

这篇关于RatingBar onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆