0

我正在使用这种方式来制作我的 ListView 项目的下拉菜单。

这是我的项目的 XML

<RelativeLayout ...>
  <TableLayout ...>
    <Something like TextView, ImageView... />
    <ImageButton android:id="@+id/btnMenu"... />
  </TableLayout>

  <LinearLayout android:id="@+id/dropMenu" android:visibility="gone" ...>
    <Some other Button... />
  </LinearLayout>
</RelativeLayout>

所以在“getView(int position, View convertView, ViewGoup parent)”中,当用户点击“btnMenu”时,我会设置“dropMenu”可见,看起来像一个下拉菜单。

我的问题是

  1. 首先我单击第 4 项,使其下拉菜单显示
  2. 其次,我单击第 6 项,使其下拉菜单显示,但第 4 项的下拉菜单应设置为“消失”。

这是我试过但没用

View lastView=getChildAt(lastIndex);
lastView.findViewById(R.id.dropMenu).setVisibility(View.GONE);

当我实际上在第 6 个列表项中时,如何操作第 4 个列表项?

4

1 回答 1

0

您可以使用 ExpandAnimation 实现您的要求。

在此处输入图像描述

在此处输入图像描述

ExpandListItem.java

package com.list.animation;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.LinearLayout.LayoutParams;

public class ExpandListItem extends Animation {
    private View mAnimatedView;
    private LayoutParams mViewLayoutParams;
    private int mMarginStart, mMarginEnd;
    private boolean mIsVisibleAfter = false;
    private boolean mWasEndedAlready = false;

    /**
     * Initialize the animation
     * @param view The layout we want to animate
     * @param duration The duration of the animation, in ms
     */
    public ExpandListItem(View view, int duration) {

        setDuration(duration);
        mAnimatedView = view;
        mViewLayoutParams = (LayoutParams) view.getLayoutParams();

        // decide to show or hide the view
        mIsVisibleAfter = (view.getVisibility() == View.VISIBLE);

        mMarginStart = mViewLayoutParams.bottomMargin;
        mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0);

        view.setVisibility(View.VISIBLE);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);

        if (interpolatedTime < 1.0f) {

            // Calculating the new bottom margin, and setting it
            mViewLayoutParams.bottomMargin = mMarginStart
                    + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);

            // Invalidating the layout, making us seeing the changes we made
            mAnimatedView.requestLayout();

        // Making sure we didn't run the ending before (it happens!)
        } else if (!mWasEndedAlready) {
            mViewLayoutParams.bottomMargin = mMarginEnd;
            mAnimatedView.requestLayout();

            if (mIsVisibleAfter) {
                mAnimatedView.setVisibility(View.GONE);
            }
            mWasEndedAlready = true;
        }
    }
}

要完整实施,请阅读以下帖子

http://amitandroid.blogspot.in/2013/03/android-listview-with-animation.html

希望这会帮助你..谢谢

于 2013-05-23T11:33:55.067 回答