Android技术知识手机移动程序开发

在Eclipse IDE下实现下滑隐藏ToolBar

2017-01-16  本文已影响144人  霡霂976447044
由于只有2个G运存,Ubuntu16.04系统,AndroidStudio运行不起来,只能用Eclipse了,记录下笔记

滑动中的效果图

第一步,导入扩展包,创建应用

第二步 修改MainActivity和清单文件

public class MainActivity extends AppCompatActivity
android:theme="@style/MyAppTheme"
该自定义主题在values->styles里定义,在跟标签resources下添加:
<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
 </style>
这里自定义了一个style,继承了一个没有ActionBar的style,目的是为了隐藏ActionBar,
用Toolbar替代
 涉及到的颜色定义在values->color.xml里,该文件定义如下:
 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#1e88e5</color>
    <color name="colorPrimaryDark">#536dfe</color>

    <color name="white">#ffffff</color>
    <color name="red">#b71c1c</color>
    <color name="black">#000000</color>
     <color name="gray">#33000000</color>
</resources>

第三步,修改activity_layout.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:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <android.support.design.widget.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"
            app:title="Hello"
            app:layout_scrollFlags="scroll|enterAlways"
            />

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" 
            android:id="@+id/tablayout"
            app:tabIndicatorColor="@color/white"
            app:tabSelectedTextColor="@color/white"
            app:tabTextColor="@color/black"
            android:background="#1565c0"
            >
        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.view.ViewPager
        app:layout_behavior="@string/appbar_scrolling_view_behavior" 
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        
    </android.support.v4.view.ViewPager>

</android.support.design.widget.CoordinatorLayout>

首先我的ViewPager里填充了RecyclerView,它支持app:layout_behavior属性

A navigation button.
一个导航按钮

A branded logo image.
一个Logo图标

One or more custom views.
一个或者多个自定义view
An action menu.
一个菜单

第四步,修改MainActivity找到控件初始化控件

public class MainActivity extends AppCompatActivity {
    private TabLayout tablayout;
    private ViewPager viewpager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        tablayout = (TabLayout) findViewById(R.id.tablayout);
        viewpager = (ViewPager) findViewById(R.id.viewpager);//viewpager里填充3个fragment
        initView();
    }

    private void initView() {
        ArrayList<Fragment> data = new ArrayList<Fragment>();//定义list装载fragment
        BaseFragment fragmentA = new BaseFragment();//第一个fragment
        BaseFragment fragmentB = new BaseFragment();
        BaseFragment fragmentC = new BaseFragment();
        data.add(fragmentA);
        data.add(fragmentB);
        data.add(fragmentC);
        
        FragmentManager fm = getSupportFragmentManager();
        MyAdapter adapter = new MyAdapter(data,fm);
        viewpager.setAdapter(adapter);
        
        tablayout.setupWithViewPager(viewpager);//把viewpager和tablayout关联
        tablayout.getTabAt(0).setText("A");
        tablayout.getTabAt(1).setText("B");
        tablayout.getTabAt(2).setText("C");
    }
    //一个FragmentPagerAdapter,每个ViewPager页面都是Fragment
    class MyAdapter extends FragmentPagerAdapter{

        ArrayList<Fragment> data;
        public MyAdapter(ArrayList<Fragment> data,FragmentManager fm) {
            super(fm);
            this.data = data;
            Log.i("data size", data.size()+"");
        }
  

        @Override
        public Fragment getItem(int pos) {
            return data.get(pos);
        }

        @Override
        public int getCount() {
            return data.size();
        }
    }

BaseFragment代码

public class BaseFragment extends Fragment {
    private RecyclerView mRecyclerView;
    public BaseFragment() {
    }

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_fragment, null);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler);
        initRecyclerView();
        return view;
    }
    
    private void initRecyclerView() {
        String[] data = new String[]{"a","sss","aaa","dsfsdf","sdfsdfsd","dfsdfsf",
                "a","sss","aaa","dsfsdf","sdfsdfsd","dfsdfsf",
                "a","sss","aaa","dsfsdf","sdfsdfsd","dfsdfsf",
                "a","sss","aaa","dsfsdf","sdfsdfsd","dfsdfsf",
                "a","sss","aaa","dsfsdf","sdfsdfsd","dfsdfsf"
        };
        RecyclerAdapter adapter = new RecyclerAdapter(data);//创建RecyclerAdapter对象
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());//
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);//为RecyclerView设置布局管理器
        mRecyclerView.setAdapter(adapter);
    }
}

layout_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:orientation="vertical" >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        >
    </android.support.v7.widget.RecyclerView>
    
</LinearLayout>

RecyclerAdapter 代码

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
    
    private String[] data = null;//传经来的要显示数据
    
    public RecyclerAdapter(String[] data) {
        this.data = data;
    }
    
    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_data;
        public ViewHolder(View view) {
            super(view);
            //找到控件id
            tv_data = (TextView) view.findViewById(R.id.tv_data);
        }
    }

    @Override
    public int getItemCount() {
        Log.e("data count", data.length+"");
        return data.length;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //根据传入的holder,为viewholder设置数据
        String str = data[position];
        holder.tv_data.setText(str);
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int position) {
        //返回一个viewholder,创建一个布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recycler_item, parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        
        // 在此处注册点击事件,和Listview不同
        view.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                Toast.makeText(parent.getContext(), "click", Toast.LENGTH_SHORT).show();
            }
        });
        return viewHolder;
    }
}

涉及到的layout_recycler_item代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_reclcler"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/tv_data"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:gravity="center"
        android:text="data"
        android:textSize="20sp"
        android:textColor="#000"
        />
    
</LinearLayout>
上一篇 下一篇

猜你喜欢

热点阅读