【问题标题】:Android get ListView sub item from another sub itemAndroid从另一个子项获取ListView子项
【发布时间】:2013-05-23 11:21:52
【问题描述】:

我正在使用这种方式来制作我的 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 个列表项?

【问题讨论】:

标签: android listview expandablelistview


【解决方案1】:

您可以使用 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

希望这会对你有所帮助.. 谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    相关资源
    最近更新 更多