Android的 - 如何实现ListViewAnimation到现有的ListView? [英] Android - How do implement ListViewAnimation to an existing ListView?

查看:153
本文介绍了Android的 - 如何实现ListViewAnimation到现有的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它的内容是从远程服务器填充一个ListView。这是按照你平时在博客上找到的教程创建了一个简单的ListView。最近,我发现了 ListViewAnimation ,并想在我的项目中使用它。

我已经有一个列表视图,现在我该如何实现在现有的listviewanimation库?

我试图按照维基在GitHub上,但仍然couldn' ŧ使通过它。

下面是我的的ListView 活动:

 公共类CategoryActivity扩展ListActivity {
    公共无效的onCreate(捆绑savedInstanceState){
        的setContentView(R.layout.activity_category);        ListView控件LV = getListView();
        lv.setOnItemClickListener(新android.widget.AdapterView.OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>为arg0,观景,
                    INT ARG2,长ARG3){                单击项目//时,去其相应的细节
                意图I =新意图(getApplicationContext()
                        ItemListActivity.class);
                字符串CATEGORY_ID =((的TextView)查看
                        .findViewById(R.id.category_id))。gettext的()
                        的ToString();
                i.putExtra(CATEGORY_ID,CATEGORY_ID);
                startActivity(ⅰ);
            }
        });        新LoadCategories()执行();
    }    类LoadCategories扩展的AsyncTask<字符串,字符串,字符串> {        @覆盖
        保护字符串doInBackground(字符串参数... args){
            //大厦参数
            清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();            // URL从获取JSON字符串
            JSON字符串= jsonParser.makeHtt prequest(Constants.URL_CATEGORY,GET,
                    PARAMS);            //检查JSON效应初探日志的猫
            Log.d(分类JSON:,>中+ JSON);            尝试{
                类别=新JSONArray(JSON);                如果(类!= NULL){
                    //通过所有专辑循环
                    的for(int i = 0; I< categories.length();我++){
                        JSONObject的C = categories.getJSONObject(I)                        //存储在变量中的每个JSON项值
                        字符串ID = c.getString(TAG_ID);
                        字符串名称= c.getString(TAG_NAME);
                        字符串songs_count = c.getString(TAG_CATEGORIES_COUNT);                        //创建新的HashMap
                        HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();                        //将每个子节点的HashMap键=>值
                        map.put(TAG_ID,身份证);
                        map.put(TAG_NAME,名);
                        map.put(TAG_CATEGORIES_COUNT,songs_count);                        //添加HashList到ArrayList的
                        categoryList.add(地图);
                    }
                }其他{
                    Log.d(分类,空);
                }            }赶上(JSONException E){
                e.printStackTrace();
            }            返回null;
        }
        @覆盖
        保护无效onPostExecute(字符串FILE_URL){
            runOnUiThread(新的Runnable(){
                公共无效的run(){                    //填充JSON数据的每一行
                    ListAdapter适配器=新SimpleAdapter(
                            CategoryActivity.this,所属分类,
                            R.layout.custom_list_category,新的String [] {
                                    TAG_ID,TAG_NAME,TAG_CATEGORIES_COUNT},
                            新的INT [] {R.id.category_id,R.id.category_name,
                                    R.id.songs_count});                    setListAdapter(适配器);
                }
            });
        }
    }}

这是 layout.xml 的活动:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>    < ListView控件
        机器人:ID =@机器人:ID /列表
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:cacheColorHint =#00000000
        机器人:dividerHeight =15dp
        机器人:footerDividersEnabled =真
        机器人:headerDividersEnabled =真/>
< / LinearLayout中>

正如你可以在code看到,该列表定制,由SimpleAdapter填充。

任何人都可以指导我如何落实ListViewAnimation库到我的项目吗?


修改

下面是我的新ListActivity。我已经修改了AsyncTask的onPostExecute一部分。现在的问题是不显示任何内容。只是一个空白屏幕:

 公共类CategoryActivity扩展ListActivity {
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);            的setContentView(R.layout.activity_category);
            //检查该用户登录敬酒
            。为userDetails = db.getUserDetails()获取(名称)的toString()。            的setTitle(为userDetails);            //为哈希映射的ListView
            所属分类=新的ArrayList<&HashMap的LT;字符串,字符串>>();            新LoadCategories()执行();            //获取列表视图
            ListView控件LV = getListView();            // LV =(ListView控件)findViewById(R.id.category_list);
            lv.setDivider(NULL);            lv.setOnItemClickListener(新android.widget.AdapterView.OnItemClickListener(){
                @覆盖
                公共无效onItemClick(适配器视图<>为arg0,观景,
                        INT ARG2,长ARG3){
                    意图I =新意图(getApplicationContext()
                            ItemListActivity.class);                    字符串CATEGORY_ID =((的TextView)查看
                            .findViewById(R.id.category_id))。gettext的()
                            的ToString();
                    i.putExtra(CATEGORY_ID,CATEGORY_ID);                    startActivity(ⅰ);
                }
            });
    }    类LoadCategories扩展的AsyncTask<字符串,字符串,字符串> {
        @覆盖
        在preExecute保护无效(){
            super.on preExecute();
            pDialog =新ProgressDialog(CategoryActivity.this);
            pDialog.setMessage(「上市类别...);
            pDialog.setIndeterminate(假);
            pDialog.setCancelable(假);
            pDialog.show();
        }        @覆盖
        保护字符串doInBackground(字符串参数... args){
            //大厦参数
            清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();            // URL从获取JSON字符串
            JSON字符串= jsonParser.makeHtt prequest(Constants.URL_CATEGORY,GET,
                    PARAMS);            //检查JSON效应初探日志的猫
            Log.d(分类JSON:,>中+ JSON);            返回JSON;
        }        @覆盖
        保护无效onPostExecute(JSON字符串){
            尝试{
                类别=新JSONArray(JSON);                如果(类!= NULL){
                    //通过所有专辑循环
                    的for(int i = 0; I< categories.length();我++){
                        JSONObject的C = categories.getJSONObject(I)                        //存储在变量中的每个JSON项值
                        字符串ID = c.getString(TAG_ID);
                        字符串名称= c.getString(TAG_NAME);
                        字符串songs_count = c.getString(TAG_CATEGORIES_COUNT);                        //创建新的HashMap
                        HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();                        //将每个子节点的HashMap键=>值
                        map.put(TAG_ID,身份证);
                        map.put(TAG_NAME,名);
                        map.put(TAG_CATEGORIES_COUNT,songs_count);                        //添加HashList到ArrayList的
                        categoryList.add(地图);
                    }                    mAdapter =新CategoryListAdapter(CategoryActivity.this,所属分类);
                    AnimationAdapter animAdapter =新AlphaInAnimationAdapter(mAdapter);
                    animAdapter.setAbsListView(getListView());
                    getListView()setAdapter(animAdapter)。                    //让所有专辑后,关闭该对话框
                    pDialog.dismiss();
                }其他{
                    Log.d(分类,空);
                }            }赶上(JSONException E){
                e.printStackTrace();
            }
        }
    }}

CategoryListAdapter 类:

 公共类CategoryListAdapter扩展ArrayAdapter<&HashMap的LT;字符串,字符串>> {
        私人语境mContext;
        LayoutInflater吹气;
        私人最终的ArrayList<&HashMap的LT;字符串,字符串>>网址;
        HashMap的<字符串,字符串> resultp =新的HashMap<字符串,字符串>();        公共CategoryListAdapter(上下文的背景下,ArrayList的<&HashMap的LT;字符串,字符串>>项目){
            / *超(项目);
            mContext =背景; * /
            this.mContext =背景;            网址=物品;
        }        @覆盖
        众长getItemId(INT位置){
            返回的getItem(位置).hash code();
        }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            TextView的ITEM_NAME;
            TextView的ITEM_COUNT;
            TextView的ITEM_ID;            吹气=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            查看查看= inflater.inflate(R.layout.custom_list_category,父母,假);            resultp = urls.get(位置);            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));            返回视图。
        }
}


解决方案

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

  AnimationSet设置=新AnimationSet(真);
动画动画= AnimationUtils.loadAnimation(的getContext()
            R.anim.fadeout);
set.addAnimation(动画);
LayoutAnimationController控制器=新LayoutAnimationController(
        集,0.5F);
< Your_list_view> .setLayoutAnimation(控制器);

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

修改
试试这个

保存动画文件夹这个XML(这是补料有效)

 <?XML版本=1.0编码=UTF-8&GT?;
<设置的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>    <阿尔法
        机器人:时间=500
        机器人:fromAlpha =0.0
        机器人:插值=@机器人:动画/ accelerate_interpolator
        机器人:toAlpha =1.0/>< /集>

和在CreateView的

 的ListView LV = getListView();AnimationSet套=新AnimationSet(真);
动画动画= AnimationUtils.loadAnimation(的getContext()
            R.anim.fadein);
set.addAnimation(动画);
LayoutAnimationController控制器=新LayoutAnimationController(
        集,0.5F);
lv.setLayoutAnimation(控制器);

和删除AnimationAdapter code

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

解决方案

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

这篇关于Android的 - 如何实现ListViewAnimation到现有的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆