Android - How do implement ListViewAnimation to an existing ListView?

人走茶凉 提交于 2019-12-12 03:07:26

问题


I have a ListView that its contents are populated from a remote server. It's a simple ListView created by following tutorials you normally found on blogs. Recently, I've found out ListViewAnimation and would like to use it in my project.

I already have a Listview, now how do I implement the listviewanimation library in the existing one?

I have tried to follow the wiki on github, but still couldn't make through it.

Here is my ListView activity:

public class CategoryActivity extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_category);

        ListView lv = getListView();


        lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                    int arg2, long arg3) {

                // when item is clicked, go to its corresponding details
                Intent i = new Intent(getApplicationContext(),
                        ItemListActivity.class);
                String category_id = ((TextView) view
                        .findViewById(R.id.category_id)).getText()
                        .toString();
                i.putExtra("category_id", category_id);
                startActivity(i);
            }
        });

        new LoadCategories().execute();
    }

    class LoadCategories extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            // getting JSON string from URL
            String json = jsonParser.makeHttpRequest(Constants.URL_CATEGORY, "GET",
                    params);

            // Check your log cat for JSON reponse
            Log.d("Categories JSON: ", "> " + json);

            try {
                categories = new JSONArray(json);

                if (categories != null) {
                    // looping through All albums
                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject c = categories.getJSONObject(i);

                        // Storing each json item values in variable
                        String id = c.getString(TAG_ID);
                        String name = c.getString(TAG_NAME);
                        String songs_count = c.getString(TAG_CATEGORIES_COUNT);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_ID, id);
                        map.put(TAG_NAME, name);
                        map.put(TAG_CATEGORIES_COUNT, songs_count);

                        // adding HashList to ArrayList
                        categoryList.add(map);
                    }
                } else {
                    Log.d("Categories: ", "null");
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }


        @Override
        protected void onPostExecute(String file_url) {
            runOnUiThread(new Runnable() {
                public void run() {

                    // populate each row with json data
                    ListAdapter adapter = new SimpleAdapter(
                            CategoryActivity.this, categoryList,
                            R.layout.custom_list_category, new String[] {
                                    TAG_ID, TAG_NAME, TAG_CATEGORIES_COUNT },
                            new int[] { R.id.category_id, R.id.category_name,
                                    R.id.songs_count });

                    setListAdapter(adapter);
                }
            });
        }
    }

}

This is the layout.xml for the activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:dividerHeight="15dp"
        android:footerDividersEnabled="true"
        android:headerDividersEnabled="true" />
</LinearLayout>

As you can see in the code, the list is customized and is populated by the SimpleAdapter.

Can anyone guide me on how to implement the ListViewAnimation library into my project here?


EDIT

Here is my new ListActivity. I've modified the AsyncTask onPostExecute part. The problem now is doesn't display anything. Just a blank screen:

public class CategoryActivity extends ListActivity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_category);


            // Check toast which user is logged in
            userDetails = db.getUserDetails().get("name").toString();

            setTitle(userDetails);

            // Hashmap for ListView
            categoryList = new ArrayList<HashMap<String, String>>();

            new LoadCategories().execute();

            // get listview
            ListView lv = getListView();

            //lv = (ListView) findViewById(R.id.category_list);
            lv.setDivider(null);

            lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View view,
                        int arg2, long arg3) {
                    Intent i = new Intent(getApplicationContext(),
                            ItemListActivity.class);

                    String category_id = ((TextView) view
                            .findViewById(R.id.category_id)).getText()
                            .toString();
                    i.putExtra("category_id", category_id);

                    startActivity(i);
                }
            });
    }

    class LoadCategories extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(CategoryActivity.this);
            pDialog.setMessage("Listing Categories...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            // getting JSON string from URL
            String json = jsonParser.makeHttpRequest(Constants.URL_CATEGORY, "GET",
                    params);

            // Check your log cat for JSON reponse
            Log.d("Categories JSON: ", "> " + json);

            return json;
        }

        @Override
        protected void onPostExecute(String json) {
            try {
                categories = new JSONArray(json);

                if (categories != null) {
                    // looping through All albums
                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject c = categories.getJSONObject(i);

                        // Storing each json item values in variable
                        String id = c.getString(TAG_ID);
                        String name = c.getString(TAG_NAME);
                        String songs_count = c.getString(TAG_CATEGORIES_COUNT);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_ID, id);
                        map.put(TAG_NAME, name);
                        map.put(TAG_CATEGORIES_COUNT, songs_count);

                        // adding HashList to ArrayList
                        categoryList.add(map);
                    }

                    mAdapter = new CategoryListAdapter(CategoryActivity.this, categoryList);
                    AnimationAdapter animAdapter = new AlphaInAnimationAdapter(mAdapter);
                    animAdapter.setAbsListView(getListView());
                    getListView().setAdapter(animAdapter);

                    // dismiss the dialog after getting all albums
                    pDialog.dismiss();
                } else {
                    Log.d("Categories: ", "null");
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

}

CategoryListAdapter class:

public class CategoryListAdapter extends ArrayAdapter<HashMap<String, String>>{
        private Context mContext;
        LayoutInflater inflater;
        private final ArrayList<HashMap<String, String>> urls;
        HashMap<String, String> resultp = new HashMap<String, String>();

        public CategoryListAdapter(Context context, ArrayList<HashMap<String, String>> items) {
            /*super(items);
            mContext = context;*/
            this.mContext = context;

            urls = items;
        }

        @Override
        public long getItemId(int position) {
            return getItem(position).hashCode();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView item_name;
            TextView item_count;
            TextView item_id;

            inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = inflater.inflate(R.layout.custom_list_category, parent, false);

            resultp = urls.get(position);

            item_name = (TextView) view.findViewById(R.id.category_name);
            item_count = (TextView) view.findViewById(R.id.songs_count);
            item_id = (TextView) view.findViewById(R.id.category_id);

            item_name.setText(resultp.get(CategoryActivity.TAG_NAME));
            item_count.setText(resultp.get(CategoryActivity.TAG_CATEGORIES_COUNT));
            item_id.setText(resultp.get(CategoryActivity.TAG_ID));

            return view;
        }
}

回答1:


You can also try setting layout animation to your existing list view. For ex:

AnimationSet set = new AnimationSet(true);
Animation animation =  AnimationUtils.loadAnimation(getContext(),
            R.anim.fadeout);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(
        set, 0.5f);
<Your_list_view>.setLayoutAnimation(controller);

This will set R.anim.fadeout animation to every item of the list.

EDIT Try this

Save this xml in anim folder(this is fed-in effect)

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="500"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

</set>

And in createView

ListView lv = getListView();

AnimationSet set = new AnimationSet(true);
Animation animation =  AnimationUtils.loadAnimation(getContext(),
            R.anim.fadein);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(
        set, 0.5f);
lv.setLayoutAnimation(controller);

And remove AnimationAdapterCode




回答2:


Have you done this --

Adapter adapter = new Adapter(this, 1, notificationList );
        AnimationAdapter animAdapter = new AlphaInAnimationAdapter(adapter);
        animAdapter.setAbsListView(listView);
        listView.setAdapter(animAdapter);



回答3:


I think you must fix your onPostExecute(). That runs on the main or UI thread. You don't need the runOnUiThread() inside onPostExecute().

Now, coming to how do we use the library part: you would add the jar file to your project under libs folder and make sure that it's also linked when you are building the apk. The steps to add the library depends on which IDE you are using - Eclipse or Android-Studio.

If the jar file is not immediately available, then you might have to build that first from the github link you have.

Finally, on the githib, there is an examples folder. That should tell you exactly how to exercise the ListViewAnimations library.

HTH.




回答4:


The problem is with your adapter change with following and make me know that its working or not.

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CategoryListAdapter extends ArrayAdapter<HashMap<String, String>>
{
    private Context mContext;
    LayoutInflater inflater;
    private final ArrayList<HashMap<String, String>> urls;


    public CategoryListAdapter(Context context,int res, ArrayList<HashMap<String, String>> items) {
        super(context, res); 
        this.mContext = context;
        urls = items;
    }

    @Override
    public int getCount()
    {
        return urls.size();
    }
    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {HashMap<String, String> resultp = new HashMap<String, String>();
        ViewHolder holder = null;
        if (convertView == null)
        {
            LayoutInflater mInflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_row1, null);
            holder = new ViewHolder();
            holder.item_name = (TextView) convertView.findViewById(R.id.category_name);
            holder.item_count = (TextView) convertView.findViewById(R.id.songs_count);
            holder.item_id = (TextView) convertView.findViewById(R.id.category_id);     
            convertView.setTag(holder);

        }
        else holder = (ViewHolder) convertView.getTag();
        resultp = urls.get(position);
        holder.item_name.setText(resultp.get(CategoryActivity.TAG_NAME));
        holder.item_count.setText(resultp.get(CategoryActivity.TAG_CATEGORIES_COUNT));
        holder.item_id.setText(resultp.get(CategoryActivity.TAG_ID));

        return convertView;
    }

    static class ViewHolder
    {
        TextView item_name;
        TextView item_count;
        TextView item_id;
    }
}


来源:https://stackoverflow.com/questions/20463649/android-how-do-implement-listviewanimation-to-an-existing-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!