渐变toolbar,悬浮控件,scrollview和recycleview滑动冲突及显示不全问题解决

最近项目有这个需求,贴出了大家一起看看。
渐变toolbar和悬浮控件的原理都很简单,渐变toolbar是根据scrollview的y轴偏移量控制toolbar的背景透明度(有字体透明度需求的原理相同);
悬浮控件原理是把一个共享view从一个容器放到另一个容器;
滑动冲突的解决方案就是在recyclerview外包一层relativelayout,解决显示不全的问题;layoutmanager重写canScrollVertically 返回false,
解决滑动卡顿的问题,原理不知道。。。我这能用。

下载地址:https://github.com/274800562/transformToolbar.git



(第一次用这玩意,有点寒碜。)

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">


    <com.example.self.MyScrollView
        android:id="@+id/myscrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/iv_pic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                android:src="@mipmap/pic" />

            <LinearLayout
                android:id="@+id/ll_inner"
                android:layout_width="match_parent"
                android:layout_height="40dip"
                android:focusableInTouchMode="false"
                android:orientation="vertical">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerview1"
                    android:background="#eff0f4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                </android.support.v7.widget.RecyclerView>
            </LinearLayout>
            <!--recycleview显示完整-->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </RelativeLayout>
        </LinearLayout>
    </com.example.self.MyScrollView>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@color/colorPrimary"
        android:fitsSystemWindows="true"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="我是麦迪"
            android:textColor="@color/color_white"
            android:textSize="18sp" />
    </android.support.v7.widget.Toolbar>

    <LinearLayout
        android:id="@+id/ll_outer"
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:layout_marginTop="70dp"
        android:orientation="vertical" />

</RelativeLayout>


主要代码

MainActivity:

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
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 android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity implements MyScrollView.OnScrollChangedListener {

    private Toolbar toolbar;
    private TextView tvTitle;
    private MyScrollView myScrollView;
    private ImageView ivPic;
    private LinearLayout llInner, llOuter;
    private RecyclerView recyclerView;
    private RecyclerView recyclerView1;

    private int searchLayoutTop;
    String[] strings = {"麦迪", "哈达威", "奥拉朱旺", "乔丹", "欧文",
            "科比", "艾佛森", "囧墙", "韦少", "大帝",
            "席梦思", "皮尔斯", "阿伦", "卡哇伊", "大鸟",
            "伯伦", "保罗", "罗宾逊", "罗德曼", "尤因"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
        initToolbar();
        initData();
    }

    private void initView() {

        toolbar = (Toolbar) findViewById(R.id.toolbar);

        tvTitle = (TextView) findViewById(R.id.title);

        myScrollView = (MyScrollView) findViewById(R.id.myscrollview);

        ivPic = (ImageView) findViewById(R.id.iv_pic);


        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView1 = (RecyclerView) findViewById(R.id.recyclerview1);

        llOuter = (LinearLayout) findViewById(R.id.ll_outer);

        llInner = (LinearLayout) findViewById(R.id.ll_inner);
    }

    private void initListener() {
        myScrollView.setOnScrollChangedListener(this);
    }

    private void initToolbar() {
        toolbar.setTitle("");
        toolbar.getBackground().setAlpha(0);  //先设置透明
        tvTitle.setTextColor(Color.argb(0, 255, 255, 255));
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            //设为 false
            actionBar.setDisplayShowTitleEnabled(true);  //是否隐藏标题
            actionBar.setDisplayHomeAsUpEnabled(true);     //是否显示返回按钮
        }
        //实现透明状态栏效果  并且toolbar 需要设置  android:fitsSystemWindows="true"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
            layoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | layoutParams.flags);
        }
    }

    private void initData() {
        final List<String> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add(strings[i]);
        }
        MyGridLayoutManager manager = new MyGridLayoutManager(this, 2);
        //防止卡顿
        manager.setScrollEnabled(false);
        MyAdapter adapter = new MyAdapter(this, list);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(manager);
        adapter.setmOnItemClickListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Toast.makeText(MainActivity.this, list.get(position), Toast.LENGTH_LONG).show();
            }
        });


        final List<String> list1 = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list1.add("标签" + i);
        }
        MyLinearLayoutManager manager1 = new MyLinearLayoutManager(this);
        manager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        //防止卡顿
        manager1.setScrollEnabled(false);
        TagAdapter adapter1 = new TagAdapter(this, list1);
        recyclerView1.setAdapter(adapter1);
        recyclerView1.setLayoutManager(manager1);
        adapter1.setmOnItemClickListener(new TagAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Toast.makeText(MainActivity.this, list1.get(position), Toast.LENGTH_LONG).show();
            }
        });

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            /**
             * 70是toolbar的高度
             * 悬浮位置在toolbar下边,不减的话在顶部
             */
            searchLayoutTop = ivPic.getBottom() - dp2px(70);//获取searchLayout的顶部位置
        }
    }

    @Override
    public void onScrollChanged(ScrollView who, int width, int t, int oldWidth, int oldHeight) {
        float height = ivPic.getHeight();  //获取图片的高度
        /**
         * toolbar 渐变背景 偏移量控制
         */
        if (oldHeight < height) {
            int i = Float.valueOf(oldHeight / height * 255).intValue();    //i 有可能小于 0
            toolbar.getBackground().setAlpha(Math.max(i, 0));   // 0~255 透明度
            /**
             * 字体颜色透明度控制
             */
            tvTitle.setTextColor(Color.argb(i, 255, 255, 255));
        } else {
            toolbar.getBackground().setAlpha(255);
            tvTitle.setTextColor(Color.argb(255, 255, 255, 255));
        }

        /**
         * 悬浮控制
         */
        if (oldHeight >= searchLayoutTop) {
            if (recyclerView1.getParent() != llOuter) {
                llInner.removeView(recyclerView1);
                llOuter.removeAllViews();
                llOuter.addView(recyclerView1);
            }
        } else {
            if (recyclerView1.getParent() != llInner) {
                llOuter.removeView(recyclerView1);
                llInner.removeAllViews();
                llInner.addView(recyclerView1);
            }
        }


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //Toolbar上的左上角的返回箭头的键值为Android.R.id.home  不是R.id.home
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }


}

接口回调

public class MyScrollView extends ScrollView {

    public MyScrollView(Context context) {
        super(context, null);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    private OnScrollChangedListener mOnScrollListener;

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollListener != null) {
            mOnScrollListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }

    public void setOnScrollChangedListener(OnScrollChangedListener listener) {
        mOnScrollListener = listener;
    }

    public interface OnScrollChangedListener {
        void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt);
    }
}

其他的也没啥,demo地址在前文。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值