简体   繁体   English

在Android中显示带有动画的ListView

[英]Display ListView with Animation in Android

I got this example from a sample source, that displays animation in populating the listView but I can't be able to interpret and integrate it with my own code for the listView. 我从一个示例源获得了这个示例,该示例在填充listView时显示了动画,但是我无法将其解释并与我自己的listView代码集成。 Below are the sample codes. 下面是示例代码。

AlphaInActivity.java AlphaInActivity.java

import android.os.Bundle;
import android.widget.BaseAdapter;
import com.example.finaltestfordatabasemanip.MyListActivity;
import com.haarman.listviewanimations.swinginadapters.prepared.AlphaInAnimationAdapter;

public class AlphaInActivity extends MyListActivity {

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

BaseAdapter mAdapter = createListAdapter();

AlphaInAnimationAdapter aplhaInAnimationAdapter = new AlphaInAnimationAdapter(mAdapter);
aplhaInAnimationAdapter.setAbsListView(getListView());

getListView().setAdapter(aplhaInAnimationAdapter);
}
}

MyListActivity.java MyListActivity.java

import java.util.ArrayList;
import com.haarman.listviewanimations.ArrayAdapter;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyListActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setDivider(null);
}

protected ArrayAdapter<String> createListAdapter() {
return new MyListAdapter(this, getItems());
}

public static ArrayList<String> getItems() {
ArrayList<String> items = new ArrayList<String>();
for (int i = 0; i < 1000; i++) {
items.add(String.valueOf(i));
}
return items;
}

private static class MyListAdapter extends ArrayAdapter<String> {

private Context mContext;

public MyListAdapter(Context context, ArrayList<String> items) {
super(items);
mContext = context;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = (TextView) convertView;
if (tv == null) {
tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_row, parent, false);
}
tv.setText("This is row number " + getItem(position));
return tv;
}
}
}

list_row.xml list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:minHeight="48dp"
    android:textSize="20sp" />

My Code in Populating my ListView: 填充ListView中的代码:

JSONArray products = null;
ArrayList<HashMap<String, String>> productsList;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
                 // Hashmap for ListView
                    productsList = new ArrayList<HashMap<String, String>>();
                    //bMain = (Button) findViewById(R.id.main);

                    // Loading products in Background Thread
                    new LoadAllProducts().execute();

                // Get listview
                ListView lv = getListView();


                // on seleting single product
                // launching Edit Product Screen
                lv.setOnItemClickListener(new OnItemClickListener() {});

class LoadAllProducts extends AsyncTask<String, String, String> {
/**
         * getting All products from url
         * */
        protected String doInBackground(String... args) {

String id = c.getString(TAG_PID);
                        String name = c.getString(TAG_NAME);
                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

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


                        // adding HashList to ArrayList
                        productsList.add(map);

                }
protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        MainActivity.this, productsList,
                        R.layout.list_item, new String[] { TAG_PID,
                                TAG_NAME},
                        new int[] { R.id.pid, R.id.name });
                // updating listview
                setListAdapter(adapter);
            }
        });

    }
}

list_item.xml list_item.xml

 <TextView
        android:id="@+id/pid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
 />
    <!-- Weather Information-->
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

activity_main3.xml activity_main3.xml

<ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content" 
        android:listSelector="@drawable/list_selector">
</ListView>

I decided to to use this library in my listview animation, my reason is it will took time to be set up. 我决定在我的listview动画中使用该库,原因是设置起来需要时间。

com.haarman.listviewanimations.swinginadapters.prepared.AlphaInAnimationAdapter , I admire this library but needs a lot of time to configure. com.haarman.listviewanimations.swinginadapters.prepared.AlphaInAnimationAdapter ,我很欣赏这个库,但是需要很多时间来配置。 So I moved out with just xml animations. 因此,我只使用了xml动画。

I this tut helped me alot. 我这个孩子帮助了我很多。 ListView animations ListView动画

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM