【问题标题】:Custom animation for ExpandableListView ChildExpandableListView Child 的自定义动画
【发布时间】:2016-06-13 03:25:45
【问题描述】:

我想在 ExpandableListView 中为孩子使用自定义动画(向下/向上滑动)。因此,如果我单击元素 #2,我希望父元素 #2 的子元素使用动画展开。并退出/关闭,当我再次单击父级时。

但是,我在将动画指向孩子本身时确实存在问题。我能得到的最接近的方法是使用“myListView.getChildAt(groupPosition).setAnimation(slide_down);”为父级设置动画。如下所示。 当我使用这段代码并单击元素 x 时,元素 x 将制作动画 - 但是应该制作动画的是孩子,而不是被点击的父。

我在 MainActivity 中的代码,其中“myAdapter”是适配器,“myListView”是 ExpandableListView:

    ExpandableListAdapter myAdapter = new ExpandableListAdapter(context, arrRO);
    final ExpandableListView myListView = (ExpandableListView) findViewById(R.id.reminderlistView);
    myListView.setAdapter(myAdapter);

    myListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

            if (myListView.isGroupExpanded(groupPosition)) {
                //Need to implement collapsing animation as well
                myListView.collapseGroup(groupPosition);
            } else {
                boolean autoScrollToExpandedGroup = false;
                myListView.expandGroup(groupPosition, autoScrollToExpandedGroup);
                setupLayoutAnimation(groupPosition);
                //
            }
            //telling the listView we have handled the group click, and don't want the default actions.
            return true;
        }

        private void setupLayoutAnimation(int groupPosition) {

            Animation slide_down = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);

            myListView.getChildAt(groupPosition).setAnimation(slide_down);

            //myListView.setAnimation(slide_down);

        }

我还包括了“myListView.setAnimation(slide_down);”,但是如果我使用它并单击一个元素,每个显示的元素(父母 + 孩子)都会制作这个小动画,我只想要它给孩子.

如果这有什么不同的话,每个父母总是只有一个孩子。

【问题讨论】:

    标签: android animation parent-child expandablelistview slide


    【解决方案1】:

    ExpandableListView 不是很灵活。您最好创建一个自定义视图,您可以在其中控制在单击组时如何显示组的子项。

    要让您的自定义视图到位,还有一些工作要做,但它会为您提供比 ExpandableListView 更大的灵活性。

    您需要 2 个新视图、一个父视图和一个子视图。父视图将从其中一个 ViewGroup 继承(使用 LinearLayout 或 FrameLayout,因为您有一个子视图),而 childView 将基于您要显示的内容,它可以是 TextView 或一些带有几个子视图的 ViewGroup。当您检测到对父级的点击时,您会为其包含的子级设置动画,如果需要,您可以使用接口将该点击事件向上传递。

    【讨论】:

      猜你喜欢
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 2013-03-06
      相关资源
      最近更新 更多