简体   繁体   中英

ViewGroup (LinearLayout) and Child onClick event not passed to ViewGroup

I have a LinearLayout with several seekbars as childs, on the SeekBars i use the android:clickable=false and on the LinearLayout i have the android:clickable=true and the corresponding setOnClickListenner defined however any press on the the SeekBars doesn't trigger the LinearLayout click, i have read somewhere that it should and tested it by replacing the SeekBars with Buttons and setting the clickable property to false and the click is bubbled up to the parent. My question is why the behaviour is not similar ?

Edit: Added code example

Layout:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">

<LinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <SeekBar

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:longClickable="false"
        android:focusable="false"
        />

    <Button
        android:layout_marginTop="10dp"
        android:clickable="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click me!"/>


    </LinearLayout>

Activity:

public class MyActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);


    ViewGroup container = (ViewGroup) findViewById(R.id.container);
    container.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MyActivity.this, "Container was clicked", Toast.LENGTH_SHORT).show();
        }
    });




}
}

Having finally understood what you are asking I think this will fix it. The SeekBar is not focusable and the Button is.

This is the reason it will not work for you delete this line from the SeekBar :

android:focusable="false"

Allowing this to be focusable should make both the Button and SeekBar function the same way when they are clicked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM