2015-10-19 18 views
1

我试图使用Google的新功能来创建更自然的滚动和隐藏工具栏,以创建应用程序,例如带隐藏应用程序栏的Google Play商店应用程序滚动,以及不会在滚动时隐藏的Tab栏。如何隐藏和显示具有CoordinatorLayout和LinearLayout的应用程序栏,而不是recyclerview

古怪不过,所有的解释和指南,我只能找到对所有做这个标签的回收意见,所以

你可以有相同类型的应用程序栏隐藏和显示,通过滚动触发的,但为可滚动的线性布局元素?

短版:如何让coordinatorlayout &应用程序栏隐藏顺利通过简单的LinearLayout在标签滚动

感谢时!

activity_part_three.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/coordinatorLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
     android:id="@+id/appBarLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" /> 
    <android.support.design.widget.TabLayout 
      android:id="@+id/tabLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabTextColor="@android:color/white" 
      app:tabSelectedTextColor="@android:color/white" 
      app:tabIndicatorColor="@android:color/white" 
      app:tabIndicatorHeight="6dp"/> 
</android.support.design.widget.AppBarLayout> 
<android.support.v4.view.ViewPager 
     android:id="@+id/viewPager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

<android.support.design.widget.FloatingActionButton 
     android:id="@+id/fabButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_favorite_outline_white_24dp" 
     android:onClick="fabAction" 
     app:borderWidth="0dp" 
     app:layout_behavior="pl.michalz.hideonscrollexample.ScrollingFABBehavior"/> 
</android.support.design.widget.CoordinatorLayout> 

PartThreeActivity.java

public class PartThreeActivity extends AppCompatActivity { 

    public Toolbar mToolbar; 
    public AppBarLayout appBarLayout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     setTheme(R.style.AppThemeBlue); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_part_three); 

     initToolbar(); 
     initViewPagerAndTabs(); 
    } 

    private void initToolbar() { 
     mToolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(mToolbar); 
     setTitle(getString(R.string.app_name)); 
     mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white)); 

     appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout); 
    } 

    private void initViewPagerAndTabs() { 
     ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); 
     PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager()); 
     pagerAdapter.addFragment(PartThreeFragment.createInstance(20), getString(R.string.tab_1)); 
     pagerAdapter.addFragment(PartThreeFragment.createInstance(4), getString(R.string.tab_2)); 
     pagerAdapter.addFragment(HomeFragment.newInstance(), "HOME"); 
     viewPager.setAdapter(pagerAdapter); 
     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout); 
     tabLayout.setupWithViewPager(viewPager); 


    } 

    public void fabAction(View view) { 
     System.out.println("allo"); 
     appBarLayout.setExpanded(false); 
    } 

    static class PagerAdapter extends FragmentPagerAdapter { 

     private final List<Fragment> fragmentList = new ArrayList<>(); 
     private final List<String> fragmentTitleList = new ArrayList<>(); 

     public PagerAdapter(FragmentManager fragmentManager) { 
      super(fragmentManager); 
     } 

     public void addFragment(Fragment fragment, String title) { 
      fragmentList.add(fragment); 
      fragmentTitleList.add(title); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      return fragmentList.get(position); 
     } 

     @Override 
     public int getCount() { 
      return fragmentList.size(); 
     } 
     @Override 
     public CharSequence getPageTitle(int position) { 
      return fragmentTitleList.get(position); 
     } 
    } 
} 

fragment_home.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="pl.michalz.hideonscrollexample.HomeFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <TextView android:layout_width="match_parent" android:layout_height="match_parent" 
       android:text="@string/hello_blank_fragment" /> 

     </LinearLayout> 
    </ScrollView> 

</FrameLayout> 

HomeFragment.java

public class HomeFragment extends Fragment { 


public HomeFragment() { 
    // Required empty public constructor 
} 

public static HomeFragment newInstance() { 
    HomeFragment homeFragment = new HomeFragment(); 
    Bundle bundle = new Bundle(); 
    homeFragment.setArguments(bundle); 
    return homeFragment; 
} 



@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    return inflater.inflate(R.layout.fragment_home, container, false); 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
} 

}

PartThreeFragment.java

public class PartThreeFragment extends Fragment { 

    public final static String ITEMS_COUNT_KEY = "PartThreeFragment$ItemsCount"; 

    public static PartThreeFragment createInstance(int itemsCount) { 
     PartThreeFragment partThreeFragment = new PartThreeFragment(); 
     Bundle bundle = new Bundle(); 
     bundle.putInt(ITEMS_COUNT_KEY, itemsCount); 
     partThreeFragment.setArguments(bundle); 
     return partThreeFragment; 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     RecyclerView recyclerView = (RecyclerView) inflater.inflate(
       R.layout.fragment_part_three, container, false); 
     setupRecyclerView(recyclerView); 
     return recyclerView; 
    } 

    private void setupRecyclerView(RecyclerView recyclerView) { 
     recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
     RecyclerAdapter recyclerAdapter = new RecyclerAdapter(createItemList()); 
     recyclerView.setAdapter(recyclerAdapter); 
    } 

    private List<String> createItemList() { 
     List<String> itemList = new ArrayList<>(); 
     Bundle bundle = getArguments(); 
     if(bundle!=null) { 
      int itemsCount = bundle.getInt(ITEMS_COUNT_KEY); 
      for (int i = 0; i < itemsCount; i++) { 
       itemList.add("Item " + i); 
      } 
     } 
     return itemList; 
    } 
} 

fragment_part_three.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" 
              android:id="@+id/recyclerView" 
              android:layout_width="match_parent" 
              android:layout_height="match_parent"/> 

回答

1

ScrollView将不会与CoordinatorLayout关联。使用嵌套滚动查看。

<<android.support.v4.widget.NestedScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView android:layout_width="match_parent" android:layout_height="match_parent" 
      android:text="@string/hello_blank_fragment" /> 

    </LinearLayout> 
</<android.support.v4.widget.NestedScrollView> 
+0

如何栏背背PROGRAMM? – Artem

+0

我不明白你的问题。当我滚动RecycleView向下工具栏隐藏(与动画)时, – WonderKid

+0

。如何以编程方式退出工具栏? – Artem

2

如果使用Listview,只有当API> 21可以模拟的RecyclerView相同的隐藏效果。

尝试用ListView.setNestedScrollingEnabled(true);

相关问题