0

I would like to add this slide in from left animation to my listView when I scroll

android:fromXDelta="100%p"
android:toXDelta="0"
android:duration = "500"
/>
<alpha
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="300"
    />

After adding it to my adapter

public class WordAdapter extends ArrayAdapter<Word> {

    public WordAdapter(Context context, ArrayList<Word> words) {
        super(context, 0, words);

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.activity_vocab, parent, false);
        }
        Word currentWord = getItem(position);

        TextView englishView =  listItemView.findViewById(R.id.englishWord);
        englishView.setText(currentWord.getmEnglishWord());

        TextView translatedView =  listItemView.findViewById(R.id.translatedWord);
        translatedView.setText(currentWord.getmTranslatedWordWord());

        TextView exampleView = listItemView.findViewById(R.id.exampleWord);
        exampleView.setText(currentWord.getmExample());

        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_left);
        convertView.startAnimation(animation);
        return listItemView;

    }
}

public class VocabActivity extends AppCompatActivity { private MediaPlayer mMediaPlayer;

private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        // Now that the sound file has finished playing, release the media player resources.
        releaseMediaPlayer();
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.word_list);


    final ArrayList<Word> words = new ArrayList<Word>();
    words.add(new Word("Abate", "To lessen the effect of something", "The storm has abated"));
    words.add(new Word("Abate", "To lessen the effect of something", "The storm has abated"));
    words.add(new Word("Abate", "To lessen the effect of something", "The storm has abated"));
    words.add(new Word("Abate", "To lessen the effect of something", "The storm has abated"));
    words.add(new Word("Abate", "To lessen the effect of something", "The storm has abated"));


    WordAdapter adapter = new WordAdapter(this, words);

    ListView listView = (ListView) findViewById(R.id.list);

    listView.setAdapter(adapter);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {


            Word word = words.get(position);


            mMediaPlayer = MediaPlayer.create(VocabActivity.this, word.getAudioResourceId());

            // Start the audio file
            mMediaPlayer.start();

            // Setup a listener on the media player, so that we can stop and release the
            // media player once the sound has finished playing.


        }
    });

}

This is the activity I'm trying to launch, just a simple one with some textviews and an mp3 file that I'll add later

9
  • 2
    Possible duplicate of What is a NullPointerException, and how do I fix it? Commented Mar 23, 2018 at 17:56
  • Your convertView is null for first time . Make it right . Commented Mar 23, 2018 at 17:58
  • show us the activity code Commented Mar 23, 2018 at 18:30
  • @MuneerahRashed I edited it in Commented Mar 23, 2018 at 19:47
  • @ADM The activity opens when I remove the lines for the animation. Would it open if the convertView was null? if so how could I change that? Commented Mar 23, 2018 at 19:48

1 Answer 1

0

I suggest , switch your code from using list view to recycle view here a lot of amazing resources to animate your list :

How to Animate Addition or Removal of Android ListView Rows

http://frogermcs.github.io/instamaterial-recyclerview-animations-done-right/

and if you still want to fix this bug , add more details about your layout , also provide the line of the error and the class

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.