使用Expandanimation单击ListView中的第二项时,如何隐藏第一项?

时间:2014-03-10 10:39:59

标签: android listview animation

我使用开发listview的行为就像expandablelistview它的工作正常但是当我点击第二个项目第一个项目没有关闭时会出现一个问题

我正在使用像

这样的动画类

ExpandAnimation.java

public ExpandAnimation(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;
        }
    }

ExpandAnimationDemo.java ,如

public class ExpandAnimationDemo extends Activity {

//  Boolean sameItemClicked=false;

    boolean boolitem = false;
    View PreviousToolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
        setContentView(R.layout.main);

        ListView list = (ListView)findViewById(R.id.udiniList);

        // Creating the list adapter and populating the list
        ArrayAdapter<String> listAdapter = new CustomListAdapter(this, R.layout.list_item);
        for (int i=0; i<20;i++)
            listAdapter.add("udini"+i);
        list.setAdapter(listAdapter);

        // Creating an item click listener, to open/close our toolbar for each item
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @SuppressWarnings("unused")
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {


                View toolbar = view.findViewById(R.id.toolbar);

                // Creating the expand animation for the item
                ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500);

                // Start the animation on the toolbar
                toolbar.startAnimation(expandAni);

                  }




        });
    }

    /**
     * A simple implementation of list adapter.
     */
    class CustomListAdapter extends ArrayAdapter<String> {

        public CustomListAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);
            }

            ((TextView)convertView.findViewById(R.id.title)).setText(getItem(position));

            // Resets the toolbar to be closed
            View toolbar = convertView.findViewById(R.id.toolbar);
            ((LinearLayout.LayoutParams) toolbar.getLayoutParams()).bottomMargin = -50;
            toolbar.setVisibility(View.GONE);

            return convertView;
        }
    }
}

Myxml文件是 list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <TextView android:id="@+id/title"
              android:padding="20dip"
              android:focusable="false"
              android:focusableInTouchMode="false"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"/>

    <!--***********************-->
    <!--*** TOOLBAR LAYOUT ****-->
    <!--***********************-->

    <LinearLayout android:id="@+id/toolbar"
          android:layout_marginBottom="-50dip"
          android:visibility="gone"
          android:layout_height="50dip"
          android:layout_width="fill_parent">
        <Button android:id="@+id/doSomething1"
                android:layout_height="50dip"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:layout_width="wrap_content"
                android:text="Harder"/>
        <Button android:id="@+id/doSomething2"
                android:layout_height="50dip"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:layout_width="wrap_content"
                android:text="Better"/>
        <Button android:id="@+id/doSomething3"
                android:layout_height="50dip"
                android:layout_width="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:text="Faster"/>
        <Button android:id="@+id/doSomething4"
                android:layout_height="50dip"
                android:layout_width="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:text="Stronger"/>

    </LinearLayout>
</LinearLayout>

这是基于此链接开发的    https://github.com/Udinic/SmallExamples/tree/master/ExpandAnimationExample

这很好但是当选择第二项第一项没有关闭时,请指导如何实现我的要求提前谢谢

是否有任何解决方案,请给我一点

1 个答案:

答案 0 :(得分:0)

检查以下内容。

mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {

                    @Override
                    public void onGroupExpand(int groupPosition) {
                        int len = menuAdapter.getGroupCount();
                        for (int i = 0; i < len; i++) {
                            if (i != groupPosition
                                    && menuAdapter.getChildrenCount(i) > 0) {
                                mExpandableListView.collapseGroup(i);
                            }
                        } }
                });