Android实现ExpandView下拉动画列表

在这里插入图片描述
1:自定义一个ExpandView继承FrameLayout

/**
 * date:2020/11/13 0013
 * author:wsm (Administrator)
 * funcation:列表
 */
public class ExpandView extends FrameLayout implements View.OnClickListener {


    private Animation mExpandAnimation;
    private Animation mCollapseAnimation;
    private boolean mIsExpand;

    public ExpandView(Context context) {
        this(context, null);
        // TODO Auto-generated constructor stub
    }

    public ExpandView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        // TODO Auto-generated constructor stub
    }

    public ExpandView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        initExpandView(context);
    }

    private void initExpandView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_expand, this, true);

        mExpandAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.expand);
        mExpandAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                setVisibility(View.VISIBLE);
            }
        });

        mCollapseAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.collapse);
        mCollapseAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                setVisibility(View.INVISIBLE);
            }
        });

    }

    public void collapse() {
        if (mIsExpand) {
            mIsExpand = false;
            clearAnimation();
            startAnimation(mCollapseAnimation);
        }
    }

    public void expand() {
        if (!mIsExpand) {
            mIsExpand = true;
            clearAnimation();
            startAnimation(mExpandAnimation);
        }
    }

    public boolean isExpand() {
        return mIsExpand;
    }

    public void setContentView() {
        View view = null;
        view = LayoutInflater.from(getContext()).inflate(R.layout.layout_expand, null);
        TextView txtpx = view.findViewById(R.id.txt_px);
        TextView txtjl = view.findViewById(R.id.txt_jl);
        TextView txtrq = view.findViewById(R.id.txt_rq);
        TextView txthp = view.findViewById(R.id.txt_hp);
        txtpx.setClickable(true);
        txtjl.setClickable(true);
        txtrq.setClickable(true);
        txthp.setClickable(true);
        txtpx.setOnClickListener(this);
        txtjl.setOnClickListener(this);
        txtrq.setOnClickListener(this);
        txthp.setOnClickListener(this);
        removeAllViews();
        addView(view);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.txt_px:
                setonclick("排序");
                break;
            case R.id.txt_jl:
                setonclick("距离");
                break;
            case R.id.txt_rq:
                setonclick("人气");
                break;
            case R.id.txt_hp:
                setonclick("好评");
                break;


        }
    }

    /**
     * 接口回调根据名称实现具体功能
     *
     * @param name
     */
    public void setonclick(String name) {
        if (mExpandOnClickListener != null) {
            mExpandOnClickListener.onclickitem(name);
        }
    }


    /**
     * 接口回调传textview进行具体操作
     */
    public interface ExpandOnClickListener {
        void onclickitem(String txtname);
    }

    public ExpandOnClickListener mExpandOnClickListener;

    public void setExpandOnClickListener(ExpandOnClickListener expandOnClickListener) {
        mExpandOnClickListener = expandOnClickListener;
    }
}

R.layout.layout_expand是列表中的item
例如:

<?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="wrap_content"
    android:background="#ffffff"
    android:focusable="true"
    android:orientation="vertical"
    android:paddingLeft="12dp">

    <TextView
        android:id="@+id/txt_px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="智能排序"
        android:textColor="#000000"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/txt_jl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="距离优先"
        android:textColor="#000000"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/txt_rq"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="人气优先"
        android:textColor="#000000"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/txt_hp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:gravity="center"
        android:text="好评优先"
        android:textColor="#000000"
        android:textSize="14sp" />


</LinearLayout>

R.anim.expand
R.anim.collapse在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="200"
        android:fromXScale="1."
        android:fromYScale=".0"
        android:pivotX="50%"
        android:pivotY="0%"
        android:toXScale="1."
        android:toYScale="1." />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="120"
        android:fromXScale="1."
        android:fromYScale="1."
        android:pivotX="50%"
        android:pivotY="0%"
        android:toXScale="1."
        android:toYScale="0." />

</set>

activity中实现效果


public class MainActivity extends AppCompatActivity {
    public static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initExpandView();
    }

    public void initExpandView() {
        LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.layout_title);
        final RelativeLayout rl_bg = (RelativeLayout) findViewById(R.id.rl_bg);
        final TextView txtfj = (TextView) findViewById(R.id.textfj_title);
        final TextView txtkm = (TextView) findViewById(R.id.textkm_title);
        final TextView txtpx = (TextView) findViewById(R.id.textpx_title);
        final TextView txtsx = (TextView) findViewById(R.id.textsx_title);
        final ExpandView mExpandView = (ExpandView) findViewById(R.id.expandView);
        //添加列表view
        mExpandView.setContentView();
        mLinearLayout.setClickable(true);
        mLinearLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (mExpandView.isExpand()) {
                    //打开列表
                    mExpandView.collapse();
                    rl_bg.setBackgroundColor(getResources().getColor(R.color.txt0));
                    rl_bg.getBackground().setAlpha(0);//0~255透明度值
                } else {
                    //收起列表
                    mExpandView.expand();
                    rl_bg.setBackgroundColor(getResources().getColor(R.color.txt0));
                    rl_bg.getBackground().setAlpha(50);//0~255透明度值
                }
            }
        });
        //对列表中的item进行想要的操作
        mExpandView.setExpandOnClickListener(new ExpandView.ExpandOnClickListener() {
            @Override
            public void onclickitem(String txtname) {
                Toast.makeText(MainActivity.this, "" + txtname, Toast.LENGTH_SHORT).show();
                switch (txtname) {
                    case "排序":
                        break;
                    case "距离":
                        break;
                }
            }
        });
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#fff"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/layout_title"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textfj_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="附近"
            android:textColor="#333333"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/textkm_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="科目"
            android:textColor="#333333"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/textpx_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="智能排序"
            android:textColor="#333333"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/textsx_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="筛选"
            android:textColor="#333333"
            android:textSize="16sp" />

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/rl_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/layout_title">

        <com.example.expandview.view.ExpandView
            android:id="@+id/expandView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="150dp"
            android:background="#ffffff"
            android:clickable="true"
            android:visibility="invisible" />
    </RelativeLayout>


</LinearLayout>

demo链接
提取码:wrcj

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值