Android自定义View之ListView条目滑动删除(仿QQ5.1)

来源:互联网 发布:狮王轰炸软件下载 编辑:程序博客网 时间:2024/06/08 08:08

转载请注明出处:http://blog.csdn.net/joker_ya/article/details/40858501

最近一直在找工作,找不到工作让我很捉急。如果大家有什么好工作的可以介绍给我,在此就感谢大家了。


今天給大家带来的是仿QQ5.1条目的滑动删除效果,还是先看看效果吧!


其实呢!完成这样的效果很简单,这里我们使用github里的一个开源项目(SwipeMenuListView-master)就可以完成这样的效果了。文章末尾给出该开源项目的下载地址。


让我们一起动手来看看是怎么实现这样的效果的吧!首先我们将下载的开源项目(SwipeMenuListView-master)解压并import进Eclipse中。具体步骤为:点击File-->Import-->如下图:

点击next-->如下图:

最后点击Finish完成。


OK!开源项目导入完成,接下来我们就新建名为MySwipeMenu的Android项目:

然后我们就要导入那个开源项目的库了,具体做法如下:右键MySwipeMenu-->点击Properties-->点击Android-->点击Add-->选择刚刚导入的那个库-->一直OK!




好了,一切准备就绪就开始编写代码了。

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.myswipemenu.MainActivity" ><com.baoyz.swipemenulistview.SwipeMenuListView        android:id="@+id/listView"        android:layout_width="match_parent"        android:layout_height="match_parent" />    </RelativeLayout>


items_layout.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <ImageView         android:id="@+id/imageview"        android:layout_width="60dip"        android:layout_height="60dip"        android:scaleType="fitCenter"        />    <TextView         android:id="@+id/textview"        android:layout_width="match_parent"        android:layout_marginLeft="10dip"        android:layout_height="60dip"        android:layout_weight="1"        android:gravity="center_vertical"        android:textSize="34sp"        /></LinearLayout>


MainActivity.java:

package com.example.myswipemenu;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.baoyz.swipemenulistview.SwipeMenu;import com.baoyz.swipemenulistview.SwipeMenuCreator;import com.baoyz.swipemenulistview.SwipeMenuItem;import com.baoyz.swipemenulistview.SwipeMenuListView;import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;import android.support.v7.app.ActionBarActivity;import android.graphics.Color;import android.graphics.drawable.ColorDrawable;import android.os.Bundle;import android.util.TypedValue;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.SimpleAdapter;import android.widget.Toast;/** * 仿QQ5.1条目滑动删除效果 *  * @author Joker_Ya *  */public class MainActivity extends ActionBarActivity {// 获得图片资源列表private int[] images = new int[] { R.drawable.girl1, R.drawable.girl2,R.drawable.girl3, R.drawable.girl4, R.drawable.girl5,R.drawable.girl6, R.drawable.girl7, };private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();private Map<String, Object> map;private SwipeMenuListView mSwipeMenuListView;private SimpleAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mSwipeMenuListView = (SwipeMenuListView) findViewById(R.id.listView);adapter = new SimpleAdapter(this, getData(), R.layout.items_layout,new String[] { "img", "name" }, new int[] { R.id.imageview,R.id.textview });mSwipeMenuListView.setAdapter(adapter);// step 1. create a MenuCreatorSwipeMenuCreator creator = new SwipeMenuCreator() {@Overridepublic void create(SwipeMenu menu) {// TODO Auto-generated method stub// create "open" itemSwipeMenuItem openItem = new SwipeMenuItem(getApplicationContext());// set item backgroundopenItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,0xCE)));// set item widthopenItem.setWidth(dp2px(90));// set item titleopenItem.setTitle("Open");// set item title fontsizeopenItem.setTitleSize(18);// set item title font coloropenItem.setTitleColor(Color.WHITE);// add to menumenu.addMenuItem(openItem);// create "delete" itemSwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());// set item backgrounddeleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,0x3F, 0x25)));// set item widthdeleteItem.setWidth(dp2px(90));// set a icondeleteItem.setIcon(R.drawable.ic_delete);// add to menumenu.addMenuItem(deleteItem);}};// set creatormSwipeMenuListView.setMenuCreator(creator);// step 2. listener menutime click eventmSwipeMenuListView.setOnMenuItemClickListener(new OnMenuItemClickListener() {@Overridepublic void onMenuItemClick(int position, SwipeMenu menu,int index) {// TODO Auto-generated method stubswitch (index) {case 0:// click "OPEN"Toast.makeText(MainActivity.this,"You click \"OPEN\"", 3000).show();break;case 1:// click "DELETE"list.remove(position);adapter.notifyDataSetChanged();break;default:break;}}});// listview item click eventmSwipeMenuListView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "You click Girl" + position,3000).show();}});}// 模拟获得数据private List<Map<String, Object>> getData() {// TODO Auto-generated method stubfor (int i = 0; i < images.length; i++) {map = new HashMap<String, Object>();map.put("img", images[i]);map.put("name", "girl" + i);list.add(map);}return list;}// dp转换为pxprivate int dp2px(int dp) {return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,getResources().getDisplayMetrics());}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}

比较简单就不多说了。注释也比较清楚,而且那个开源项目有demo也有注释,大家不妨下来看看。


最后附上SwipeMenuListView-master下载地址:

SwipeMenuListView-master

源码下载



0 0
原创粉丝点击