当我更改片段中的页面时,我的onClickListener没有被调用

问题描述 投票:0回答:3

在我的片段中,我具有三个控件:两个ImageViews和一个Button

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:id="@+id/rel_layout"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#ffffffff"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:textStyle="bold"
        android:visibility="visible"
        android:textSize="25sp"
        android:layout_marginTop="10dp"
        android:layout_marginStart="10dp" />

    <ImageView
        android:id="@+id/forward"
        android:background="@drawable/chevronright"
        android:layout_below="@+id/heading"
        android:layout_alignParentEnd="true"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <ImageView
        android:id="@+id/back"
        android:background="@drawable/chevronleft"
        android:layout_below="@+id/heading"
        android:visibility="invisible"
        android:layout_alignParentStart="true"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <Button
        android:id="@+id/skip_button"
        android:text="@string/skip_button"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_below="@+id/back"
        android:layout_alignParentStart="true"
        android:textStyle="bold"
        android:layout_alignParentBottom="true"
        android:gravity="bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/heading" >

    <TextView
        android:id="@+id/body"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="normal"
        android:visibility="visible"
        android:textSize="20sp" />

    </LinearLayout>
</RelativeLayout>

在片段中,我为所有三个控件定义了onClickListeners

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    SharedPreferences sharedPref = getActivity().getSharedPreferences("WDWHIC settings", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt("lastPage", INTRO);
    editor.commit();

    View view = inflater.inflate(R.layout.fragment_intro, container, false);

    ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(R.string.intro_page_full);

    RelativeLayout topLayout = getActivity().findViewById(R.id.topLayout);
    topLayout.setVisibility(View.INVISIBLE);

    final Animation in = new AlphaAnimation(0.0f, 1.0f);
    in.setDuration(500);

    RelativeLayout rel_layout = view.findViewById(R.id.rel_layout);

    Resources res = getResources();

    pages = res.getStringArray(R.array.pages_array);

    heading = rel_layout.findViewById(R.id.heading);
    heading.setText(pages[pageNum]);

    forward = rel_layout.findViewById(R.id.forward);
    forward.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            back.setVisibility(View.VISIBLE);
            pageNum += 2;
            heading.setText(pages[pageNum]);
            body.startAnimation(in);
            body.setText(pages[pageNum + 1]);
            body.scrollTo(0, 0);
            if (pageNum == 2 * MAX_PAGES) forward.setVisibility(View.INVISIBLE);
        }
    });

    back = rel_layout.findViewById(R.id.back);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            forward.setVisibility(View.VISIBLE);
            pageNum -= 2;
            heading.setText(pages[pageNum]);
            body.startAnimation(in);
            body.setText(pages[pageNum + 1]);
            body.scrollTo(0, 0);
            if (pageNum == 0) back.setVisibility(View.INVISIBLE);
        }
    });

    skip = rel_layout.findViewById(R.id.skip_button);
    skip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditProfileFragment editProfileFragment = new EditProfileFragment();
            FragmentManager manager = getActivity().getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.fragment_container, editProfileFragment, editProfileFragment.getTag()).commit();
            getActivity().overridePendingTransition(R.anim.fadein, R.anim.fadeout);
        }
    });

    body = rel_layout.findViewById(R.id.body);
    body.startAnimation(in);
    body.setText(pages[pageNum + 1]);
    body.setMovementMethod(new ScrollingMovementMethod());

    return view;
}

如果在第一页上单击“ skip”按钮,则会调用其onClick()方法。但是,如果单击前进或后退,除非返回首页,否则不会显示。这是怎么回事?为什么前进的页面会有所作为?

android android-fragments imageview onclicklistener buttonclick
3个回答
0
投票
尝试将android:clickable="true"添加到图像视图中。

0
投票
我将尝试在扩展FragmentActivity的类中设置onClickListener功能。

0
投票
我不知道为什么这应该起作用,但是我将按钮移到了标题上方,没有进行任何其他更改,现在每个页面都调用了onClick()。
© www.soinside.com 2019 - 2024. All rights reserved.