Android आपको पहले से लोड किया गया ऐनिमेशन देता है, जो लेआउट बदलने पर चलता है. एट्रिब्यूट को Android सिस्टम को इन लेआउट बदलावों को ऐनिमेट करने के लिए बताता है. साथ ही, यह सिस्टम की डिफ़ॉल्ट सेटिंग को लागू करता है आपके लिए ऐनिमेशन.
यहां बताया गया है कि किसी सूची में आइटम जोड़ते समय डिफ़ॉल्ट लेआउट ऐनिमेशन कैसा दिखता है:
लेआउट बनाएं
अपनी गतिविधि के लेआउट की एक्सएमएल फ़ाइल में, android:animateLayoutChanges
एट्रिब्यूट सेट करें
true
पर उस लेआउट के लिए जिसके लिए आपको ऐनिमेशन चालू करना है:
<LinearLayout android:id="@+id/container"
android:animateLayoutChanges="true"
...
/>
लेआउट में आइटम जोड़ना, अपडेट करना या हटाना
लेआउट में आइटम जोड़ें, हटाएं या अपडेट करें और आइटम अपने-आप ऐनिमेट हो जाते हैं:
lateinit var containerView: ViewGroup
...
private fun addItem() {
val newView: View = ...
containerView.addView(newView, 0)
}
private ViewGroup containerView;
...
private void addItem() {
View newView;
...
containerView.addView(newView, 0);
}