为什么onClickListener不起作用?

时间:2015-07-05 22:13:45

标签: android onclicklistener

我创建了一个从RelativeLayout扩展的视图,其中包含一些ImageView,其中包含onClick个侦听器。 但是,它不起作用,我试图展示Toast但没有成功。 我误解了一些关于View的事情。

这是我的代码: (片段包含XML格式的视图)

public class Timeline extends RelativeLayout {

private Context context;
private ImageView imageViewFirstStep;
private ImageView imageViewSecondStep;
private ImageView imageViewThirdStep;
private ImageView imageViewEndStep;
private View timelineView;
private int viewWidth;
private RelativeLayout relativeLayout;
private Drawable drawableFirstStep;
private Drawable drawableSecondStep;
private Drawable drawableThirdStep;
private Drawable drawableEndStep;
private RelativeLayout.LayoutParams layoutParamsStep;
private ProcessActivity activity;

public Timeline(Context context) {
    super(context);
    activity = (ProcessActivity) context;
    init();
}

public Timeline(Context context, AttributeSet attrs){
    super(context, attrs);
    activity = (ProcessActivity) context;
    init();
}

public Timeline(Context context, AttributeSet attrs, int defStyle){
    super(context, attrs, defStyle);
    activity = (ProcessActivity) context;
    init();
}

public void init() {
    inflate(getContext(), R.layout.timeline, this);

    this.context = getContext();

    this.imageViewFirstStep = (ImageView) findViewById(R.id.timeline_first_step);
    this.imageViewSecondStep = (ImageView) findViewById(R.id.timeline_second_step);
    this.imageViewThirdStep = (ImageView) findViewById(R.id.timeline_third_step);
    this.imageViewEndStep = (ImageView) findViewById(R.id.timeline_end_step);

    this.timelineView = (View) findViewById(R.id.timeline_timeline);

    this.relativeLayout = (RelativeLayout) findViewById(R.id.timeline);

}

public void setTimelineImages(int firstStep, int secondStep,
                              int thirdStep, int endStep){

    viewWidth = timelineView.getWidth();

    drawableFirstStep = context.getResources().getDrawable(firstStep);
    drawableSecondStep = context.getResources().getDrawable(secondStep);
    drawableThirdStep = context.getResources().getDrawable(thirdStep);
    drawableEndStep = context.getResources().getDrawable(endStep);

    imageViewFirstStep.setImageDrawable(drawableFirstStep);
    imageViewSecondStep.setImageDrawable(drawableSecondStep);
    imageViewThirdStep.setImageDrawable(drawableThirdStep);
    imageViewEndStep.setImageDrawable(drawableEndStep);

    imageViewSecondStep.setPadding(viewWidth / 3, 0, 0, 0);
    imageViewThirdStep.setPadding((viewWidth / 3) * 2, 0, 0, 0);
    imageViewEndStep.setPadding(viewWidth, 0, 0, 0);

    imageViewFirstStep.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            AtelierFragment atelierFragment = new AtelierFragment();
            activity.showTest(atelierFragment);
            /*AtelierFragment atelierFragment = (AtelierFragment) activity.getFragmentManager().findFragmentById(R.id.container);
            FragmentTransaction fragmentTransaction =
                    activity.getFragmentManager().beginTransaction().replace(R.id.container, atelierFragment);
            fragmentTransaction.commit();*/
            Toast.makeText(v.getContext(), "test", Toast.LENGTH_LONG).show();
        }
    });

    imageViewSecondStep.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    imageViewThirdStep.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    imageViewEndStep.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

/*
* set le texte affiché dans les images de la timeline en fonction des string passés en paramètre
* */

public void setTimelineTextView(String Step, TextView textViewStep, int position){

    textViewStep = new TextView(context);
    textViewStep.setText(Step);
    Typeface face = Typeface.createFromAsset(context.getAssets(),
            "fonts/TradeGothic-BoldCondTwenty.otf");
    textViewStep.setTypeface(face);
}

我的观点的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/timeline">
    <View
        android:id="@+id/timeline_timeline"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="25px"
        android:layout_marginRight="25px"
        android:background="@color/hypred_menu_bleu_clair"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:id="@+id/timeline_first_step"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:id="@+id/timeline_second_step"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:id="@+id/timeline_third_step"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:id="@+id/timeline_end_step"/>
</RelativeLayout>

我的片段的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="100"
    android:background="@color/hypred_blanc">
    <LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="15"></LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="70"
        android:orientation="vertical"
        android:weightSum="100">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="30"
            android:weightSum="100"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="20"></LinearLayout>
            <com.ylly.hypred.process.view.Timeline
                android:id="@+id/fragment_organe_timeline"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="60">
            </com.ylly.hypred.process.view.Timeline>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="20"></LinearLayout>
        </LinearLayout>

        <com.ylly.hypred.custom.MyTextView
            android:id="@+id/fragment_atelier_choix_atelier_text_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:gravity="center"
            android:text="@string/choix_equipement"
            android:textSize="21dp"
            android:textColor="@color/hypred_bleu"
            android:layout_marginBottom="5dp"
            custom:font_name="Arial-Bold.ttf"
            android:background="@drawable/gradient"
            android:layout_weight="5"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="65"
            android:weightSum="100"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="10"></LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="80"
                android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="60"
                android:weightSum="100"
                android:orientation="vertical"
                android:background="@drawable/right_border">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="50"
                    android:orientation="horizontal"
                    android:weightSum="100">
                    <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="25">
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/image_view_ligne_1_groupe_1"
                            android:src="@drawable/hypred_organe_groupe_1"
                            android:scaleType="fitCenter"
                            android:adjustViewBounds="true"/>
                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="75">
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/image_view_ligne_1_groupe_2"
                            android:src="@drawable/hypred_organe_groupe_3"
                            android:scaleType="fitCenter"
                            android:adjustViewBounds="true"/>
                    </LinearLayout>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="50"
                    android:weightSum="100"
                    android:orientation="horizontal">
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="50">
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:id="@+id/image_view_ligne_2_groupe_1"
                            android:src="@drawable/hypred_organe_groupe_2"/>
                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="25">
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:id="@+id/image_view_ligne_2_groupe_2"
                            android:src="@drawable/hypred_organe_groupe_1"/>
                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="25">
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:id="@+id/image_view_ligne_2_groupe_3"
                            android:src="@drawable/hypred_organe_groupe_1"/>
                    </LinearLayout>

                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="40"
                android:weightSum="100"
                android:orientation="vertical">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="50"
                        android:weightSum="100"
                        android:orientation="horizontal">
                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="50"
                            android:weightSum="100"
                            android:orientation="vertical"
                            android:layout_gravity="center">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="0dp"
                                android:layout_weight="80"
                                android:id="@+id/fragment_organe_circle_one"
                                android:layout_gravity="center"/>
                            <com.ylly.hypred.custom.MyTextView
                                android:layout_width="wrap_content"
                                android:layout_height="0dp"
                                android:layout_weight="20"
                                android:id="@+id/fragment_organe_text_view_circle_one"
                                android:textColor="@color/hypred_gris"
                                custom:font_name="Arial-Bold.ttf"
                                android:layout_gravity="center"/>

                        </LinearLayout>
                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="50"
                            android:weightSum="100"
                            android:orientation="vertical"
                            android:layout_gravity="center">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="0dp"
                                android:id="@+id/fragment_organe_circle_two"
                                android:layout_weight="80"
                                android:layout_gravity="center"/>
                            <com.ylly.hypred.custom.MyTextView
                                android:layout_width="wrap_content"
                                android:layout_height="0dp"
                                android:layout_weight="20"
                                android:id="@+id/fragment_organe_text_view_circle_two"
                                android:textColor="@color/hypred_gris"
                                custom:font_name="Arial-Bold.ttf"
                                android:layout_gravity="center"/>

                        </LinearLayout>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="50"
                        android:weightSum="100"
                        android:orientation="horizontal"
                        android:layout_gravity="center">
                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="50"
                            android:weightSum="100"
                            android:orientation="vertical"
                            android:layout_gravity="center">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="0dp"
                                android:id="@+id/fragment_organe_circle_three"
                                android:layout_weight="80"
                                android:layout_gravity="center"/>
                            <com.ylly.hypred.custom.MyTextView
                                android:layout_width="wrap_content"
                                android:layout_height="0dp"
                                android:layout_weight="20"
                                android:id="@+id/fragment_organe_text_view_circle_three"
                                android:textColor="@color/hypred_gris"
                                custom:font_name="Arial-Bold.ttf"
                                android:layout_gravity="center"/>

                        </LinearLayout>
                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="50"
                            android:weightSum="100"
                            android:orientation="vertical"
                            android:layout_gravity="center">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="0dp"
                                android:id="@+id/fragment_organe_circle_four"
                                android:layout_weight="80"
                                android:layout_gravity="center"/>
                            <com.ylly.hypred.custom.MyTextView
                                android:layout_width="wrap_content"
                                android:layout_height="0dp"
                                android:layout_weight="20"
                                android:id="@+id/fragment_organe_text_view_circle_four"
                                android:textColor="@color/hypred_gris"
                                custom:font_name="Arial-Bold.ttf"
                                android:layout_gravity="center"/>

                        </LinearLayout>
                    </LinearLayout>
            </LinearLayout>
        </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="10"></LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="15"></LinearLayout>
</LinearLayout>

提前致谢并度过美好的一天:)

0 个答案:

没有答案