android - 我如何为自定义 ExpandableListview 上的每个项目设置动画

标签 android android-animation expandablelistview

我使用 This Tutorial为了使我的 Custome Expandable ListView ,我需要在有人点击它的父级时将动画设置为项目。 (只是我点击的组必须有动画) 我搜索并发现我也可以使用此方法来执行此操作。

   public void onGroupCollapse(int groupPosition)

但我不知道如何查看我的 child 并为其设置动画:(

这是我的动画代码:

  Animation animation = AnimationUtils.loadAnimation(childsanimationview.getContext(), R.anim.fadein);
            childsanimationview.setAnimation(animation);

这是我的适配器代码

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

private ArrayList<Parent> parents;
private LayoutInflater inflater;

// public View childsanimationview ;
public MyExpandableListAdapter(Context c, ArrayList<Parent> parentsfrom) {
    // Create Layout Inflator
    parents = parentsfrom;
    //  childsanimationview=childsanimation;
    inflater = LayoutInflater.from(c);
}

//这个函数用来膨胀父行 View

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parentView) {

    final Parent parent = parents.get(groupPosition);

    // Inflate grouprow.xml file for parent rows
    convertView = inflater.inflate(R.layout.grouprow, parentView, false);
    // Get grouprow.xml file elements and set values
    ((TextView) convertView.findViewById(R.id.parent_name)).setText(parent.getParentName());
    ImageView parent_right_img = (ImageView) convertView.findViewById(R.id.parent_right_img);
    ImageView parent_left_img = (ImageView) convertView.findViewById(R.id.parent_left_img);

    // Change right && left arrow image on parent at runtime
    if (isExpanded == true) {
        parent_right_img.setImageResource(R.drawable.parent_opened);
        parent_left_img.setImageResource(R.drawable.parent_opened);
    } else {
        parent_right_img.setImageResource(R.drawable.parent_right_closed);
        parent_left_img.setImageResource(R.drawable.parent_left_closed);
    }
    //     checkbox.setOnCheckedChangeListener(new CheckUpdateListener(parent));

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parentView) {

    final Parent parent = parents.get(groupPosition);
    final Child child = parent.getChildren().get(childPosition);

    // Inflate childrow.xml file for child rows
    convertView = inflater.inflate(R.layout.childrow, parentView, false);

    TextView Product_Name = (TextView) convertView.findViewById(R.id.product_name);
    TextView Ingredient = (TextView) convertView.findViewById(R.id.ingredient);
    TextView Product_Price = (TextView) convertView.findViewById(R.id.product_price);
    ImageView Product_Image = (ImageView) convertView.findViewById(R.id.product_img);


    Product_Name.setText(child.getProductName());
    Ingredient.setText(child.getIngredient());
    Product_Price.setText(child.getProductPrice());
    Product_Image.setImageResource(child.getProductImg());

    //  Product_Image.setImageResource(getResources().getIdentifier("com.androidexample.customexpandablelist:drawable/setting" + parent.getParentName(), null, null));

    return convertView;
}


@Override
public Object getChild(int groupPosition, int childPosition) {
    return parents.get(groupPosition).getChildren().get(childPosition);
}

//Call when child row clicked
@Override
public long getChildId(int groupPosition, int childPosition) {
    /****** When Child row clicked then this function call *******/
    return childPosition;
}

@Override
public int getChildrenCount(int groupPosition) {
    int size = 0;
    if (parents.get(groupPosition).getChildren() != null)
        size = parents.get(groupPosition).getChildren().size();
    return size;
}

@Override
public Object getGroup(int groupPosition) {
    return parents.get(groupPosition);
}

@Override
public int getGroupCount() {
    return parents.size();
}

//Call when parent row clicked
@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public void notifyDataSetChanged() {
    // Refresh List rows
    super.notifyDataSetChanged();
}

@Override
public boolean isEmpty() {
    return ((parents == null) || parents.isEmpty());
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

@Override
public boolean hasStableIds() {
    return true;
}

我该怎么做?

我怎样才能引用我 child 的观点?

坦克斯

最佳答案

检查下面的代码。创建一个动画对象并将其设置为您需要的 View 。

convertView.startAnimation(animation);  

关于android - 我如何为自定义 ExpandableListview 上的每个项目设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25666184/

相关文章:

java - 如何修复工具栏在选择另一个选项卡之前不更改标题的问题?

android - 为什么 Fragment 在 .hide() 和 .setCustomAnimations() 上崩溃?

java - 从 ExpandableListView 调用的 Fragment Communicator 接口(interface)不起作用

android - 我们可以通过推送通知发送多少数据?

android - Butterknife fragment 按钮不起作用

javascript - 如何限制对api rest的调用

heap - 三星 Galaxy S3 的堆大小是多少?

安卓循环定时器

android - 在可展开的 ListView 中展开所有子项

android - 具有不可扩展项的 ExpandableListView