24

さまざまなXMLファイルを使用してビューを作成し、そこからアイテムを作成するListViewがあります。これらのXMLファイルの1つには、RatingBarが含まれています。すべてが表示され、見栄えがします。

onClickハンドラーをRatingBarにアタッチして、新しいアクティビティを起動しようとしています。私のRatingBarのスタイルは?android:attr / ratioBarStyleSmall; したがって、これは単なる指標です(小さなRatingBarをクリックして、ユーザーをさまざまな評価を行うことができるアクティビティに移動させます)。

私の問題は、RatingBarのonClickハンドラーが実行されないことです。さらに興味深いのは、同じコードを使用してLinearLayoutをクリック可能にし、正常に動作することです。誰か教えてもらえますか?

私のアダプタの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;
}
4

6 に答える 6

64

setOnClickListener()動作しない理由は、RatingBarオーバーライドonTouchEvent()(実際にはそのスーパークラス、、は実行します)をオーバーライドし、それを処理AbsSeekBarさせないViewため、View#performClick()呼び出されることはありません(これは、を呼び出しますOnClickListener)。

2つの可能な回避策:

    から派生しRatingBarてオーバーライドonTouchEvent()
    OnTouchListener代わりに、次のように 使用します。
    ratingBar.setOnTouchListener(new OnTouchListener(){
        @オーバーライド
        public boolean onTouch(View v、MotionEvent event){
            if(event.getAction()== MotionEvent.ACTION_UP){
                //TODOはここでアクションを実行します
            }
            trueを返します。
        }
    

Kotlinで

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

HTH、ジョナス

于 2010-10-24T20:48:17.677 に答える
7

Andrew、 RatingBarで利用可能な別のイベントOnRatingBarChangeListenerを使用してみてください。この例を参照してください、それは私のために働きます!

ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            viewMarketDetails(mContext);
            dialog.dismiss();
        }
    });

したがって、onClickEventを使用する必要はありませんが、同じ効果がありますが、実際に値を変更した場合にのみ機能します。(実際の値をクリックしても効果がない場合)

于 2013-05-30T22:14:12.130 に答える
6
ratingBar1.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

            String rateValue = String.valueOf(ratingbar1.getRating());
            System.out.println("Rate for Module is"+rateValue);
        }
    });             
于 2014-11-05T08:14:06.227 に答える
5

私は、RatingBarをLinearLayoutでラップし、onClickリスナーをLinearLayoutにアタッチすることで、この問題を回避しました。私はこれをするのが好きではありませんが、それは機能します。いくつかの不思議な理由により、onClickはRatingBarに対して実行されません。

于 2010-08-11T18:19:54.760 に答える
1

ListViewでは、通常、リストビューのアイテムをクリック可能にするか、リストビューに含まれるアイテムにするかを決定する必要があります。r.isClickable()はtrueを返しますか?

RatingBarでclickableをfalseに設定し、代わりにlistViewのonClickListenerを使用して、listView行のクリックイベントを処理する方がよい場合があります。

于 2010-08-09T20:57:39.497 に答える
0

これは評価のクリックをリッスンし、評価の変更にも影響を与えません

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatRatingBar
        android:id="@+id/rating_bar_current_rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dp_16"
        android:maxHeight="@dimen/dp_23"
        android:minHeight="@dimen/dp_23"
        android:progressDrawable="@drawable/custom_rating_selector"
        android:stepSize="0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_find_provider_subheader" />


    <TextView
        android:id="@+id/tv_current_rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:layout_constraintBottom_toBottomOf="@+id/rating_bar_current_rating"
        app:layout_constraintEnd_toEndOf="@+id/tv_current_rating"
        app:layout_constraintStart_toStartOf="@+id/rating_bar_current_rating"
        app:layout_constraintTop_toTopOf="@+id/rating_bar_current_rating" />

</androidx.constraintlayout.widget.ConstraintLayout>

評価バーの上にテキストビューレイアウトを追加し、テキストビューレイアウトのクリックリスナーを聞くだけです。Constraintレイアウト内の両方のビュー。

Javaコードで

Textview textview = findViewById(R.id.tv_current_rating)
textview.setOnClickListener(this)

@override
public void onClick(View v) {
  while (v.id) {
   case R.id.tv_current_rating :
   // TODO : This will show toast on click event 
   Toast.makeText(getApplicationContext(),
               "This a toast message",
               Toast.LENGTH_LONG)
     .show();
   break;
  }
}
于 2022-01-24T11:22:02.590 に答える