自定义工具栏重叠片段

时间:2016-11-10 23:20:50

标签: java android xml toolbar

所以基本上我试图设置一个片段,以便它显示我的徽章,由于某种原因它只是不适合屏幕并从顶部开始。这似乎导致工具栏浮动整个片段。有没有人能指出我做错了什么?我使用三个片段和一个标签系统。

Here is the screenshot

1)片段(myFragment.java)

public class myFragment extends Fragment {


public myFragment() {
    // Required empty public constructor
}
private RecyclerView recyclerView;
private badgeAdapter adapter;
private List<hatBadge> badge;

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

    final View view = inflater.inflate(R.layout.fragment_my_closet, container, false);
    final FragmentActivity c = getActivity();
    final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);

    badge = new ArrayList<>();

    adapter = new badgeAdapter(getContext(), badge);

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getContext(), 2);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.addItemDecoration(new myFragment.GridSpacingItemDecoration(2, dpToPx(10), true));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    new Thread(new Runnable() {
        @Override
        public void run() {
            c.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    recyclerView.setAdapter(adapter);

                }
            });
        }
    }).start();

    prepareAlbums();

    return view;
}
/**
 * Adding few albums for testing
 */
private void prepareAlbums() {
    int[] covers = new int[]{
            R.drawable.nike,
            R.drawable.nike,
            R.drawable.nike,
            R.drawable.nike};

    hatBadge a = new hatBadge("Hat 1", 13, covers[0]);
    badge.add(a);

    a = new hatBadge("Hat 1", 8, covers[1]);
    badge.add(a);

    a = new hatBadge("Hat 5", 11, covers[2]);
    badge.add(a);

    a = new hatBadge("Hat 5", 11, covers[2]);
    badge.add(a);


    adapter.notifyDataSetChanged();
}

/**
 * RecyclerView item decoration - give equal margin around grid item
 */
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int spanCount;
    private int spacing;
    private boolean includeEdge;

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        int column = position % spanCount; // item column

        if (includeEdge) {
            outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
            outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

            if (position < spanCount) { // top edge
                outRect.top = spacing;
            }
            outRect.bottom = spacing; // item bottom
        } else {
            outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
            outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
            if (position >= spanCount) {
                outRect.top = spacing; // item top
            }
        }
    }
}

/**
 * Converting dp to pixel
 */
private int dpToPx(int dp) {
    Resources r = getResources();
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}

}

2)活动运行Fragment(myActivity.java)

public class myActivity extends ActionBarActivity {

Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_tab);

    toolbar = (Toolbar)findViewById(R.id.toolBar);
    setSupportActionBar(toolbar);
    tabLayout = (TabLayout)findViewById(R.id.tabLayout);
    viewPager = (ViewPager)findViewById(R.id.viewPager);
    viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
    viewPagerAdapter.addFragments(new homeFragment(), "Info");
    viewPagerAdapter.addFragments(new myFragment(), "my Fragment");
    viewPagerAdapter.addFragments(new careFragment(), "Care");
    viewPager.setAdapter(viewPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);

}

3)fragment_my_closet.xml

<RelativeLayout 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"
android:background="@color/viewBg"
>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:clipToPadding="false" />

</RelativeLayout>

4)Activity_tab.xml

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

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:theme="@style/AppTheme"
        >
        <include
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            layout="@layout/toolbar_layout"
            />
        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tabLayout"
            app:tabMode="fixed"
            app:tabGravity="fill"
            ></android.support.design.widget.TabLayout>


    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPager"
        ></android.support.v4.view.ViewPager>
</RelativeLayout>

5)toolbar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"

android:fitsSystemWindows="true"
android:id="@+id/toolBar"
app:theme="@style/Theme.AppCompat.Light.NoActionBar">

</android.support.v7.widget.Toolbar>

1 个答案:

答案 0 :(得分:1)

AppBarLayout取决于成为CoordinatorLayout的孩子。它还需要一个实现ScrollingViewBehavior的滚动视图。您可以通过设置app:layout_behavior="@string/appbar_scrolling_view_behavior"来使用标准版本。

<android.support.design.widget.CoordinatorLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:theme="@style/AppTheme"
        >
        <include
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            layout="@layout/toolbar_layout"
            />
        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tabLayout"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>