Back to project page divider.
The source code is released under:
Apache License
If you think the Android project divider listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package se.urvancevav.divider; // w w w . j a va2 s . c om /* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Currency; import java.util.Locale; import de.greenrobot.event.EventBus; public class ListViewFragment extends ListFragment { private ListViewAdapter listViewAdapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_listview, container, false); ListView listView = (ListView) rootView.findViewById(android.R.id.list); SwipeDismissListViewTouchListener touchListener = new SwipeDismissListViewTouchListener( listView, new SwipeDismissListViewTouchListener.DismissCallbacks() { @Override public boolean canDismiss(int position) { return true; } public void onDismiss(ListView listView, int[] reverseSortedPositions) { boolean isChanged = false; for (int position : reverseSortedPositions) { if (listViewAdapter.getCount() > position) { ListItem item = listViewAdapter.getItem(position); listViewAdapter.remove(item); isChanged = true; } } if (isChanged) { recalculateSums(); } } }); listView.setOnTouchListener(touchListener); listView.setOnScrollListener(touchListener.makeScrollListener()); return rootView; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); listViewAdapter = new ListViewAdapter(this.getActivity(), R.layout.list_view_row_layout); ListItem item = new ListItem(); item.setName("First"); listViewAdapter.add(item); item = new ListItem(); item.setName("Second"); listViewAdapter.add(item); setListAdapter(listViewAdapter); } @Override public void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Override public void onStop() { EventBus.getDefault().unregister(this); super.onStop(); } @SuppressWarnings("unused") public void onEvent(AddNewItemEvent event) { ListItem item = new ListItem(); item.setName("aa_bbb_" + listViewAdapter.getCount()); item.setPaidAmount(new BigDecimal(100)); listViewAdapter.add(item); recalculateSums(); //Toast.makeText(getActivity(), item.getName(), Toast.LENGTH_SHORT).show(); } private void recalculateSums() { BigDecimal total = new BigDecimal(0); // Recalculate total sum for (int idx = 0; idx < listViewAdapter.getCount(); idx++) { ListItem listItem = listViewAdapter.getItem(idx); total = total.add(listItem.getPaidAmount()); } BigDecimal meanValue = BigDecimal.ZERO; if (!listViewAdapter.isEmpty()) { meanValue = total.divide(new BigDecimal(listViewAdapter.getCount()), Currency.getInstance(Locale.getDefault()).getDefaultFractionDigits(), RoundingMode.HALF_EVEN); } for (int idx = 0; idx < listViewAdapter.getCount(); idx++) { ListItem listItem = listViewAdapter.getItem(idx); listItem.setReturnAmount(listItem.getPaidAmount().subtract(meanValue)); } listViewAdapter.notifyDataSetChanged(); } @Override public void onSaveInstanceState(Bundle outState) { } }