【问题标题】:I need animation in ExpandableLIstView in onGroupExpand and onGroupCollapse我需要 onGroupExpand 和 onGroupCollapse 的 ExpandableLIstView 中的动画
【发布时间】:2015-11-02 11:36:51
【问题描述】:

我是 android 新手,非常需要在 onGroupExpand 和 onGroupCollapse 的 ExpandableLIstView 中做动画。

请帮帮我。

下面是我的代码 -

我的 MainActivity 是

package comsam.myexpandablelistviedjsonapp;

import android.app.ProgressDialog;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.animation.PathInterpolatorCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Interpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ExpandListAdapter ExpAdapter;
    private ExpandableListView ExpandList;

    private Interpolator easeInOutQuart = PathInterpolatorCompat.create(0.77f, 0f, 0.175f, 1f);

    private int lastExpandedPosition = -1;

    ArrayList<Parent> list = new ArrayList<Parent>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView animatedButton = (ImageView) findViewById(R.id.parent_img);
        ExpandList = (ExpandableListView)findViewById(R.id.exp_list);
        new JSONAsyncTask().execute("http://local.agdits.com.bd/hrdemo/expandable.php");

       ExpAdapter = new ExpandListAdapter(MainActivity.this, list);

        ExpandList.setAdapter(ExpAdapter);


        //to show which list has been clicked for Expand


        ExpandList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(final int groupPosition) {
                if (lastExpandedPosition != -1 && groupPosition != lastExpandedPosition) {
                    ExpandList.collapseGroup(lastExpandedPosition);
                    // setupLayoutAnimationClose(groupPosition);
                    // new AnimUtils().collapse(ExpandList);
                }
                lastExpandedPosition = groupPosition;


            }


        });


        //to show which list has been clicked for Collapse
        ExpandList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int parentPosition) {
                Toast.makeText(getBaseContext(), ExpAdapter.getGroupName(parentPosition) + " is collapse ", Toast.LENGTH_LONG).show();

            }
        });

        //to show which Child has been clicked
        ExpandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int parentPosition, int childPosition, long id) {

                Toast.makeText(getBaseContext(), ExpAdapter.getChildName(parentPosition, childPosition) + " from category " + ExpAdapter.getGroupName(parentPosition) + " is selected", Toast.LENGTH_LONG).show();
                return false;
            }
        });





    }



    private void expand1() {
        System.out.println("I am here expand");

    }

    private void collapse() {
        System.out.println("I am here collapse");
    }




    class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(MainActivity.this);
            dialog.setMessage("Loading, please wait");
            dialog.setTitle("Connecting server");
            dialog.show();
            dialog.setCancelable(false);
        }

        @Override
        protected Boolean doInBackground(String... urls) {
            try {

                //------------------>>
                HttpGet httppost = new HttpGet(urls[0]);
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(httppost);

                // StatusLine stat = response.getStatusLine();
                int status = response.getStatusLine().getStatusCode();

                if (status == 200) {
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);

                    //System.out.println(data);

                    ArrayList<Child> ch_list;

                    JSONArray jsonArray1 = new JSONArray(data);

                    for (int i = 0; i < jsonArray1.length(); i++) {
                        JSONObject object = jsonArray1.getJSONObject(i);
                        // System.out.println(object.getString("_id"));
                        Parent parent = new Parent();
                        parent.setParentName(object.getString("CategoryName"));
                        parent.setParentImage(object.getString("CatImage"));
                        //System.out.println(object.getString("CategoryName"));

                        //Make Child List Array
                        ch_list = new ArrayList<Child>();
                        JSONArray childArray = object.getJSONArray("child");
                        //System.out.println(childArray.length());
                        for (int j = 0; j < childArray.length(); j++) {

                            JSONObject childobject = childArray.getJSONObject(j);

                            Child child = new Child();

                            child.setName(childobject.getString("name"));
                            child.setImage(childobject.getString("image"));

                            ch_list.add(child);
                        }

                       // System.out.println(childArray);
                        //Child List to Parent Items
                        parent.setItems(ch_list);

                        list.add(parent);


                    }
                    return true;
                }

                //------------------>>



            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return false;
        }

        protected void onPostExecute(Boolean result) {
            dialog.cancel();
            ExpAdapter.notifyDataSetChanged();
            if(result == false) {
               Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
            }

        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我的 ExpandListAdapter 类是

package comsam.myexpandablelistviedjsonapp;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.InputStream;
import java.util.ArrayList;


/**
 * Created by tanvinn on 10/27/2015.
 */
public class ExpandListAdapter extends BaseExpandableListAdapter{

    private Context context;
    private ArrayList<Parent> parents;
    public MainActivity activity;
    public ImageLoader imageLoader;
    public int lastExpandedGroupPosition=-1;

    public ExpandListAdapter(Context context, ArrayList<Parent> parents) {
        this.context = context;
        this.parents = parents;

        // Create ImageLoader object to download and show image in list
        // Call ImageLoader constructor to initialize FileCache
        imageLoader = new ImageLoader(activity);
    }

    @Override
    public Object getChild(int parentPosition, int childPosition) {
        ArrayList<Child> chList = parents.get(parentPosition).getItems();
        return chList.get(childPosition);
    }

    @Override
    public long getChildId(int parentPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int parentPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        Child child = (Child) getChild(parentPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }


        TextView tv = (TextView) convertView.findViewById(R.id.child_txt);
        ImageView imageView = (ImageView)convertView.findViewById(R.id.child_img);
        imageView.setImageResource(R.drawable.geofence1);


        tv.setText(child.getName().toString());
        //new DownloadImageTask(imageView).execute(child.getImage().toString());
        imageLoader.DisplayImage(child.getImage().toString(), imageView);



        return convertView;
    }

    @Override
    public int getChildrenCount(int parentPosition) {
        ArrayList<Child> chList = parents.get(parentPosition).getItems();
        return chList.size();
    }

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

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

    @Override
    public long getGroupId(int parentPosition) {
        return parentPosition;
    }

    @Override
    public View getGroupView(int parentPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        Parent parent1 = (Parent) getGroup(parentPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.parent_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.parent_txt);
        ImageView imageView = (ImageView)convertView.findViewById(R.id.parent_img);
        imageView.setImageResource(R.drawable.geofence1);

        tv.setText(parent1.getParentName());
       // new DownloadImageTask(imageView).execute(parent1.getParentImage().toString());
        imageLoader.DisplayImage(parent1.getParentImage().toString(), imageView);
        return convertView;
    }


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

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


    public Object getGroupName(int parentPosition) {
        Parent parent1 = (Parent) getGroup(parentPosition);
        String parentName = parent1.getParentName();

        return parentName;
    }

    public Object getChildName(int parentPosition, int childPosition) {

        Child child = (Child) getChild(parentPosition, childPosition);
        String childName = child.getName();

        return childName;
    }




    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }

    }


}   

提前致谢。

【问题讨论】:

    标签: android


    【解决方案1】:

    你可以试试这个AnimatedExpandableListView

    listView = (AnimatedExpandableListView) findViewById(R.id.listView);
    listView.setAdapter(YOUR_ADAPTER);
    
        // In order to show animations, we need to use a custom click handler
        // for our ExpandableListView.
        listView.setOnGroupClickListener(new OnGroupClickListener() {
    
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                // We call collapseGroupWithAnimation(int) and
                // expandGroupWithAnimation(int) to animate group 
                // expansion/collapse.
                if (listView.isGroupExpanded(groupPosition)) {
                    listView.collapseGroupWithAnimation(groupPosition);
                } else {
                    listView.expandGroupWithAnimation(groupPosition);
                }
                return true;
            }
    
        });
    

    Android ExpandableListView using animation

    Animation for expandableListView

    【讨论】:

    • 我已经编辑了我的问题。云你看看,让我知道如何在展开和折叠组视图时添加动画。
    • @ShaymolBapary 好的。你检查上面的代码了吗?请从资源下载并检查。如果不相似..然后我会描述
    • 我已经做了那个例子。但我需要 ExpandableListview 中的动画。
    【解决方案2】:

    item_animation_fall_down.xml 中的以下内容(将此 xml 文件放在 res/anim 文件夹中)

      <?xml version="1.0" encoding="utf-8"?> 
      <set xmlns:android="http://schemas.android.com/apk/res/android"android:duration="500"> 
      <translate android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
           android:fromYDelta="50%p" android:toYDelta="0" /> 
      <alpha android:fromAlpha="0" android:toAlpha="1" 
       android:interpolator="@android:anim/accelerate_decelerate_interpolator" /> 
      </set> 
    

    getGroupView(....) {} 块中的以下内容 convertView.setAnimation(AnimationUtils.loadAnimation(_context,R.anim.item_animation_fall_down));

    Activity类中的如下

    expListView.setAnimation(AnimationUtils.loadAnimation(this,R.anim.item_animation_from_right));

    activity_home.xml 中的以下内容(到根元素)

    android:fillAfter="true" android:fillEnabled="true"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      相关资源
      最近更新 更多