博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CoordinatorLayout、AppBarLayout实现上滑隐藏图片,下滑显示图片
阅读量:6699 次
发布时间:2019-06-25

本文共 4870 字,大约阅读时间需要 16 分钟。

仿keep个人中心界面,实现上滑隐藏图片,下滑显示图片

activity_mode.xml

复制代码

ModeActivity.java

package com.zhoujian.mykeep.activity;import android.graphics.Color;import android.os.Bundle;import android.support.design.widget.AppBarLayout;import android.support.design.widget.CollapsingToolbarLayout;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.DefaultItemAnimator;import android.support.v7.widget.DividerItemDecoration;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.support.v7.widget.Toolbar;import android.util.Log;import android.view.MenuItem;import android.view.View;import android.widget.Toast;import com.zhoujian.mykeep.R;import com.zhoujian.mykeep.adapter.MainAdapter;import com.zhoujian.mykeep.bean.Person;import java.util.ArrayList;/** * Created by zhoujian on 2017/3/14. */public class ModeActivity extends AppCompatActivity{    public static final String TAG = "ModeActivity";    private RecyclerView mRecyclerView;    private ArrayList
personList; private MainAdapter adapter; @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mode); initData(); initView(); } private void initData() { personList = new ArrayList
(); Person mPerson0= new Person("林黛玉"); personList.add(mPerson0); Person mPerson1= new Person("薛宝钗"); personList.add(mPerson1); Person mPerson2= new Person("贾元春"); personList.add(mPerson2); Person mPerson3= new Person("贾迎春"); personList.add(mPerson3); Person mPerson4= new Person("贾探春"); personList.add(mPerson4); Person mPerson5= new Person("贾惜春"); personList.add(mPerson5); Person mPerson6= new Person("史湘云"); personList.add(mPerson6); Person mPerson7= new Person("王熙凤"); personList.add(mPerson7); Person mPerson8= new Person("秦可卿"); personList.add(mPerson8); Person mPerson9= new Person("贾宝玉"); personList.add(mPerson9); } private void initView() { final Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); toolbar.setTitleTextColor(Color.TRANSPARENT); toolbar.inflateMenu(R.menu.menu_search); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()){ case R.id.item_search: //点击设置 Toast.makeText(ModeActivity.this, "按钮被点击了", Toast.LENGTH_SHORT).show(); break; } return false; } }); AppBarLayout app_bar_layout = (AppBarLayout)findViewById(R.id.app_bar_layout); final CollapsingToolbarLayout collapsing_toolbar_layout = (CollapsingToolbarLayout)findViewById(R.id.collapsing_toolbar_layout); collapsing_toolbar_layout.setTitle(""); collapsing_toolbar_layout.setCollapsedTitleTextColor(getResources().getColor(R.color.white)); collapsing_toolbar_layout.setExpandedTitleColor(getResources().getColor(R.color.white)); collapsing_toolbar_layout.setExpandedTitleColor(Color.TRANSPARENT); app_bar_layout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { Log.e(TAG,"appBarLayoutHeight:"+appBarLayout.getHeight()+" getTotalScrollRange:"+appBarLayout.getTotalScrollRange()+" offSet:"+verticalOffset); if(Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()){ toolbar.setTitleTextColor(getResources().getColor(R.color.white)); collapsing_toolbar_layout.setTitle("keep"); }else{ collapsing_toolbar_layout.setTitle(""); } } }); mRecyclerView =(RecyclerView) findViewById(R.id.id_recyclerview); adapter = new MainAdapter(ModeActivity.this, personList); mRecyclerView.setLayoutManager(new LinearLayoutManager(ModeActivity.this)); mRecyclerView.addItemDecoration(new DividerItemDecoration(ModeActivity.this,DividerItemDecoration.VERTICAL)); // 设置item动画 mRecyclerView.setItemAnimator(new DefaultItemAnimator()); mRecyclerView.setAdapter(adapter); }}复制代码

实现效果

这里写图片描述

源码下载

转载地址:http://pcwlo.baihongyu.com/

你可能感兴趣的文章
Icon and Image Sizes
查看>>
[LeetCode]151.Reverse Words in a String
查看>>
ZSH 终级Shell
查看>>
【JavaScript】document对象_Cookie属性
查看>>
OSX定时执行脚本
查看>>
基于注解的spring MVC程序
查看>>
接口请求身份认证的Token和RefreshToken的解决方案
查看>>
React-Native环境搭建(windows)
查看>>
[译]带你揭开Kotlin中属性代理和懒加载语法糖衣
查看>>
2019值得关注的几个技术类公众号!
查看>>
OC内存管理--引用计数器
查看>>
Android 屏幕适配
查看>>
[译] 介绍 Google Play 上新的优质 Android 应用与游戏
查看>>
Jenkins 100次构建失败踩坑全录(二)
查看>>
Python每日一练0002
查看>>
scala笔记
查看>>
拍拍贷消息系统原理与应用
查看>>
android平台下OpenGL ES 3.0从零开始
查看>>
优秀的年轻人都有哪些特征?
查看>>
视频直播之优化移动端WebRTC通信
查看>>