Android - 如何将ListViewAnimation实现到现有的ListView?

时间:2013-12-09 05:16:07

标签: android json android-listview

我有一个ListView,它的内容是从远程服务器填充的。这是一个简单的ListView,通过以下通常在博客上找到的教程创建。最近,我发现ListViewAnimation并希望在我的项目中使用它。

我已经有了Listview,现在如何在现有的库中实现listviewanimation库?

我试图在github上关注wiki,但仍无法通过它。

以下是我的ListView活动:

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);
                }
            });
        }
    }

}

这是活动的layout.xml

<?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>

正如您在代码中看到的那样,列表是自定义的,并由SimpleAdapter填充。

有人可以指导我如何在我的项目中实现ListViewAnimation库吗?


修改

这是我的新ListActivity。我修改了AsyncTask onPostExecute部分。现在的问题是没有显示任何东西。只是一个空白屏幕:

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上课:

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;
        }
}

4 个答案:

答案 0 :(得分:2)

你做过这个 -

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

答案 1 :(得分:1)

我认为您必须修复onPostExecute()。它运行在主线程或UI线程上。您不需要runOnUiThread()内的onPostExecute()

现在,我们将如何使用库部分:您将jar文件添加到libs文件夹下的项目中,并确保在构建apk时它也链接到了。添加库的步骤取决于您使用的IDE - Eclipse或Android-Studio。

如果jar文件没有立即可用,那么你可能必须先从你的github链接构建它。

最后,在githib上,有一个examples文件夹。这应该告诉你如何运用ListViewAnimations库。

HTH。

答案 2 :(得分:1)

您还可以尝试将布局动画设置为现有列表视图。 例如:

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);

这会将R.anim.fadeout动画设置为列表中的每个项目。

修改 试试这个

将此xml保存在anim文件夹中(这是输入效果)

<?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>

在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);

删除AnimationAdapterCode

答案 3 :(得分:1)

问题在于您的适配器更改了以下内容并让我知道它的工作与否。

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;
    }
}