Skip to content

Commit fe3070e

Browse files
committed
能根据listview的数据动态改变SwipeMenuItem的值
1 parent 4e06cc4 commit fe3070e

File tree

9 files changed

+414
-155
lines changed

9 files changed

+414
-155
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.baoyz.swipemenulistview.demo;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.baoyz.swipemenulistview.SwipeMenu;
7+
import com.baoyz.swipemenulistview.SwipeMenuCreator;
8+
import com.baoyz.swipemenulistview.SwipeMenuItem;
9+
import com.baoyz.swipemenulistview.SwipeMenuListView;
10+
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
11+
import com.example.swipelistview.R;
12+
import com.example.swipelistview.R.id;
13+
import com.example.swipelistview.R.layout;
14+
15+
import android.app.Activity;
16+
import android.graphics.Color;
17+
import android.graphics.drawable.ColorDrawable;
18+
import android.os.Bundle;
19+
import android.view.Menu;
20+
import android.view.MenuItem;
21+
22+
public class DynamicActivity extends Activity {
23+
public SwipeMenuListView listview;
24+
List<User> values = new ArrayList<User>();
25+
26+
@Override
27+
protected void onCreate(Bundle savedInstanceState) {
28+
super.onCreate(savedInstanceState);
29+
setContentView(R.layout.test);
30+
listview = (SwipeMenuListView) this.findViewById(R.id.listView);
31+
for (int i = 0; i < 6; i++) {
32+
User user=new User();
33+
user.setName("1");
34+
values.add(user);
35+
}
36+
for (int j = 0; j < 3; j++) {
37+
User user=new User();
38+
user.setName("2");
39+
values.add(user);
40+
}
41+
SwipeMenuCreator creator = new SwipeMenuCreator() {
42+
43+
@Override
44+
public void create(SwipeMenu menu) {
45+
// create "open" item
46+
SwipeMenuItem openItem = new SwipeMenuItem(
47+
getApplicationContext());
48+
// set item background
49+
openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
50+
0xCE)));
51+
// set item width
52+
openItem.setWidth(90);
53+
// set item title
54+
openItem.setTitle("收藏");
55+
// set item title fontsize
56+
openItem.setTitleSize(18);
57+
// set item title font color
58+
openItem.setTitleColor(Color.WHITE);
59+
// add to menu
60+
menu.addMenuItem(openItem);
61+
// create "delete" item
62+
SwipeMenuItem deleteItem = new SwipeMenuItem(
63+
getApplicationContext());
64+
// set item background
65+
deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
66+
0x3F, 0x25)));
67+
// set item width
68+
deleteItem.setWidth(90);
69+
// add to menu
70+
menu.addMenuItem(deleteItem);
71+
}
72+
};
73+
listview.setMenuCreator(creator);
74+
final MyAdapter adapter = new MyAdapter(this, values);
75+
listview.setAdapter(adapter);
76+
listview.setOnMenuItemClickListener(new OnMenuItemClickListener(){
77+
78+
@Override
79+
public boolean onMenuItemClick(int position, SwipeMenu menu,
80+
int index) {
81+
// TODO Auto-generated method stub
82+
String name= ((User)adapter.getItem(position)).getName();
83+
if(name.equals("1")){
84+
((User)adapter.getItem(position)).setName("2");
85+
}
86+
if(name.equals("2")){
87+
((User)adapter.getItem(position)).setName("1");
88+
}
89+
adapter.notifyDataSetChanged();
90+
return false;
91+
}
92+
93+
});
94+
95+
}
96+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.baoyz.swipemenulistview.demo;
2+
3+
import java.util.List;
4+
5+
import com.baoyz.swipemenulistview.BaseSwipListAdapter;
6+
import com.baoyz.swipemenulistview.SwipeMenu;
7+
import com.baoyz.swipemenulistview.SwipeMenuItem;
8+
import com.example.swipelistview.R;
9+
import com.example.swipelistview.R.id;
10+
import com.example.swipelistview.R.layout;
11+
12+
import android.content.Context;
13+
import android.view.LayoutInflater;
14+
import android.view.View;
15+
import android.view.ViewGroup;
16+
import android.widget.BaseAdapter;
17+
import android.widget.TextView;
18+
19+
public class MyAdapter extends BaseSwipListAdapter {
20+
public List<User> list = null;
21+
public Context context;
22+
23+
public MyAdapter(Context cxt, List<User> values) {
24+
this.context = cxt;
25+
list = values;
26+
}
27+
28+
@Override
29+
public int getCount() {
30+
// TODO Auto-generated method stub
31+
return list.size();
32+
}
33+
34+
@Override
35+
public Object getItem(int position) {
36+
// TODO Auto-generated method stub
37+
return list.get(position);
38+
}
39+
40+
@Override
41+
public long getItemId(int position) {
42+
// TODO Auto-generated method stub
43+
return 0;
44+
}
45+
46+
@Override
47+
public View getView(int position, View convertView, ViewGroup parent) {
48+
// TODO Auto-generated method stub
49+
ViewHolder holder = null;
50+
if (convertView == null) {
51+
convertView = LayoutInflater.from(context).inflate(R.layout.item,
52+
null);
53+
holder=new ViewHolder();
54+
holder.tx = (TextView) convertView.findViewById(R.id.tip);
55+
56+
convertView.setTag(holder);
57+
} else {
58+
holder = (ViewHolder) convertView.getTag();
59+
}
60+
holder.tx.setText(list.get(position).getName());
61+
return convertView;
62+
}
63+
64+
@Override
65+
public void SwipeMenuMaker(SwipeMenu menu, int position) {
66+
// TODO Auto-generated method stub
67+
if(list.get(position).getName().equals("2")){
68+
SwipeMenuItem item=menu.getMenuItem(0);
69+
item.setTitle("已收藏");
70+
menu.updateMenuItem(item, 0);
71+
}else if(list.get(position).getName().equals("1")){
72+
SwipeMenuItem item=menu.getMenuItem(0);
73+
item.setTitle("收藏");
74+
menu.updateMenuItem(item, 0);
75+
}
76+
}
77+
78+
public class ViewHolder {
79+
TextView tx;
80+
}
81+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baoyz.swipemenulistview.demo;
2+
3+
public class User {
4+
private String name;
5+
6+
public String getName() {
7+
return name;
8+
}
9+
10+
public void setName(String name) {
11+
this.name = name;
12+
}
13+
14+
}

demo/src/main/res/layout/item.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical" >
6+
<TextView
7+
android:id="@+id/tip"
8+
android:layout_width="fill_parent"
9+
android:layout_height="100dip"
10+
android:text="test"
11+
/>
12+
13+
14+
</LinearLayout>

demo/src/main/res/layout/test.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
tools:context="${packageName}.${activityClass}" >
6+
7+
<com.baoyz.swipemenulistview.SwipeMenuListView
8+
android:id="@+id/listView"
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent" />
11+
12+
</RelativeLayout>
Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
11
/*
2-
* The MIT License (MIT)
3-
*
4-
* Copyright (c) 2015 nimengbo
5-
*
6-
* Permission is hereby granted, free of charge, to any person obtaining a copy
7-
* of this software and associated documentation files (the "Software"), to deal
8-
* in the Software without restriction, including without limitation the rights
9-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
* copies of the Software, and to permit persons to whom the Software is
11-
* furnished to do so, subject to the following conditions:
12-
*
13-
* The above copyright notice and this permission notice shall be included in all
14-
* copies or substantial portions of the Software.
15-
*
16-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22-
* SOFTWARE.
23-
*/
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2015 nimengbo
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
2424
package com.baoyz.swipemenulistview;
2525

2626
import android.widget.BaseAdapter;
2727

2828
/**
29-
* Created by Abner on 15/11/20.
30-
* QQ 230877476
31-
32-
* github https://github.com/nimengbo
29+
* Created by Abner on 15/11/20. QQ 230877476 Email [email protected] github
30+
* https://github.com/nimengbo
3331
*/
3432
public abstract class BaseSwipListAdapter extends BaseAdapter {
3533

36-
public boolean getSwipEnableByPosition(int position){
37-
return true;
38-
}
34+
public boolean getSwipEnableByPosition(int position) {
35+
return true;
36+
}
3937

38+
// 根据adapter数据源自定义SwipeMenuItem
39+
public void SwipeMenuMaker(SwipeMenu menu, int position) {
4040

41+
}
4142

4243
}

library/src/main/java/com/baoyz/swipemenulistview/SwipeMenu.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class SwipeMenu {
1616
private Context mContext;
1717
private List<SwipeMenuItem> mItems;
1818
private int mViewType;
19+
private SwipeMenuView mSwipeMenuView;
1920

2021
public SwipeMenu(Context context) {
2122
mContext = context;
@@ -42,10 +43,18 @@ public SwipeMenuItem getMenuItem(int index) {
4243
return mItems.get(index);
4344
}
4445

46+
public void updateMenuItem(SwipeMenuItem item, int viewId) {
47+
mSwipeMenuView.updateItem(mItems.get(item.getId()), viewId);
48+
}
49+
4550
public int getViewType() {
4651
return mViewType;
4752
}
4853

54+
public void setSwipeMenuView(SwipeMenuView swipeMenuView) {
55+
mSwipeMenuView = swipeMenuView;
56+
}
57+
4958
public void setViewType(int viewType) {
5059
this.mViewType = viewType;
5160
}

0 commit comments

Comments
 (0)