这是一个使用NineOldAndroids实现仿微信listview左滑出现删除itembutton效果。使用的是Jake Wharton的动画开源库NineOldAndroids。在API3.0(Honeycomb), SDK新增了一个android.animation包,里面的类是实现动画效果相关的类,通过Honeycomb API,能够实现非常复杂的动画效果,但是如果开发者想在3.0以下使用这一套API, 则需要使用开源框架Nine Old Androids,在这个库中会根据我们运行的机器判断其SDK版本,如果是API3.0以上则使用Android自带的动画类,否则就使用Nine Old Androids库中,这是一个兼容库。而在这里实现的是使用NineOldAndroids这个库,实现这种效果。这个库的下载地址:点击下载。
本文项目源码地址:点击下载
下面来看看部分代码是怎样实现的。
一、先看最终的效果图
二、使用这个listview的适配器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(); } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } @Override public 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() { @Override public 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; } }三、使用这个适配器的一些接口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; } }四、怎么调用上面的接口以及适配器,返回的接口。以及数据源的添加。看以下MainActivity
package com.main.listviewsideslip; import java.util.Collections; import java.util.LinkedList; 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 LinkedList<RecentItem> mRecentDatas; private SwipeListView mRecentListView; private TextView mEmpty; @Override protected void onCreate(Bundle savedInstanceState) { super.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() { @Override public void onClickFrontView(int position) { } @Override public void onClickBackView(int position) { mRecentListView.closeOpenedItems();// 关闭打开的项 } @Override public void onDismiss(int[] reverseSortedPositions) { for (int position : reverseSortedPositions) { mAdapter.remove(position); } } }); } //数据源 public LinkedList<RecentItem> getRecentList() { LinkedList<RecentItem> list = new LinkedList<RecentItem>(); nameArray = getResources().getStringArray(R.array.jazzy_effects_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; } 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 = { 21,67,65,8,54,67,23,78,98,54,67,23,67,423,23,67,90,12,34,56,978}; public static String[] nameArray = new String[21]; }其他动画效果怎么弄出来的代码就不贴,包括怎样进行手势的控制。有兴趣下载本文的项目认真查看。xml文件也不贴出来。全部贴有点多。这里只说怎么使用这个库,相关的适配器,接口。实现的效果在本文的最上面。虽然在上面的注释写得很少。我相信使用继承BaseAdapter较多开发者,都能看清楚这个流程。如果新手看不懂,怎样使用这个开源库,请在下面留言。我会耐心解答。
版权声明:本文为博主原创文章,未经博主允许不得转载。