Skip to content

Commit

Permalink
fix:解决数据变化之后刷新crush.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouwei committed Jul 20, 2017
1 parent 3466d07 commit 0cbe987
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ dependencies {
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
//compile project(':mzbanner')
compile 'com.github.pinguo-zhouwei:MZBannerView:v1.1.1'
compile project(':mzbanner')
//compile 'com.github.pinguo-zhouwei:MZBannerView:v1.1.1'

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zhouwei.mzbanner.MZBannerView;
Expand All @@ -18,6 +19,7 @@
import com.zhouwei.mzbannerview.R;
import com.zhouwei.mzbannerview.http.Fault;

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

import rx.functions.Action1;
Expand All @@ -26,9 +28,11 @@
* Created by zhouwei on 17/6/8.
*/

public class RemoteTestFragment extends Fragment {
public class RemoteTestFragment extends Fragment implements View.OnClickListener{
private MovieLoader mMovieLoader;
private MZBannerView mMZBannerView;
private TextView mBtnChange;
private List<Movie> mMovies;
private Handler mHandler = new Handler();
public static RemoteTestFragment newInstance(){
RemoteTestFragment fragment = new RemoteTestFragment();
Expand All @@ -40,6 +44,8 @@ public static RemoteTestFragment newInstance(){
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.remote_test_layout,null);
mMZBannerView = (MZBannerView) view.findViewById(R.id.my_banner);
mBtnChange = (TextView) view.findViewById(R.id.btn_change);
mBtnChange.setOnClickListener(this);
mMovieLoader = new MovieLoader();
getMovieList();
return view;
Expand All @@ -53,6 +59,7 @@ private void getMovieList(){
mMovieLoader.getMovie(0,10).subscribe(new Action1<List<Movie>>() {
@Override
public void call(List<Movie> movies) {
mMovies = movies;
Log.e("zhouwei","get data suceess");
setBanner(movies);
}
Expand Down Expand Up @@ -94,6 +101,16 @@ public void onDestroy() {
mMZBannerView.pause();
}

@Override
public void onClick(View v) {
if(v.getId() == R.id.btn_change){
List movies = new ArrayList(mMovies);
movies.add(mMovies.get(0));
movies.add(mMovies.get(1));
setBanner(movies);
}
}

public static class BannerViewHolder implements MZViewHolder<Movie>{
private ImageView mImageView;
@Override
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/remote_test_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.zhouwei.mzbanner.MZBannerView
android:id="@+id/my_banner"
android:layout_width="match_parent"
Expand All @@ -13,4 +14,17 @@
>

</com.zhouwei.mzbanner.MZBannerView>

<TextView
android:id="@+id/btn_change"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="数目增加更新"
android:padding="10dp"
android:gravity="center"
android:textColor="@android:color/white"
android:background="#AA47BC"
/>
</LinearLayout>
11 changes: 9 additions & 2 deletions mzbanner/src/main/java/com/zhouwei/mzbanner/MZBannerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ public void setPages(List<T> datas,MZHolderCreator mzHolderCreator){
return;
}
mDatas = datas;
//如果在播放,就先让播放停止
pause();

//增加一个逻辑:由于魅族模式会在一个页面展示前后页面的部分,因此,数据集合的长度至少为3,否则,自动为普通Banner模式
//不管配置的:open_mz_mode 属性的值是否为true
Expand All @@ -350,10 +352,16 @@ public void setPages(List<T> datas,MZHolderCreator mzHolderCreator){
mViewPager.setClipChildren(true);
}
setOpenMZEffect();
// 2017.7.20 fix:将Indicator初始化放在Adapter的初始化之前,解决更新数据变化更新时crush.
//初始化Indicator
initIndicator();

mAdapter = new MZPagerAdapter(datas,mzHolderCreator,mIsCanLoop);
mAdapter.setUpViewViewPager(mViewPager);
mAdapter.setPageClickListener(mBannerPageClickListener);



mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Expand Down Expand Up @@ -392,8 +400,7 @@ public void onPageScrollStateChanged(int state) {
if(mOnPageChangeListener!=null){
mViewPager.addOnPageChangeListener(mOnPageChangeListener);
}
//初始化Indicator
initIndicator();

}

/**
Expand Down

0 comments on commit 0cbe987

Please sign in to comment.