调用listview的setAdapter方法时FAB位置发生变化

FAB position changes when listview's setAdapter method is called

我正在使用 this 库来实现 fab。 正如您在此 video 中看到的那样,当片段加载时,fab 位置正在改变。 调试了一下,发现是listview的"setAdapter()"方法导致了位置变化。 我不知道为什么会这样..

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
       android:id="@+id/toolbar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:theme="@style/AppTheme.ActionBar" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/toolbar">

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <LinearLayout
            android:id="@android:id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical"
            android:gravity="center">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/sad_face"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/no_leagues" />

        </LinearLayout>

    </FrameLayout>

    <com.melnykov.fab.FloatingActionButton
        android:id="@+id/fab_new_league"
        android:src="@drawable/ic_action_add"
        app:fab_colorNormal="@color/orange"
        app:fab_colorPressed="@color/redDark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignBottom="@id/toolbar"
        android:layout_marginBottom="-28dp"
        android:layout_marginRight="28dp"
        android:elevation="8dp"/>

</RelativeLayout>

我的片段

public class LeagueListFragment extends MyFragment {
    @InjectView(android.R.id.list)   ListView mListView;
    @InjectView(android.R.id.empty)  LinearLayout mEmpty;

    public static Fragment newInstance() {
        return new LeagueListFragment();
    }

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

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

        ButterKnife.inject(this, view);

        // Fetch league list and set up adapter
        List<League> list = League.getAll();
        mAdapter = new LeagueAdapter(getActivity(), list);

        // Set up ListView
        mListView.setEmptyView(mEmpty);
        mListView.setDivider(new ColorDrawable(getResources().getColor(R.color.gray)));
        mListView.setDividerHeight(2);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);

        return view;
    }
}

Lollipop 之前的运行时边距发生变化,因为设置边距时不得考虑阴影。 尝试通过以下方式禁用阴影:

fab:fab_shadow="false"

目前预计不会修复此问题。(参见 makovkastar answer