2011-08-14 20 views
6

में सूची सूची एनिमेट करें मैं अपनी सूची दृश्य में नए आइटम एनिमेट करने का प्रयास करता हूं। मेरे पास स्थिर आईडी है, इसलिए मुझे पता है कि कौन सा तत्व एनिमेट करना है। समस्या ListView के रीसायकल तंत्र से आता है। जब मैं जानता हूं कि मुझे हाल ही में सम्मिलित तत्व मिला है तो मैं दृश्य पर एनीमेशन को कॉल करता हूं। लेकिन फिर, दृश्य को विभिन्न डेटा से भरा, पुनर्नवीनीकरण किया गया। यह गलत पंक्ति को एनिमेट करने वाले यूआई पर परिणाम देता है। कुछ बिंदु पर दृश्य सही डेटा धारण कर रहा था, लेकिन फिर पुनर्नवीनीकरण किया गया। मैंने लॉगकैट के माध्यम से इसकी पुष्टि की। क्या इसे हल करने का कोई तरीका है?ListView

संपादित करें:

public ExpensCursorAdapter(Context context, Cursor c, boolean autoRequery, 
     CopyOnWriteArraySet<String> fadeAnimateTags) { 
    super(context, c, autoRequery); 
    this.mFadeAnimTags = fadeAnimateTags; 
} 

@Override 
public boolean hasStableIds() { 
    return true; 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    setup(view, context, cursor); 
} 

private void setup(View view, Context context, Cursor cursor) { 
    final String id = cursor.getString(4); 
    if (LOCAL_LOGV) Log.v(TAG, String.format("Create item for %s. Received view: %s", id, view.toString())); 
    view.setTag(id); 
    final TextView dateText = (TextView) view.findViewById(R.id.date); 
    final TextView timeText = (TextView) view.findViewById(R.id.time); 
    final TextView title = (TextView) view.findViewById(R.id.title); 
    final TextView amount = (TextView) view.findViewById(R.id.amount); 
    final Date date = new Date(cursor.getLong(0)); 
    title.setText(cursor.getString(1)); 
    dateText.setText(dFormat.format(date)); 
    timeText.setText(tFormat.format(date)); 
    amount.setText(String.format("%d Ft", cursor.getInt(2))); 
    if (cursor.getInt(3) == 1) { 
     timeText.setTextColor(Color.LTGRAY); 
     title.setTextColor(Color.LTGRAY); 
     dateText.setTextColor(Color.LTGRAY); 
     amount.setTextColor(Color.LTGRAY); 
    } else { 
     timeText.setTextColor(Color.BLACK); 
     title.setTextColor(Color.BLACK); 
     dateText.setTextColor(Color.BLACK); 
     amount.setTextColor(Color.BLACK); 
    } 
    if (mFadeAnimTags.contains(id)) { 
    view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.fade)); 
    mFadeAnimTags.remove(id); 
    } 

} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    final LayoutInflater inflater = LayoutInflater.from(context); 
    View view = inflater.inflate(R.layout.expense_list_item, parent, false); 
    setup(view, context, cursor); 
    return view; 
} 
+0

आप कहाँ एनीमेशन शुरू कर रहे हैं? मुझे लगता है कि यह एडाप्टर की getView() विधि पर होना चाहिए। – manelizzard

+0

मैं वहां से शुरू करता हूं। प्रश्न में कोड जोड़ा गया। – gmate

+0

क्या आपने पंक्ति को "सेटअप" करते समय हर बार नई एनिमेशन ऑब्जेक्ट बनाने का प्रयास किया है? शायद क्योंकि आप एक स्थिर विधि का उपयोग कर रहे हैं, यह हमेशा एक ही एनीमेशन का संदर्भ देता है और यह एक पंक्ति से दूसरे में बदल जाता है। – manelizzard

उत्तर

5

चेतन अपने कस्टम एडाप्टर के getView विधि में प्रत्येक जोड़ा तत्व।

public View getView(int position, View convertView, ViewGroup parent) { 

    View v = convertView; 

    if (v == null) { 
     LayoutInflater vi = (LayoutInflater) getActivity() 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.simple_list_item_1, null); 
    } 

    ListData o = list.get(position); 
    TextView tt = (TextView) v.findViewById(R.id.toptext); 

    tt.setText(o.content); 

    Log.d("ListTest", "Position : "+position); 

    if(flag == false) { 
     Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_top_to_bottom); 
     v.startAnimation(animation); 
    } 
    return v; 
} 

और इस प्रकार एनीमेशन प्राप्त करता है।

+2

स्वीकार करें एडाप्टर –

+9

@ कॉर्निलत्सेवएनाटोली में कभी भी एनिमेट नहीं करें, तो कहां और कैसे? –

1

मुझे एक ही समस्या थी। मुझे 100% यकीन नहीं है कि यह जाने का सही तरीका है, लेकिन आप पुनर्नवीनीकरण दृश्य से किसी भी बचे हुए एनीमेशन को रद्द करने के लिए नया शुरू करने से पहले एनीमेशन को दृश्य पर साफ़ करने का प्रयास कर सकते हैं। इससे मेरी मदद की।

if(flag == false) { 
    v.clearAnimation(); 
    Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_top_to_bottom); 
    v.startAnimation(animation); 
} 
3

ListViewAnimations पुस्तकालय पर एक नज़र अपने ListView आइटम चेतन करने के लिए है।

से पहले::

MyListAdapter mAdapter = new MyListAdapter(this, getItems()); 
getListView().setAdapter(mAdapter); 

के बाद:

MyListAdapter mAdapter = new MyListAdapter(this, getItems()); 
AlphaInAnimationAdapter alphaInAnimationAdapter = new AlphaInAnimationAdapter(mAdapter); 
alphaInAnimationAdapter.setAbsListView(getListView()); 
getListView().setAdapter(alphaInAnimationAdapter); 
+0

+1 वास्तव में अच्छी लाइब्रेरी। छोटी चिंता, एक बार सूचीदृश्य लोड होने पर, मुझे जोड़ने की ज़रूरत है अतिरिक्त आइटम जोड़ने पर, सूची दृश्यों के लिए अतिरिक्त आइटम, मुझे उन वस्तुओं को एनिमेटेड होने की आवश्यकता है (जोड़ने के दौरान केवल नए जोड़े गए आइटम एनिमेट करें), क्या यह लाइब्रेरी में यह समर्थन है? –

4

आप getView का उपयोग करते हैं() सरल

मूल रूप से, आप केवल अपने आइटम एनिमेटेड है करने के लिए दो और लाइनों जोड़ना होगा यहां कोड, (कोई ज़रूरत नहीं कस्टम लाइब्रेरी)

private int lastPosition = -1; 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
//Load your view, populate it, etc... 
View view = ...; 

Animation animation = AnimationUtils.loadAnimation(getContext(), (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top); 
view.startAnimation(animation); 
lastPosition = position; 

return view; 
} 

up_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:shareInterpolator="@android:anim/decelerate_interpolator"> 
<translate 
    android:fromXDelta="0%" android:toXDelta="0%" 
    android:fromYDelta="100%" android:toYDelta="0%" 
    android:duration="400" /> 
</set> 

down_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:shareInterpolator="@android:anim/decelerate_interpolator"> 
<translate 
    android:fromXDelta="0%" android:toXDelta="0%" 
    android:fromYDelta="-100%" android:toYDelta="0%" 
    android:duration="400" /> 
</set> 

इस साइट से संदर्भ: http://kylewbanks.com/blog/Implementing-Google-Plus-Style-ListView-Animations-on-Android

+1

इस उत्तर को +1000 अपवॉट की आवश्यकता है ... – Noman