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>
<?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
- Android自定义View之ListView条目滑动删除(仿QQ5.1)
- 自定义View之仿QQ消息滑动删除
- android自定义View之滑动删除
- android 滑动删除的listview(自定义view)
- android自定义listview滑动删除
- Android仿QQ实现ListView滑动删除
- Android仿QQ实现ListView滑动删除
- Android SwipeToDismiss:左右滑动删除ListView条目Item
- android自定义控件--条目可以左右滑动的listview
- Android自定义View之仿知乎滑动删除Activity
- 自定义View之列表滑动删除DEMO
- 自定义View——神之ViewDragHelper实现ListView滑动删除
- Android 自定义view 带滑动点击删除的的listview 简单版
- Android自定义View之ScrollViewforListView(解决ScrollView嵌套listview,listview不能滑动)
- 滑动删除--自定义ListView
- Android自定义view 滑动开关 支持左右滑动 适用于listview
- android自定义View之仿通讯录侧边栏滑动,实现A-Z字母检索
- android自定义View之仿通讯录侧边栏滑动,实现A-Z字母检索
- /etc/profile、~/.bashrc、/etc/bashrc、~/.bash_profile、 ~/.bash_login、 ~/.profile、/etc/environment
- Apache Maven 入门篇 ( 上 )
- 使用hive来分析flume收集的日志数据
- css3 动画 结合jquery 学习备忘
- Spring 整合 Redis
- Android自定义View之ListView条目滑动删除(仿QQ5.1)
- C#无法将顶级控件添加到控件
- The Repository Pattern (库模式)Example in C#[引文]
- UITextField使用全解
- 摘取网上关于Cocos2d—x的笔记
- mac查看硬盘使用情况
- Member Data和Inspector, Serialize的关系
- app-android中常见的四种布局
- 浏览器缓存详解:expires,cache-control,last-modified,etag详细说明