安卓开源框架特效Android Class

五分钟学会 Material Design之实现上滑隐藏Tool

2016-10-30  本文已影响7010人  ifjgm

一:目的效果

二:你需要了解的概念

1. **CoordinatorLayout **
2.本例的目的

三 .要实现滚动必须具备以下条件

  1. 最外层 CoordinatorLayout作为父控件,必须是这个牛叉的控件
  2. 给因滑动而出现或者消失的组件设置 app:layout_scrollFlags=”scroll|enterAlways” 属性
  3. 给滑动的组件设置app:layout_behavior属性

【注意:】这里使用的Behavior 是@string/appbar_scrolling_view_behavior**),所以需要滚动的组件必须为其子view,才能实现滚动。

四. 实现过程

1 . 布局文件(主界面布局

<?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-
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zhang.testing.newtechnique.MainActivity">

<android.support.design.widget.AppBarLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:fitsSystemWindows="true">
  <android.support.v7.widget.Toolbar   
    android:id="@+id/tool_bar"   
    android:layout_width="match_parent"    
    android:layout_height="?attr/actionBarSize"  
    android:background="#ff0000"    
    app:layout_scrollFlags="scroll|enterAlways">
  </android.support.v7.widget.Toolbar>

  <android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#30469b"
    app:tabGravity="center" 
    app:tabMode="scrollable"
    app:tabSelectedTextColor="#ffffff"
    app:tabTextColor="#ff0000">
</android.support.design.widget.TabLayout>
 </android.support.design.widget.AppBarLayout>
 </android.support.design.widget.AppBarLayout>
<RelativeLayout   
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior">
     <android.support.v4.view.ViewPager
         android:id="@+id/view_page"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     </android.support.v4.view.ViewPager>
  </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

viewpage中放Fragment ,Fragment切换用TabLayout 的标签作为指示器 每个Fragment 的布局为RecyclerView
这里对控件的属性做下说明

【注意:】这里我们给ToolBar添加了layout_scrollFlags属性,但没有给TabLayout设置该属性。如果TabLayout在ToolBar的上方,也是无法滚动的,因为TabLayout是固定的,它把ToolBar挡住了,

2 . Fragment 的布局文件

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

由于RecyclerView 内仅仅绑定了一个TextView非常简单,这里就不贴代码了

3 . MainActivity代码(本文使用ButterKnife

public class MainActivity extends AppCompatActivity {
  @BindView(R.id.hello_world)TextView hello_world;
  @BindView(R.id.activity_main)CoordinatorLayout coordinatorLayout;
  @BindView(R.id.tool_bar)Toolbar toolbar;
  @BindView(R.id.view_page)ViewPager myViewPageer;
  @BindView(R.id.tab_layout)TabLayout tab_layout;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     ButterKnife.bind(this);   
     initUiAndData();
  }

  /** *
   初始化话界面和数据
   */
  private void initUiAndData() {
      toolbar.setTitleTextColor(Color.WHITE);
      setSupportActionBar(toolbar);
      //初始化ViewPager的 Aapter 代码会在后面贴
      MyPageViewerAdapter adapter = new MyPageViewerAdapter(getSupportFragmentManager());
      //为Adapter添加Aapter和标题
      adapter.addFragment(new MyFragment(),"zhangsan");
      adapter.addFragment(new MyFragment(),"lisi");
      adapter.addFragment(new MyFragment(),"wanger");
      //为ViewPager绑定Adapter
      myViewPageer.setAdapter(adapter);
      //为TabLayout添加标签,注意这里我们传入了标签名称,但demo运行时显示的标签名称并不是我们添加的,那么为什么呢?卖个官子...
      tab_layout.addTab(tab_layout.newTab().setText("one_"));
      tab_layout.addTab(tab_layout.newTab().setText("two_"));
      tab_layout.addTab(tab_layout.newTab().setText("three_"));
      tab_layout.addTab(tab_layout.newTab().setText("three_"));
      //给tabLayout设置ViewPage,如果设置关联了Viewpage,那么ViewpagAdapter中getPageTitle返回的就是Tab上标题(上面疑问的回答)
      //为ViewPager 和Tablelayout进行绑定,从而实现滑动标签切换Fragment的目的
      tab_layout.setupWithViewPager(myViewPageer);
  }

4 . MyViewPagerAdaper的代码也贴下吧

public class MyViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragments = new ArrayList<>();
    private final List<String> fragmentTitles = new ArrayList<>();
    public MyViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    public void addFragment(Fragment fragment,String title){
        mFragments.add(fragment);
        fragmentTitles.add(title);
    }
    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }
    @Override
    public int getCount() {
        return mFragments.size();
    }
    @Override
    public CharSequence getPageTitle(int position) {
         //这里返回的标题就是TabLayout的标题
        return fragmentTitles.get(position);
    }
}

5 . MyFragment的代码

public class MyFragment extends Fragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_, container, false);
        ButterKnife.bind(this, view);
        initUIAndData();
        return view;
    }
    private void initUIAndData() {
        RecyclerAdapter adapter = new RecyclerAdapter(getActivity().getApplicationContext());        
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);
    }
}

6 . RecycerView 的Adaper RecyclerAdapter的代码

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Myholder> {
    private Context mContext;
    private String[] strs = new String[100];
    public RecyclerAdapter(Context context) {
        this.mContext = context;
        //为测试给Recycler添加数据
        for (int i = 0; i < 100; i++) {
            strs[i] = i + "";
        }
    }
    //这里返回一个ViewHolder
    @Override
    public RecyclerAdapter.Myholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item, null);
        Myholder myholder = new Myholder(view);
        return myholder;
    }
    //为ViewHolder中的布局绑定数据
    @Override
    public void onBindViewHolder(Myholder holder, int position) {
        holder.textView.setText(strs[position]);
    }
    @Override
    public int getItemCount() {
        return strs.length;
    }
    static class Myholder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_text)
        TextView textView;
        public Myholder(View itemView) {
            super(itemView);
            //ButterKnife也可以用于ViewHoder中
            ButterKnife.bind(this, itemView);
        }
    }
}

五 :OK搞定了,这么详细还没看懂??,可耻的隐了吧

福利福利
上一篇 下一篇

猜你喜欢

热点阅读