Android uses NineOldAndroids to implement the function of removing itembutton and androidlistview when the imitation WeChat listview is Slide left.

Source: Internet
Author: User

Android uses NineOldAndroids to implement a left-slide display like listview to delete itembutton, androidlistview

This example uses NineOldAndroids to implement the itembutton deletion effect on the left slide of the imitation listview. We use the open-source library NineOldAndroids for animation of Jake Wharton. In API3.0 (Honeycomb), the SDK adds an android. the class in the animation package is related to the implementation of animation effects. The Honeycomb API can be used to achieve very complex animation effects. However, if a developer wants to use this API in less than 3.0, we need to use the open-source framework Nine Old Androids. In this library, we will judge the SDK version based on the running machine. If it is API3.0 or above, we will use the animation class that comes with Android, otherwise, use the Nine Old Androids library, which is a compatible library. Here, the NineOldAndroids library is used to achieve this effect. Click to download the library.

Source code of this article: Click to download

Let's take a look at how some code is implemented.

I. first look at the final


2. Use the listview adapter RecentAdapter

package com.listview.adapter;import java.util.LinkedList;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;import com.main.entity.RecentItem;import com.main.listviewsideslip.R;import com.view.listview.SwipeListView;public class RecentAdapter extends BaseAdapter {private LayoutInflater mInflater;private LinkedList<RecentItem> mData;private SwipeListView mListView;private Context mContext;public RecentAdapter(Context context, LinkedList<RecentItem> data,SwipeListView listview) {mContext = context;this.mInflater = LayoutInflater.from(mContext);mData = data;this.mListView = listview;}public void remove(int position) {if (position < mData.size()) {mData.remove(position);notifyDataSetChanged();}}public void remove(RecentItem item) {if (mData.contains(item)) {mData.remove(item);notifyDataSetChanged();}}public void addFirst(RecentItem item) {if (mData.contains(item)) {mData.remove(item);}mData.addFirst(item);notifyDataSetChanged();}@Overridepublic int getCount() {return mData.size();}@Overridepublic Object getItem(int position) {return mData.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {final RecentItem item = mData.get(position);ViewHolder vHolder = null;if (convertView == null) {convertView = mInflater.inflate(R.layout.recent_listview_item, null);vHolder = new ViewHolder();vHolder.nameTV = (TextView) convertView.findViewById(R.id.recent_list_item_name);vHolder.numTV = (TextView) convertView.findViewById(R.id.unreadmsg);vHolder.headIV = (ImageView) convertView.findViewById(R.id.icon);vHolder.deleteBtn = (Button) convertView.findViewById(R.id.recent_del_btn);convertView.setTag(vHolder);}else {vHolder  = (ViewHolder) convertView.getTag();}vHolder.nameTV.setText(item.getName());vHolder.headIV.setImageResource(item.getHeadImg());int num = item.getNewNum();if (num > 0) {vHolder.numTV.setVisibility(View.VISIBLE);vHolder.numTV.setText(num + "");} else {vHolder.numTV.setVisibility(View.GONE);}vHolder.deleteBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {mData.remove(position);notifyDataSetChanged();if (mListView != null)mListView.closeOpenedItems();}});return convertView;}class ViewHolder{public TextView nameTV;public TextView numTV;public ImageView headIV;public Button deleteBtn;}}
Iii. BaseSwipeListViewListener

package com.view.listview;public abstract class BaseSwipeListViewListener implements SwipeListViewListener {    @Override    public void onOpened(int position, boolean toRight) {    }    @Override    public void onClosed(int position, boolean fromRight) {    }    @Override    public void onListChanged() {    }    @Override    public void onMove(int position, float x) {    }    @Override    public void onStartOpen(int position, int action, boolean right) {    }    @Override    public void onStartClose(int position, boolean right) {    }    @Override    public abstract void onClickFrontView(int position);    @Override    public abstract void onClickBackView(int position);    @Override    public abstract void onDismiss(int[] reverseSortedPositions);    @Override    public int onChangeSwipeMode(int position) {        return SwipeListView.SWIPE_MODE_DEFAULT;    }}
4. How to call the above interface and the interface returned by the adapter. And add a data source. See the following MainActivity

Package com. main. listviewsideslip; import java. util. collections; import java. util. using list; import com. listview. adapter. recentAdapter; import com. main. entity. recentItem; import com. view. listview. baseSwipeListViewListener; import com. view. listview. swipeListView; import android. OS. bundle; import android. widget. textView; import android. app. activity; public class MainActivity extends Activity {private RecentAdapter mAdapter; private jsonlist <RecentItem> mRecentDatas; private SwipeListView mRecentListView; private TextView mEmpty; @ jsonvoid onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView (savedInstanceState);} private void initView (Bundle savedInstanceState) {mRecentListView = (SwipeListView) findViewById (R. id. recent_listview); mRecentDatas = getRecentList (); mAdapter = new RecentAdapter (this, mRecentDatas, mRecentListView); mRecentListView. setAdapter (mAdapter); mEmpty = (TextView) findViewById (R. id. empty); mRecentListView. setEmptyView (mEmpty); mRecentListView. setSwipeListViewListener (new BaseSwipeListViewListener () {@ Overridepublic void onClickFrontView (int position) {}@ Overridepublic void onClickBackView (int position) {mRecentListView. closeOpenedItems (); // close the opened item} @ Overridepublic void onDismiss (int [] reverseSortedPositions) {for (int position: reverseSortedPositions) {mAdapter. remove (position) ;}}});} // public region list of the data source <RecentItem> getRecentList () {region list <RecentItem> list = new region list <RecentItem> (); nameArray = getResources (). getStringArray (R. array. jazzy_javasts_ch); for (int I = 0; I <20; I ++) {int icon = heads [I]; String name = nameArray [I]; int num = numArray [I]; RecentItem item = new RecentItem (name, icon, num); list. add (item);} Collections. sort (list); // return list in descending order;} public static int [] heads = {R. drawable. h0, R. drawable. h1, R. drawable. h2, R. drawable. h3, R. drawable. h4, R. drawable. h5, R. drawable. h6, R. drawable. h7, R. drawable. h8, R. drawable. h9, R. drawable. h10, R. drawable. h11, R. drawable. h12, R. drawable. h13, R. drawable. h14, R. drawable. h15, R. drawable. h16, R. drawable. h17, R. drawable. h18, R. drawable. h19, R. drawable. h20, R. drawable. h21}; public static int [] numArray = {67,423, 978 }; public static String [] nameArray = new String [21];}
The code for how other animation effects are obtained is not pasted, including how to control gestures. If you are interested in downloading this article, carefully check the project. Xml files are not pasted. All posts have more points. This document only describes how to use this library, related adapters, and interfaces. The implementation result is at the top of this article. Although the above comments are rarely written. I believe that many developers who inherit BaseAdapter can see this process clearly. If you cannot understand how to use this open-source library, leave a message below. I will answer your questions patiently.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.