找不到符号方法addOnPageChangeListener

时间:2015-06-28 12:01:30

标签: java android

嘿,我刚刚开始我的第一个编程壁纸,所以这应该是一个简单的解决方案。很简单就是一个错误,但我仍然无法弄清楚这个问题。 请帮忙,谢谢。

  

AppCompat版本:21.0.3

package com.mikepenz.unsplash.materialintro;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;

import com.mikepenz.unsplash.R;

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

public abstract class MaterialIntroActivity extends AppCompatActivity {

    //Pager
    private IntroPagerAdapter mPagerAdapter;
    private ViewPager pager;

    private List<Fragment> fragments = new ArrayList<>();
    private List<ImageView> dots;

    //Config values
    private int totalPagesNum;
    private int currentPageNum;
    private boolean showSkip = true;
    private static int FIRST_PAGE_NUM = 0;

    private float oldPositionOffset;

    /* Abstract Methods */
    protected abstract void init();
    protected abstract void onSkipPressed();
    protected abstract void onDonePressed();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_intro);
        if(getActionBar()!=null) getActionBar().hide();

        final TextView skipButton = (TextView) findViewById(R.id.skip_button);
        final ImageView nextButton = (ImageView) findViewById(R.id.next_button);
        final TextView doneButton = (TextView) findViewById(R.id.done_button);

        mPagerAdapter = new IntroPagerAdapter(getSupportFragmentManager(), fragments);
        pager = (ViewPager) findViewById(R.id.view_pager);
        pager.setAdapter(mPagerAdapter);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                boolean directionNext = position > currentPageNum;

                selectDot(currentPageNum, positionOffset, directionNext);
            }
            @Override
            public void onPageSelected(int position) {
                currentPageNum = position;
                if (position == totalPagesNum - 1){
                    nextButton.setVisibility(View.GONE);
                    doneButton.setVisibility(View.VISIBLE);
                    if(showSkip) skipButton.setVisibility(View.INVISIBLE);
                } else {
                    nextButton.setVisibility(View.VISIBLE);
                    doneButton.setVisibility(View.GONE);
                    if(showSkip) skipButton.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {}
        });
        //pager.setPageTransformer(false, new PageTransformer());

        skipButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSkipPressed();
            }
        });
        doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onDonePressed();
            }
        });
        nextButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(pager.getCurrentItem() + 1);
            }
        });

        init();
        loadDots();
    }

/*    public class PageTransformer implements ViewPager.PageTransformer {
        public void transformPage(View view, float position) {
            if(position <= -1.0F || position >= 1.0F) {
                selectDot(currentPageNum, 0.0F);
            } else if( position == 0.0F ) {
                selectDot(currentPageNum, 1.0F);
            } else {
                // position is between -1.0F & 0.0F OR 0.0F & 1.0F
                //selectDot(currentPageNum, 1.0F - Math.abs(position));
                selectDot(currentPageNum, 1.0F - Math.abs(position));
            }
            if(position>1)
            Log.d("GGG", position + "");
        }
    }*/

    private void selectDot(int index) {
        selectDot(index, 0, true);
    }

    private void selectDot(int index, float positionOffset, boolean directionNext) {
        Log.d("GGG", positionOffset + " Next " + directionNext);

        float offset = positionOffset;
        if(offset>1 || offset<=0){
            offset = 1.0f;
        } else {
            offset = (offset/2) + 0.5f;
            if(directionNext)
                offset = (float) 1.5 - offset;
            else
                offset = (offset/2) + 0.5f;
        }

        Log.d("GGG", "NEW " + offset);
        Log.d("GGG", "INDEX " + index);

        for (int i = 0; i < fragments.size(); i++) {
            if(index == i){
                dots.get(i).setAlpha(offset);
            } else {
                dots.get(i).setAlpha(0.5F);
            }
        }
    }

    private void _selectDot(int index) {
        Resources res = getResources();
        for (int i = 0; i < fragments.size(); i++) {
            int drawableId = (i == index) ? (R.drawable.indicator_dot_white) : (R.drawable.indicator_dot_grey);
            Drawable drawable = res.getDrawable(drawableId);
            dots.get(i).setImageDrawable(drawable);
        }
    }


    private void loadDots() {
        LinearLayout dotLayout = (LinearLayout) findViewById(R.id.dotLayout);
        dots = new ArrayList<>();
        totalPagesNum = fragments.size();

        for (int i = 0; i < totalPagesNum; i++) {
            ImageView dot = new ImageView(this);
            dot.setImageDrawable(getResources().getDrawable(R.drawable.indicator_dot_white));

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );
            dotLayout.addView(dot, params);

            dots.add(dot);
        }

        selectDot(FIRST_PAGE_NUM);
    }

    private class IntroPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments;

        public IntroPagerAdapter(FragmentManager fragmentManager, List<Fragment> fragments) {
            super(fragmentManager);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }

        @Override
        public int getCount() {
            return this.fragments.size();
        }
    }


    /* Config Methods */
    public void addPage(Fragment fragment) {
        fragments.add(fragment);
        mPagerAdapter.notifyDataSetChanged();
    }

    public void setBarColor(int colorRes){
        LinearLayout bottomBar = (LinearLayout) findViewById(R.id.bottom_container);
        bottomBar.setBackgroundResource(colorRes);
    }

    public void setSeparatorColor(int colorRes){
        Space separator = (Space) findViewById(R.id.bottom_separator_view);
        separator.setBackgroundResource(colorRes);
    }


    public void showSkipButton(boolean showButton){
        this.showSkip = showButton;

        TextView skip = (TextView) findViewById(R.id.skip_button);
        skip.setVisibility(showButton ? View.VISIBLE : View.INVISIBLE);
    }
}

错误:

Error:(63, 14) error: cannot find symbol method addOnPageChangeListener(<anonymous OnPageChangeListener>)

1 个答案:

答案 0 :(得分:3)

随着v4支持库的修订版ViewPager.addOnPageListener引入了

22.2。使用

更新support-v4的gradle依赖关系
compile 'com.android.support:support-v4:22.2.0'