今天使用了android-swipelistview这个开源库,其实就是我们以前微信 QQ上面的侧滑删除效果,删除一条item后,后面的补上来。项目地址:https://github.com/47deg/android-swipelistview 这次除了下载该项目,还要下载swipelistview的依赖库NineOldAndroids
地址:https://github.com/JakeWharton/NineOldAndroids/downloads,将jar文件导入到eclipse里的项目里
下面上我的效果实现图:
如图上所示,我侧滑删除了杭州这个地点之后,后面的上海变成了第一条。
贴代码:
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/list_bg"
tools:context="com.sdf.swipelistviewdemo.MainActivity" >
<com.sdf.swipelistviewdemo.swipelistview.SwipeListView
android:id="@+id/currentCityList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:background="@drawable/shape_city_bg"
android:listSelector="#00000000"
swipe:swipeBackView="@+id/back"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeDrawableChecked="@drawable/choice_selected"
swipe:swipeDrawableUnchecked="@drawable/choice_unselected"
swipe:swipeFrontView="@+id/front"
swipe:swipeMode="both" >
</com.sdf.swipelistviewdemo.swipelistview.SwipeListView>
</RelativeLayout>
适配器item的布局 item_city.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="back"
>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"/>
<TextView
android:id="@+id/deleteCity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/comm_3"
android:padding="20dp"
android:textColor="@color/white"
android:textSize="18sp"
android:gravity="center"
android:text="@string/delete"/>
</LinearLayout>
<TextView
android:id="@+id/front"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/item_color"
android:padding="20dp"
android:textColor="@color/white"
android:textSize="18sp"
android:gravity="left"
android:tag="front"
android:text="北京"/>
</RelativeLayout>
Java代码:
public class MainActivity extends Activity {
private CityAdapter adapter;
private SwipeListView mListViewMyCity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListViewMyCity = (SwipeListView) findViewById(R.id.currentCityList);
adapter = new CityAdapter(getApplicationContext());
swipeListviewSetting();
mListViewMyCity.setAdapter(adapter);
}
/**
* 侧滑listview设置
*/
private void swipeListviewSetting() {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screen = size.x;
// 滑动模式为仅支持左滑
mListViewMyCity.setSwipeMode(SwipeListView.SWIPE_MODE_LEFT);
mListViewMyCity.setSwipeActionLeft(0);
mListViewMyCity.setSwipeActionRight(0);
mListViewMyCity.setOffsetLeft(screen / 4 * 3);
mListViewMyCity.setOffsetRight(0);
mListViewMyCity.setAnimationTime(0);
// 支持长按自动滑动
mListViewMyCity.setSwipeOpenOnLongPress(true);
}
适配器dapter代码:
ublic class CityAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private List<Map<String, Object>> data;
public CityAdapter(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
init();
}
public void init() {
data = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map = new HashMap<String, Object>();
map.put("place", "北京");
data.add(map);
map = new HashMap<String, Object>();
map.put("place", "杭州");
data.add(map);
map = new HashMap<String, Object>();
map.put("place", "上海");
data.add(map);
map = new HashMap<String, Object>();
map.put("place", "台湾");
data.add(map);
map = new HashMap<String, Object>();
map.put("place", "香港");
data.add(map);
map = new HashMap<String, Object>();
map.put("place", "钓鱼岛");
data.add(map);
map = new HashMap<String, Object>();
map.put("place", "日本");
data.add(map);
map = new HashMap<String, Object>();
map.put("place", "广东");
data.add(map);
map = new HashMap<String, Object>();
map.put("place", "深圳");
data.add(map);
map = new HashMap<String, Object>();
map.put("place", "嘉兴");
data.add(map);
map = new HashMap<String, Object>();
map.put("place", "天津");
data.add(map);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_mycity, null);
viewHolder = new ViewHolder();
viewHolder.textViewTitle = (TextView) convertView
.findViewById(R.id.front);
viewHolder.textViewDelete = (TextView) convertView
.findViewById(R.id.deleteCity);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textViewTitle.setText(data.get(position).get("place")
.toString());
viewHolder.textViewDelete.setText("删除");
viewHolder.textViewDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
data.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
class ViewHolder {
TextView textViewTitle;
TextView textViewDelete;
}
}
难度不大,应该都能看得懂==,这次的"晴天"天气app的开源库逐步学习到这里算是个结尾了,当然这个app里面还有很多东西需要去学习,我也是拿这个app的一些开源库熟悉熟悉。Android之路还很远,我只是一只菜鸟,还需不断学习与磨练!与君共勉,博客还会更新下去。
还有很多开源项目值得去学习,因为以后开发的时候会用到,很重要!!!比如Volley(网络的传输与异步下载等等),Android-async-http,aFinal框架,Android-Universal-Image-Loader(异步图片下载,缓存及显示)....很多很多,就这样了,自己也是小白,以后会慢慢接触学习。
例子源码:http://download.csdn.net/detail/u011388551/8763543