Skip to content

Commit b7936e5

Browse files
mcxtzhangzhangxutong
authored and
zhangxutong
committed
fixes #14,fixes #47,因为ViewPager 和 SwipMenuLayout都是水平方向滑动的控件。
所以在一起使用时会有冲突, 使用本控件(CstViewPager),可以在ViewPager的第一页使用左滑。在ViewPager的最后一页使用右滑菜单。
1 parent 8db1fed commit b7936e5

File tree

10 files changed

+375
-7
lines changed

10 files changed

+375
-7
lines changed

Diff for: app/src/main/AndroidManifest.xml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<category android:name="android.intent.category.LAUNCHER"/>
2222
</intent-filter>
2323
</activity>
24+
<activity android:name="mcxtzhang.swipedelmenu.viewpager.ViewPagerActivity">
25+
</activity>
2426
</application>
2527

2628
</manifest>

Diff for: app/src/main/java/mcxtzhang/swipedelmenu/LauncherActivity.java

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import mcxtzhang.listswipemenudemo.R;
99
import mcxtzhang.swipedelmenu.FullDemo.FullDelDemoActivity;
10+
import mcxtzhang.swipedelmenu.viewpager.ViewPagerActivity;
1011

1112
public class LauncherActivity extends AppCompatActivity {
1213

@@ -35,5 +36,12 @@ public void onClick(View v) {
3536
startActivity(new Intent(v.getContext(), LinearLayoutDelDemoActivity.class));
3637
}
3738
});
39+
40+
findViewById(R.id.viewPager).setOnClickListener(new View.OnClickListener() {
41+
@Override
42+
public void onClick(View view) {
43+
startActivity(new Intent(LauncherActivity.this, ViewPagerActivity.class));
44+
}
45+
});
3846
}
3947
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package mcxtzhang.swipedelmenu.viewpager;
2+
3+
import android.graphics.Color;
4+
import android.os.Bundle;
5+
import android.support.annotation.Nullable;
6+
import android.support.v4.app.Fragment;
7+
import android.support.v7.widget.LinearLayoutManager;
8+
import android.support.v7.widget.RecyclerView;
9+
import android.util.Log;
10+
import android.view.LayoutInflater;
11+
import android.view.MotionEvent;
12+
import android.view.View;
13+
import android.view.ViewGroup;
14+
import android.widget.Toast;
15+
16+
import com.mcxtzhang.commonadapter.rv.CommonAdapter;
17+
import com.mcxtzhang.commonadapter.rv.ViewHolder;
18+
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import mcxtzhang.listswipemenudemo.R;
24+
import mcxtzhang.swipedelmenu.SwipeBean;
25+
26+
/**
27+
* Intro:
28+
* Author: zhangxutong
29+
30+
* Home Page: http://blog.csdn.net/zxt0601
31+
* Created: 2017/9/27.
32+
* History:
33+
*/
34+
35+
public class FullDemoFragment extends Fragment {
36+
public static FullDemoFragment newInstance(int position) {
37+
Bundle args = new Bundle();
38+
FullDemoFragment fragment = new FullDemoFragment();
39+
args.putInt("index", position);
40+
fragment.setArguments(args);
41+
return fragment;
42+
}
43+
44+
@Override
45+
public void onCreate(@Nullable Bundle savedInstanceState) {
46+
super.onCreate(savedInstanceState);
47+
mIndex = getArguments().getInt("index");
48+
}
49+
50+
private static final String TAG = "zxt";
51+
private RecyclerView mRecyclerView;
52+
private CommonAdapter<SwipeBean> mAdapter;
53+
private LinearLayoutManager mLayoutManager;
54+
private List<SwipeBean> mDatas;
55+
private int mIndex;
56+
57+
@Nullable
58+
@Override
59+
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
60+
View inflate = LayoutInflater.from(getContext()).inflate(R.layout.fragment_full_del_demo, container, false);
61+
62+
mRecyclerView = (RecyclerView) inflate.findViewById(R.id.rv);
63+
//区分一下不同页面
64+
switch (mIndex) {
65+
case 0:
66+
mRecyclerView.setBackgroundColor(Color.WHITE);
67+
break;
68+
case 1:
69+
mRecyclerView.setBackgroundColor(Color.YELLOW);
70+
break;
71+
default:
72+
mRecyclerView.setBackgroundColor(Color.GREEN);
73+
break;
74+
}
75+
76+
77+
initDatas();
78+
mAdapter = new CommonAdapter<SwipeBean>(getContext(), mDatas, R.layout.item_cst_swipe) {
79+
@Override
80+
public void convert(final ViewHolder holder, SwipeBean swipeBean) {
81+
((SwipeMenuLayout) holder.itemView).setLeftSwipe(mIndex == 0 ? false : true);//这句话关掉IOS阻塞式交互效果 并依次打开左滑右滑
82+
83+
holder.setText(R.id.content, swipeBean.name + (mIndex == 0 ? "我左青龙" : "我右白虎"));
84+
85+
//验证长按
86+
holder.setOnLongClickListener(R.id.content, new View.OnLongClickListener() {
87+
@Override
88+
public boolean onLongClick(View v) {
89+
Toast.makeText(mContext, "longclig", Toast.LENGTH_SHORT).show();
90+
Log.d("TAG", "onLongClick() called with: v = [" + v + "]");
91+
return false;
92+
}
93+
});
94+
95+
holder.setOnClickListener(R.id.btnDelete, new View.OnClickListener() {
96+
@Override
97+
public void onClick(View v) {
98+
int pos = holder.getLayoutPosition();
99+
if (pos >= 0 && pos < mDatas.size()) {
100+
Toast.makeText(getContext(), "删除:" + pos, Toast.LENGTH_SHORT).show();
101+
mDatas.remove(pos);
102+
mAdapter.notifyItemRemoved(pos);//推荐用这个
103+
//如果删除时,不使用mAdapter.notifyItemRemoved(pos),则删除没有动画效果,
104+
//且如果想让侧滑菜单同时关闭,需要同时调用 ((SwipeMenuLayout) holder.itemView).quickClose();
105+
//mAdapter.notifyDataSetChanged();
106+
}
107+
}
108+
});
109+
//注意事项,设置item点击,不能对整个holder.itemView设置咯,只能对第一个子View,即原来的content设置,这算是局限性吧。
110+
(holder).setOnClickListener(R.id.content, new View.OnClickListener() {
111+
@Override
112+
public void onClick(View v) {
113+
Toast.makeText(mContext, "onClick:" + mDatas.get(holder.getAdapterPosition()).name, Toast.LENGTH_SHORT).show();
114+
Log.d("TAG", "onClick() called with: v = [" + v + "]");
115+
}
116+
});
117+
//置顶:
118+
holder.setOnClickListener(R.id.btnTop, new View.OnClickListener() {
119+
@Override
120+
public void onClick(View v) {
121+
int pos = holder.getLayoutPosition();
122+
123+
if (pos > 0 && pos < mDatas.size()) {
124+
SwipeBean swipeBean = mDatas.get(pos);
125+
mDatas.remove(swipeBean);
126+
mAdapter.notifyItemInserted(0);
127+
mDatas.add(0, swipeBean);
128+
mAdapter.notifyItemRemoved(pos + 1);
129+
if (mLayoutManager.findFirstVisibleItemPosition() == 0) {
130+
mRecyclerView.scrollToPosition(0);
131+
}
132+
//notifyItemRangeChanged(0,holder.getAdapterPosition()+1);
133+
}
134+
135+
}
136+
});
137+
138+
139+
}
140+
};
141+
142+
mRecyclerView.setAdapter(mAdapter);
143+
//mRecyclerView.setLayoutManager(mLayoutManager = new GridLayoutManager(getContext(), 2));
144+
mRecyclerView.setLayoutManager(mLayoutManager = new LinearLayoutManager(getContext()));
145+
146+
//6 2016 10 21 add , 增加viewChache 的 get()方法,
147+
// 可以用在:当点击外部空白处时,关闭正在展开的侧滑菜单。我个人觉得意义不大,
148+
mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
149+
@Override
150+
public boolean onTouch(View v, MotionEvent event) {
151+
if (event.getAction() == MotionEvent.ACTION_UP) {
152+
SwipeMenuLayout viewCache = SwipeMenuLayout.getViewCache();
153+
if (null != viewCache) {
154+
viewCache.smoothClose();
155+
}
156+
}
157+
return false;
158+
}
159+
});
160+
161+
return inflate;
162+
}
163+
164+
165+
private void initDatas() {
166+
mDatas = new ArrayList<>();
167+
for (int i = 0; i < 20; i++) {
168+
mDatas.add(new SwipeBean("" + i));
169+
}
170+
}
171+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package mcxtzhang.swipedelmenu.viewpager;
2+
3+
import android.support.v4.app.Fragment;
4+
import android.support.v4.app.FragmentPagerAdapter;
5+
import android.support.v4.view.ViewPager;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.os.Bundle;
8+
9+
import mcxtzhang.listswipemenudemo.R;
10+
11+
public class ViewPagerActivity extends AppCompatActivity {
12+
ViewPager mViewPager;
13+
14+
@Override
15+
protected void onCreate(Bundle savedInstanceState) {
16+
super.onCreate(savedInstanceState);
17+
setContentView(R.layout.activity_view_pager);
18+
mViewPager = (ViewPager) findViewById(R.id.viewPager);
19+
mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
20+
@Override
21+
public Fragment getItem(int position) {
22+
return FullDemoFragment.newInstance(position);
23+
}
24+
25+
@Override
26+
public int getCount() {
27+
return 3;
28+
}
29+
});
30+
}
31+
}

Diff for: app/src/main/res/layout/activity_launcher.xml

+14-6
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,25 @@
1414

1515
<Button
1616
android:id="@+id/rv"
17-
android:text="完整的RecyclerViewDemo"
1817
android:layout_width="wrap_content"
19-
android:layout_height="wrap_content"/>
18+
android:layout_height="wrap_content"
19+
android:text="完整的RecyclerViewDemo"/>
20+
2021
<Button
2122
android:id="@+id/lv"
22-
android:text="ListView"
2323
android:layout_width="wrap_content"
24-
android:layout_height="wrap_content"/>
24+
android:layout_height="wrap_content"
25+
android:text="ListView"/>
26+
2527
<Button
2628
android:id="@+id/ll"
27-
android:text="线性布局LinearLayout"
2829
android:layout_width="wrap_content"
29-
android:layout_height="wrap_content"/>
30+
android:layout_height="wrap_content"
31+
android:text="线性布局LinearLayout"/>
32+
33+
<Button
34+
android:id="@+id/viewPager"
35+
android:layout_width="wrap_content"
36+
android:layout_height="wrap_content"
37+
android:text="ViewPager"/>
3038
</LinearLayout>

Diff for: app/src/main/res/layout/activity_view_pager.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/activity_linear_layout_del_demo"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
android:orientation="vertical">
9+
10+
<com.mcxtzhang.swipemenulib.CstViewPager
11+
android:id="@+id/viewPager"
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent"></com.mcxtzhang.swipemenulib.CstViewPager>
14+
</LinearLayout>

Diff for: app/src/main/res/layout/fragment_full_del_demo.xml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.v4.widget.SwipeRefreshLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
8+
<android.support.v7.widget.RecyclerView
9+
android:id="@+id/rv"
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
12+
android:background="#efefef"/>
13+
android:layout_width="match_parent"
14+
android:layout_height="match_parent">
15+
</android.support.v4.widget.SwipeRefreshLayout>
16+
17+

Diff for: swipemenulib/build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 24
4+
compileSdkVersion 25
55
buildToolsVersion '25.0.0'
66

77
defaultConfig {
@@ -21,4 +21,5 @@ android {
2121

2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
compile 'com.android.support:support-core-ui:25.0.0'
2425
}

0 commit comments

Comments
 (0)