【问题标题】:Why is my floating action button not responding to clicks?为什么我的浮动操作按钮对点击没有响应?
【发布时间】:2021-08-12 00:23:47
【问题描述】:

我在一个由 ViewPager 创建的片段中有两个浮动操作按钮。我的目的是让 FAB 做与 ViewPager 相同的事情,以便用户可以在导航方面进行选择。我的问题是 FAB 最初不响应点击,所以如果我单击左侧 FAB,然后单击右侧 FAB,ViewPager 将向左翻转而不是向右翻转。我做错了什么?

这是我的代码:

activity_view_pager:

<?xml version="1.0" encoding="utf-8"?>
<androidx.viewpager.widget.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

片段测试:

<?xml version="1.0" encoding="utf-8"?>
<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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Title"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/a"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:text="Description"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/b"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="250dp"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/c"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/left_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginTop="450dp"
        android:src="@drawable/ic_left_arrow"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/right_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="450dp"
        android:layout_marginEnd="50dp"
        android:src="@drawable/ic_right_arrow"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

ViewPagerActivity:

package com.fl4me.android.exampleapplication;

import static com.fl4me.android.exampleapplication.TestAdapter.EXTRA_ID;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class ViewPagerActivity extends AppCompatActivity {

    public static int index;
    public static ViewPager viewPager;

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

        index = (int) getIntent().getSerializableExtra(EXTRA_ID);

        viewPager = (ViewPager) findViewById(R.id.view_pager);

        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
            @Override
            public Fragment getItem(int position) {
                return TestFragment.newInstance(position);
            }

            @Override
            public int getCount() {
                return 5;
            }
        });

        for(int i = 0; i < Test.testData.length; i++) {
            if(i == index) {
                viewPager.setCurrentItem(i);
                break;
            }
        }
    }
}

测试片段:

package com.fl4me.android.exampleapplication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class TestFragment extends Fragment {

    private static final String ARG_ID = "Id";
    private int Id;
    private String a;
    private TextView aTV;
    private String b;
    private TextView bTV;
    private int c;
    private TextView cTV;
    private FloatingActionButton leftBtn;
    private FloatingActionButton rightBtn;

    public static TestFragment newInstance(int Id) {
        Bundle args = new Bundle();
        args.putSerializable(ARG_ID, Id);

        TestFragment fragment = new TestFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_test, container, false);

        Id = getArguments().getInt(ARG_ID);
        a = Test.testData[Id].getA();
        b = Test.testData[Id].getB();
        c = Test.testData[Id].getC();

        aTV = (TextView) view.findViewById(R.id.a);
        aTV.setText(a);

        bTV = (TextView) view.findViewById(R.id.b);
        bTV.setText(b);

        cTV = (TextView) view.findViewById(R.id.c);
        cTV.setText(String.valueOf(c));

        leftBtn = view.findViewById(R.id.left_btn);
        rightBtn = view.findViewById(R.id.right_btn);

        leftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ViewPagerActivity.viewPager.setCurrentItem(ViewPagerActivity.index--);
            }
        });

        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ViewPagerActivity.viewPager.setCurrentItem(ViewPagerActivity.index++);
            }
        });

        return view;
    }
}

【问题讨论】:

    标签: android-fragments android-viewpager floating-action-button


    【解决方案1】:

    这可能是因为您正在对index 进行后递增或后递减,而不是对其进行预递增或预递减。我认为你应该知道 post 和 pre 递增是如何工作的,但是 here 值得一读。

    基本上,您为setCurrentItem 提供的index 不会在现场评估为index + 1 或index - 1,但只有在执行包含是表达式index++ 或index--。这就是为什么您在切换您所描述的页面时出现“滞后”的原因。

    因此,在rightBtn 的侦听器中,代码可能应该是这样的:

    ViewPagerActivity.viewPager.setCurrentItem(++ViewPagerActivity.index);
    

    leftBtn 的侦听器将与此类似,但使用的是 --。

    【讨论】:

    • 哦,这很有道理。我确实知道增量前和增量后是如何工作的,但我从来没有这么多地使用过它们,所以我还没有真正体验过它们。我在想也许我的 ViewPager 以某种方式拦截了点击或其他东西,但我没想到修复这么简单。非常感谢您回答这个问题和我之前的问题。我真的很感激!
    • 嘿,很抱歉再次提出这个问题,但我的按钮有点麻烦。当用户在 ViewPager 的第一个和最后一个项目上时,我正在尝试让按钮变得不可见,但它无法正常工作。我有一个用 TestFragment 编写的单独方法,当索引为零时,左按钮不可见,而当索引是数组的最后一个元素时,右按钮不可见。然后我在找到左右按钮的视图并在两个按钮的onClick 方法中设置 ViewPager 之后调用此方法。有什么想法吗?
    • 再一次,您需要发布代码以供某人解决问题,因此您可能需要为此创建一个单独的问题。
    • 好的,我发布了一个新问题。我不想针对看似很小的问题提出新问题,但我想人们需要查看代码才能帮助解决问题。
    猜你喜欢
    • 2020-04-11
    • 2013-03-21
    • 2016-06-23
    • 2016-06-24
    • 1970-01-01
    • 2019-06-18
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多