哎 累了大概一个半月左右 项目学生版的终于搞完了,但是还有点小问题 未处理 小白入行写的很low,新版的教师版就在昨天也开始步入了新的起点,看了设计图 ,ui界面太复杂,一个界面6种布局,我丢对于我这种小白来说,苦不堪言啊,没办法写呗,在做页面的时候,发现了一个新的好玩的东西,下拉筛选,当时有想过用popwindow实现,但是相对来说,会很麻烦,其中也遇到了很多的问题,自己就百度了一个自定义view写的下拉的筛选,先看一下效果图
我大概觉得还行 没有gif 勉强凑合着看吧,这也是借鉴的别人写的自己添加了自己想要的效果
上代码:
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import xx.xx.R;
public class DropDownMenuView extends RelativeLayout {
//默认插值器
private Interpolator mInterpolator = new DecelerateInterpolator();
//顶部的view
private View viewTop;
//筛选条件的view
private View viewMenu;
//遮盖层的view
private View viewMask;
private Context mContext;
//遮盖层默认颜色
private int mMaskViewColor = 0x60434444;
//动画时间
private int mDurationTime = 300;
//判断当前状态
private boolean mIsOpen = false;
public DropDownMenuView(Context context) {
this(context,null);
}
public DropDownMenuView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public DropDownMenuView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
//初始化属性
initAttr(attrs);
initView();
}
private void initView(){
//添加一个遮盖层
addMaskView();
//检查是否满足要求
checkLayout();
}
/**
* 初始化属性
*/
private void initAttr(@Nullable AttributeSet attrs){
TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.DropDownMenuView);
mMaskViewColor = array.getColor(R.styleable.DropDownMenuView_mask_view_color, mMaskViewColor);
mDurationTime = array.getInteger(R.styleable.DropDownMenuView_duration,mDurationTime);
array.recycle();
}
/**
* 添加一层遮盖层(只有在展开情况下,才会显示遮盖层)
*/
private void addMaskView(){
if(viewMask!=null){
return;
}
viewMask = new View(mContext);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
viewMask.setLayoutParams(params);
//灰黑色透明背景
viewMask.setBackgroundColor(mMaskViewColor);
//添加view
addView(viewMask);
//默认是隐藏的
viewMask.setVisibility(GONE);
viewMask.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
//收缩遮盖层
close();
}
});
}
/**
* 布局填充完成时
*/
@Override protected void onFinishInflate() {
super.onFinishInflate();
//检查子view的数量
checkChildCount();
viewTop = getChildAt(1);
viewMenu = getChildAt(2);
//设置可点击,避免点击事件传递到底层的viewMask
viewTop.setClickable(true);
viewMenu.setClickable(true);
//设置背景色
if(viewTop.getBackground()==null){
viewTop.setBackgroundColor(Color.WHITE);
}
if(viewMenu.getBackground()==null){
viewMenu.setBackgroundColor(Color.WHITE);
}
//默认是隐藏的
viewMenu.setVisibility(GONE);
}
/**
* 检查是否满足条件(需要view绘制完成才能检查)
*/
private void checkLayout(){
post(new Runnable() {
@Override public void run() {
//检查父布局是否满足要求
checkParentLayout();
}
});
}
/**
* 检查DropDownMenu所在的容器布局,如果是LinearLayout,抛出错误
*/
private void checkParentLayout(){
ViewGroup viewGroup = (ViewGroup) this.getParent();
if(!(viewGroup instanceof FrameLayout)){
throw new RuntimeException("ParentView must is FrameLayout ");
}
}
/**
* 检查子view的数量,最多只有三个(有一个是遮盖层)
*/
private void checkChildCount(){
if(this.getChildCount()!=3){
throw new RuntimeException("Only two child view support!");
}
}
/**
* 展开动画
*/
private void openAnimation(){
//设置展开的基准位置,从顶部开始展开(默认是中心位置展开收缩)
viewMenu.setPivotY(0);
viewMenu.setVisibility(View.VISIBLE);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(viewMenu,"scaleY",0f,1f);
scaleY.setDuration(mDurationTime);
scaleY.setInterpolator(mInterpolator);
scaleY.addListener(new Animator.AnimatorListener() {
@Override public void onAnimationStart(Animator animation) {
}
@Override public void onAnimationEnd(Animator animation) {
mIsOpen = true;
}
@Override public void onAnimationCancel(Animator animation) {
}
@Override public void onAnimationRepeat(Animator animation) {
}
});
scaleY.start();
//渐变显示maskView
viewMask.setVisibility(View.VISIBLE);
changeAlpha(viewMask,0f,1f);
}
/**
* 收缩动画
*/
private void closeAnimation(){
viewMenu.setPivotY(0);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(viewMenu,"scaleY",1f,0f);
scaleY.setDuration(mDurationTime);
scaleY.setInterpolator(mInterpolator);
scaleY.addListener(new Animator.AnimatorListener() {
@Override public void onAnimationStart(Animator animation) {
}
@Override public void onAnimationEnd(Animator animation) {
viewMenu.setVisibility(GONE);
if(viewMask!=null){
viewMask.setVisibility(GONE);
}
mIsOpen = false;
}
@Override public void onAnimationCancel(Animator animation) {
}
@Override public void onAnimationRepeat(Animator animation) {
}
});
scaleY.start();
//渐变隐藏maskView
changeAlpha(viewMask,1f,0f);
}
/**
* 实现背景渐变
* @param view
* @param startStatus
* @param endStatus
*/
private void changeAlpha(@NonNull View view, float startStatus, float endStatus){
ObjectAnimator alpha = ObjectAnimator.ofFloat(view,"alpha",startStatus,endStatus);
alpha.setDuration(mDurationTime);
alpha.start();
}
/***************************************外部调用方法********************************************/
/**
* 展开
*/
public void open(){
if(isOpen()){
return;
}
if(viewMenu==null){
return;
}
openAnimation();
}
/**
* 收缩
*/
public void close(){
if (!isOpen()){
return;
}
if(viewMenu==null){
return;
}
closeAnimation();
}
/**
* 判断当前是处于展开状态还是隐藏状态
* @return
*/
public boolean isOpen() {
return mIsOpen;
}
/**
* 更改动画的插值器
* @param interpolator
*/
public void setInterpolator(Interpolator interpolator) {
this.mInterpolator = interpolator;
}
}
布局也得有讲究啊
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.officehall.TelListActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_TelList"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_44"
android:layout_marginTop="@dimen/dp_44"
android:background="#FFFFFF"
app:tabTextAppearance="@style/TabLayoutTextStyle" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp_TelList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/dp_88"
android:overScrollMode="never" />
<xx.xx.xx.view.DropDownMenuView
android:id="@+id/dropDownMenu_tel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_telTitle"
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_44">
<include layout="@layout/title2" />
<TextView
android:id="@+id/tv_TelScreen"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/dp_20"
android:drawableLeft="@mipmap/icon_jctn_pick"
android:drawablePadding="@dimen/dp_3"
android:gravity="center"
android:text="筛选"
android:textColor="@color/white"
android:textSize="@dimen/sp_16" />
</RelativeLayout>
<!-- 第二个布局是筛选条件的layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="387dp"
android:layout_below="@+id/rl_telTitle"
android:background="#0F59A5"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_43"
android:gravity="center_vertical"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/rl_tel_yuanTeam"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="@+id/tv_telYuan"
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_30"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/shape_screen"
android:gravity="center"
android:paddingLeft="@dimen/dp_25"
android:paddingRight="@dimen/dp_25"
android:text="院系"
android:textColor="#666666"
android:textSize="@dimen/sp_14" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_tel_zhongTeam"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="@+id/tv_telZhong"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:gravity="center"
android:paddingLeft="@dimen/dp_25"
android:paddingRight="@dimen/dp_25"
android:text="中队"
android:textColor="#FFFFFF"
android:textSize="@dimen/sp_14" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_tel_quTeam"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="@+id/tv_telQu"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:gravity="center"
android:paddingLeft="@dimen/dp_25"
android:paddingRight="@dimen/dp_25"
android:text="区队"
android:textColor="#FFFFFF"
android:textSize="@dimen/sp_14" />
</RelativeLayout>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ECF1F5" />
</LinearLayout>
</xx.xx.xx.view.DropDownMenuView>
</FrameLayout>
至于为啥外面必须要用fragmelayout我也不怎么清除 最后我附上原文链接 ,毕竟是抄的
我丢 找不到了 ,回头找找再放上吧