Skip to content

Commit

Permalink
Add setInterceptParent method
Browse files Browse the repository at this point in the history
  • Loading branch information
shucc committed May 3, 2017
1 parent 93f9707 commit e1a2eb1
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ allprojects {
在使用库的module中添加,为避免重复引用,推荐使用exclude::
```groovy
dependencies {
compile ('com.github.shucc:Carousel:v1.5') {
compile ('com.github.shucc:Carousel:v1.6') {
exclude group: 'com.android.support', module: 'appcompat-v7'
}
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
android:screenOrientation="portrait"/>
<activity android:name=".PtrLayoutTestActivity"
android:screenOrientation="portrait" />
<activity android:name=".ViewPagerTestActivity"
android:screenOrientation="portrait" />
</application>

</manifest>
6 changes: 6 additions & 0 deletions app/src/main/java/org/cchao/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ public void onClick(View v) {
PtrLayoutTestActivity.launch(MainActivity.this);
}
});
findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewPagerTestActivity.launch(MainActivity.this);
}
});
}
}
88 changes: 88 additions & 0 deletions app/src/main/java/org/cchao/sample/ViewPagerFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.cchao.sample;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import org.cchao.carousel.CarouselView;
import org.cchao.carousel.listener.ImageloaderListener;
import org.cchao.carousel.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
* Created by shucc on 17/5/3.
* cc@cchao.org
*/
public class ViewPagerFragment extends Fragment {

private final String TAG = getClass().getName();

private CarouselView carouselView;

private List<String> imageUrls;

private List<String> titles;

public static ViewPagerFragment newInstance() {
Bundle args = new Bundle();
ViewPagerFragment fragment = new ViewPagerFragment();
fragment.setArguments(args);
return fragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_viewpager, container, false);
carouselView = (CarouselView) rootView.findViewById(R.id.carouselView);
initData();
return rootView;
}

private void initData() {
carouselView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onClick(View view, int position) {
Toast.makeText(getActivity(), "hehe" + position, Toast.LENGTH_SHORT).show();
}
});
imageUrls = new ArrayList<>();
titles = new ArrayList<>();
imageUrls.add("http://img0.imgtn.bdimg.com/it/u=2334645085,3449212359&fm=23&gp=0.jpg");
imageUrls.add("http://pic18.nipic.com/20120115/4999414_151322555150_2.jpg");
imageUrls.add("http://att.bbs.duowan.com/forum/201204/30/201115l6j65f42kjuljluf.jpg");
imageUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492060262991&di=4c3e773b8229057c53b1c36bf16d54c4&imgtype=0&src=http%3A%2F%2Fpic30.nipic.com%2F20130621%2F11295670_223619063339_2.jpg");
titles.add("我是标题一");
titles.add("我是标题二");
titles.add("我是标题三");
titles.add("我是标题四");
carouselView.with(this)
.setImageUrls(imageUrls)
.setTitles(titles)
.setDealyTime(4 * 1000)
.setShowIndicator(true)
.setShowTitle(true)
.setAutoSwitch(false)
.setCanLoop(false)
.setInterceptParent(false)
.setImageLoaderListener(new ImageloaderListener() {
@Override
public void loadImage(Context context, ImageView imageView, int position) {
Glide.with(context)
.load(imageUrls.get(position))
.into(imageView);
}
})
.start();
}
}
54 changes: 54 additions & 0 deletions app/src/main/java/org/cchao/sample/ViewPagerTestActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.cchao.sample;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

/**
* Created by shucc on 17/5/2.
* cc@cchao.org
*/
public class ViewPagerTestActivity extends AppCompatActivity {

private ViewPager viewPager;

private List<Fragment> fragments;

public static void launch(Context context) {
Intent starter = new Intent(context, ViewPagerTestActivity.class);
context.startActivity(starter);
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager);

viewPager = (ViewPager) findViewById(R.id.viewPager);

fragments = new ArrayList<>();
fragments.add(ViewPagerFragment.newInstance());
fragments.add(ViewPagerFragment.newInstance());
fragments.add(ViewPagerFragment.newInstance());

viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}

@Override
public int getCount() {
return 3;
}
});
}
}
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@
android:layout_height="48dp"
android:text="PtrLayoutTest"/>

<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="ViewPager"/>

</LinearLayout>
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_viewpager.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
19 changes: 19 additions & 0 deletions app/src/main/res/layout/fragment_viewpager.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<org.cchao.carousel.CarouselView
android:id="@+id/carouselView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:carousel_indicator_drawable_selected="@drawable/ic_indicator_selected"
app:carousel_indicator_drawable_unselected="@drawable/ic_indicator_unselected"
app:carousel_indicator_width="24dp"
app:carousel_indicator_height="2dp"
app:carousel_indicator_padding="8dp"
app:carousel_indicator_margin_bottom="8dp"
app:carousel_title_margin_bottom="18dp"/>

</RelativeLayout>
4 changes: 2 additions & 2 deletions carouselLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 25
versionCode 6
versionName "1.5"
versionCode 7
versionName "1.6"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public CarouselRequestManager setShowTitle(boolean showTitle) {
return this;
}

public CarouselRequestManager setInterceptParent(boolean interceptParent) {
carouselView.setInterceptParent(interceptParent);
return this;
}

public CarouselRequestManager setImageLoaderListener(ImageloaderListener imageLoaderListener) {
carouselView.setImageLoaderListener(imageLoaderListener);
return this;
Expand Down
12 changes: 12 additions & 0 deletions carouselLib/src/main/java/org/cchao/carousel/CarouselView.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -79,6 +80,9 @@ public class CarouselView extends FrameLayout {
//title距离底部距离
private int titleMarginBottom;

//ViewPager是否进行事件拦截
private boolean interceptParent = true;

private Context context;

private List<String> imageUrls;
Expand Down Expand Up @@ -141,13 +145,15 @@ public CarouselView(Context context, AttributeSet attrs, int defStyleAttr) {
carouselLifecycleListener = new CarouselLifecycleListener() {
@Override
public void onStop() {
Log.d(TAG, "onStop: ");
if (isAutoSwitch) {
stop();
}
}

@Override
public void onResume() {
Log.d(TAG, "onResume: ");
if (isAutoSwitch) {
resume();
}
Expand Down Expand Up @@ -272,6 +278,11 @@ protected CarouselView setShowTitle(boolean showTitle) {
return this;
}

protected CarouselView setInterceptParent(boolean interceptParent) {
this.interceptParent = interceptParent;
return this;
}

protected CarouselView setImageLoaderListener(ImageloaderListener imageLoaderListener) {
this.imageloaderListener = imageLoaderListener;
return this;
Expand Down Expand Up @@ -314,6 +325,7 @@ protected void start() {
indicatorViews = new ArrayList<>();
addIndicator();
}
vpCarousel.setInterceptParent(false);
loopPageAdapter = new CarouselLoopPageAdapter(vpCarousel, canLoop, imageUrls, titles, showTitle
, titleColor, titleSize, titleMarginBottom, imageloaderListener);
if (onItemClickListener != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
*/
public class CarouselViewPager extends ViewPager {

private final String TAG = getClass().getName();

private float startY;
private float startX;
// 记录viewPager是否拖拽的标记
private boolean mIsVpDragger;
private final int mTouchSlop;

private boolean interceptParent = true;

public CarouselViewPager(Context context) {
super(context);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Expand All @@ -31,6 +35,9 @@ public CarouselViewPager(Context context, AttributeSet attrs) {

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (!interceptParent) {
return super.onInterceptTouchEvent(ev);
}
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Expand Down Expand Up @@ -65,4 +72,8 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
getParent().requestDisallowInterceptTouchEvent(true);
return super.onInterceptTouchEvent(ev);
}

public void setInterceptParent(boolean interceptrParent) {
this.interceptParent = interceptrParent;
}
}

0 comments on commit e1a2eb1

Please sign in to comment.