自定义工具栏重叠片段

问题描述:

所以基本上我试图设置一个片段,以便它显示我的徽章,并出于某种原因,它只是不适合屏幕,并从顶部开始。看起来这会导致工具栏在整个片段上浮动。有什么我做错了,任何人都可以指出?我使用三个片段与tabbing系统。自定义工具栏重叠片段

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)活动运行片段(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> 
+0

你可以添加屏幕截图到帖子中吗? –

+0

我已经添加了屏幕截图,它发生在我从屏幕顶部开始的所有3个片段中,而不仅仅是在工具栏下作为起点。 – TheCryptKeeper

+0

那'新的线程'真的没有必要,顺便说一下 –

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> 
+0

问题在Activity_tab.xml中是。需要更改为LinearLayout – TheCryptKeeper