android滑动toolbar 很炫的标题栏

作为一名程序员,要不断的学习新的技术。不断汲取新的知识。

CoordinatorLayout 坐标局
AppBarLayout 用于替换早起的actionbar 
CollapsingToolbarLayout 可折叠的toolbar 
Toolbar   标题栏
通过以上组件 写一个简单的可折叠的标题栏  先看三张图片
通过这个三张图可以看出来,标题栏的大小会随着recyclerview 的滑动伸张折叠
下面简单说下 是如何实现的
先上布局文件



    
    
        
    
        
        
        
        

    
    
    
    



布局文件也没什么好说的了  里面都有注释 。
直接上代码:
package com.lefeng.lfvideo.myndk;

import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import com.lefeng.lfvideo.myndk.adapter.RecylerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CYL on 2016/3/23.
 * email:[email protected]
 */
public class CollapingActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private RecyclerView recyclerView;
    private List dataSource;
    private RecylerAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.applayout);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collap_layout);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
	//这个主意下   这个activity的theme 设置为noactionbar
	//设置actionbar 为toolbar 
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	//这里设置标题的时候需要在collapsingtoolbarlayout上设置  否则没有效果
        collapsingToolbarLayout.setTitle("collapsingToolbarLayout");

        initData();
        adapter = new RecylerAdapter(dataSource,getApplicationContext());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    private void initData(){
        dataSource = new ArrayList<>();
        for (int i = 0;i<100;i++){
            dataSource.add("recylerview");
        }

    }
}
里面还有个recylerview 的适配器贴出啦 其实很简单和 listview 的适配器差不多
 
  
package com.lefeng.lfvideo.myndk.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lefeng.lfvideo.myndk.R;
import com.lefeng.lfvideo.myndk.utils.Sp2Px;

import java.util.List;

/**
 * Created by CYL on 2016/3/22.
 * email:[email protected]
 */
public class RecylerAdapter extends RecyclerView.Adapter {
    private List dataSource;
    private LayoutInflater mInflater;
    private Context context;

    public RecylerAdapter(List dataSource,Context context) {
        this.dataSource = dataSource;
        mInflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder viewHolder = new MyViewHolder(mInflater.inflate(R.layout.recycler_item,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        ViewGroup.LayoutParams params = holder.container.getLayoutParams();
        params.height = (int) Sp2Px.sp2px(mInflater.getContext(), (float) (100+(150*Math.random())));
        holder.container.setLayoutParams(params);
        holder.tv.setText(dataSource.get(position));
        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(position%2==0){
                    removeItem(position);
                }
                else{
                    addItem();
                }
            }
        });
    }
    public final class MyViewHolder extends  RecyclerView.ViewHolder{
        private TextView tv ;
        private LinearLayout container;
        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.t1);
            container = (LinearLayout) itemView.findViewById(R.id.main_container);
        }
    }
    @Override
    public int getItemCount() {
        return dataSource.size();
    }

    /**
     * 给recyclerview 添加一个条目
     */
    public void addItem(){
        dataSource.add("hello recyler");
        notifyDataSetChanged();
    }

    /**
     * 根据指定的位置删除一个条目
     * @param postion
     */
    public void removeItem(int postion){
        dataSource.remove(postion);
        notifyDataSetChanged();
    }
}

每个条目的布局文件就不贴出来了,就一个简单的textview
这样就可以出来一个比较绚丽的可折叠的标题栏了 。。。。





 
  

你可能感兴趣的:(UI)