0

我从一个示例源中获得了这个示例,该示例在填充 listView 时显示动画,但我无法解释它并将其与我自己的 listView 代码集成。以下是示例代码。

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

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

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

我在填充我的 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

 <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

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

1 回答 1

0

我决定在我的列表视图动画中使用这个库,我的原因是它需要时间来设置。

com.haarman.listviewanimations.swinginadapters.prepared.AlphaInAnimationAdapter,我很佩服这个库但是需要很多时间来配置。所以我只用了 xml 动画。

我这个 tut 帮了我很多。ListView 动画

于 2013-08-02T03:34:18.073 回答