In the below example I have created a ListView , when user touch ListView row item then that particular row item will be remove dynamically. Here, first I have added Listview in activity_main.xml layout. In next step I have created row.xml layout here I have added a TextView.
Now see coding part In MainActivity I have used ArrayList in array list , In ArrayList I have added string names then I have used setOnItemClickListener method and Toast function, In next line I have created removeListItem method and here I have used AnimationUtils.loadAnimation to remove listitem. You can see below example code it clearly describe you "How to remove ListView Item dynamically using animation function in android".
Step(1)-actvity_main.xml layout-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainListView">
</ListView>
</LinearLayout>
Step(2)-Created a row.xml layout-
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
Step(3)-MainActivity-
public class MainActivity extends AppCompatActivity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
ArrayList<String> employee =
new ArrayList<String>(){
private static final long serialVersionUID = -1773393753338094625L;
{
add("Raj ");
add("Inder ");
add("Tarun");
add("Qazi");
add("Siva");
add("Sachin");
add("Sombir");
add("Om");
add("Sumit");
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainListView = (ListView) findViewById( R.id.mainListView );
listAdapter = new ArrayAdapter<String>(this, R.layout.row, employee);
mainListView.setAdapter( listAdapter );
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View rowView, int positon, long id) {
Toast.makeText(rowView.getContext(), "" + positon, Toast.LENGTH_LONG).show();
removeListItem(rowView, positon);
}
});
}
protected void removeListItem(View rowView, final int positon) {
final Animation animation = AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_out_right);
rowView.startAnimation(animation);
Handler handle = new Handler();
handle.postDelayed(new Runnable() {
@Override
public void run() {
employee.remove(positon);
listAdapter.notifyDataSetChanged();
animation.cancel();
}
},100);
}
}
0 Comment(s)