Animate the removal of a ListView item

后端 未结 4 1888
生来不讨喜
生来不讨喜 2021-01-30 05:33

I\'m attempting to animate the removal of a ListView item using this:

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Overr         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 06:02

    Idea: I start animation when the user selects the row. When the animation is complete i remove the row from the adapter.The following code animates the selected row:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        final ListView listView = (ListView) findViewById(R.id.listView1);
        final ArrayList values = new ArrayList();
        values.add("Android");
        values.add("iPhone");
        values.add("WindowsMobile");
        values.add("Blackberry");
        values.add("Windows7");
    
        final ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);
        listView.setAdapter(adapter);
    
        final Animation animation = AnimationUtils.loadAnimation(this,
                R.anim.slide_out);
        animation.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
    
            @Override
            public void onAnimationEnd(Animation animation) {
                adapter.remove(adapter.getItem(selectedRow));
                adapter.notifyDataSetChanged();
            }
        });
    
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view,
                    int position, long id) {
                Toast.makeText(getApplicationContext(),
                        " " + values.get(position), Toast.LENGTH_LONG).show();
                view.startAnimation(animation);
                selectedRow = position;
    
            }
    
        });
    }
    

    slide_out.xml file:

    
       
          
     
    

提交回复
热议问题