Skip to content

Commit

Permalink
Demo Page 之间添加 padding
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouwei committed Aug 20, 2017
1 parent 4f36f57 commit 6cf3548
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
146 changes: 146 additions & 0 deletions app/src/main/java/com/zhouwei/mzbannerview/MZModeNotCoverFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.zhouwei.mzbannerview;

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

import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

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

/**
* Created by zhouwei on 17/5/31.
*/

public class MZModeNotCoverFragment extends Fragment{
public static final String TAG = "MZModeBannerFragment";
public static final int []RES = new int[]{R.mipmap.image5,R.mipmap.image2,R.mipmap.image3,R.mipmap.image4,R.mipmap.image6,R.mipmap.image7,R.mipmap.image8};
public static final int []BANNER = new int[]{R.mipmap.banner1,R.mipmap.banner2,R.mipmap.banner3,R.mipmap.banner4,R.mipmap.banner5};
private MZBannerView mMZBanner;
private MZBannerView mNormalBanner;


public static MZModeNotCoverFragment newInstance(){
return new MZModeNotCoverFragment();
}

private void initView(View view) {

mMZBanner = (MZBannerView) view.findViewById(R.id.banner);
mMZBanner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
@Override
public void onPageClick(View view, int position) {
Toast.makeText(getContext(),"click page:"+position,Toast.LENGTH_LONG).show();
}
});
mMZBanner.addPageChangeLisnter(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.e(TAG,"----->addPageChangeLisnter:"+position + "positionOffset:"+positionOffset+ "positionOffsetPixels:"+positionOffsetPixels);
}

@Override
public void onPageSelected(int position) {
Log.e(TAG,"addPageChangeLisnter:"+position);
}

@Override
public void onPageScrollStateChanged(int state) {

}
});
List<Integer> list = new ArrayList<>();
for(int i=0;i<RES.length;i++){
list.add(RES[i]);
}

List<Integer> bannerList = new ArrayList<>();
for(int i=0;i<BANNER.length;i++){
bannerList.add(BANNER[i]);
}
mMZBanner.setIndicatorVisible(false);
mMZBanner.setPages(bannerList, new MZHolderCreator<BannerViewHolder>() {
@Override
public BannerViewHolder createViewHolder() {
return new BannerViewHolder();
}
});

mNormalBanner = (MZBannerView) view.findViewById(R.id.banner_normal);
mNormalBanner.setIndicatorRes(R.color.colorAccent,R.color.colorPrimary);
mNormalBanner.setPages(list, new MZHolderCreator<BannerPaddingViewHolder>() {
@Override
public BannerPaddingViewHolder createViewHolder() {
return new BannerPaddingViewHolder();
}
});


}

public static class BannerViewHolder implements MZViewHolder<Integer> {
private ImageView mImageView;
@Override
public View createView(Context context) {
// 返回页面布局文件
View view = LayoutInflater.from(context).inflate(R.layout.banner_item,null);
mImageView = (ImageView) view.findViewById(R.id.banner_image);
return view;
}

@Override
public void onBind(Context context, int position, Integer data) {
// 数据绑定
mImageView.setImageResource(data);
}
}

public static class BannerPaddingViewHolder implements MZViewHolder<Integer> {
private ImageView mImageView;
@Override
public View createView(Context context) {
// 返回页面布局文件
View view = LayoutInflater.from(context).inflate(R.layout.banner_item_padding,null);
mImageView = (ImageView) view.findViewById(R.id.banner_image);
return view;
}

@Override
public void onBind(Context context, int position, Integer data) {
// 数据绑定
mImageView.setImageResource(data);
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mz_mode_not_cover,null);
initView(view);
return view;
}

@Override
public void onPause() {
super.onPause();
mMZBanner.pause();
mNormalBanner.pause();
}

@Override
public void onResume() {
super.onResume();
mMZBanner.start();
mNormalBanner.start();
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/com/zhouwei/mzbannerview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public boolean onMenuItemClick(MenuItem item) {
switchViewPagerMode();
}else if(item.getItemId() == R.id.remote_mode){
switchRemoteMode();
}else if(item.getItemId() == R.id.mz_mode_not_cover){
switchMZModeNotCover();
}
return true;
}
Expand All @@ -52,8 +54,16 @@ public void switchViewPagerMode(){
getSupportFragmentManager().beginTransaction().replace(R.id.home_container,fragment).commit();
}

/**
* 从网络获取数据
*/
public void switchRemoteMode(){
Fragment fragment = RemoteTestFragment.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.home_container,fragment).commit();
}

public void switchMZModeNotCover(){
Fragment fragment = MZModeNotCoverFragment.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.home_container,fragment).commit();
}
}
14 changes: 14 additions & 0 deletions app/src/main/res/layout/banner_item_padding.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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">
<ImageView
android:id="@+id/banner_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
/>
</LinearLayout>
52 changes: 52 additions & 0 deletions app/src/main/res/layout/mz_mode_not_cover.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/darker_gray"
android:textSize="20sp"
android:text="仿魅族Banner,中间Page不覆盖"
android:gravity="center"
/>

<com.zhouwei.mzbanner.MZBannerView
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="10dp"
app:open_mz_mode="true"
app:canLoop="true"
app:middle_page_cover="false"
app:indicatorAlign="center"
app:indicatorPaddingLeft="10dp"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/darker_gray"
android:textSize="20sp"
android:text="普通Banner"
android:gravity="center"
android:layout_marginTop="20dp"
/>

<com.zhouwei.mzbanner.MZBannerView
android:id="@+id/banner_normal"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="10dp"
app:open_mz_mode="true"
app:middle_page_cover="false"
app:canLoop="true"
app:indicatorAlign="center"
/>

</LinearLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/menu/setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
<item android:id="@+id/remote_mode"
android:title="从网上获取数据模式"
/>
<item android:id="@+id/mz_mode_not_cover"
android:title="Banner模式,中间Page不覆盖"
/>
</menu>

0 comments on commit 6cf3548

Please sign in to comment.