gpt4 book ai didi

java - Appcompact 21.0.3 addOnPageChangeListener 错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:42:56 25 4
gpt4 key购买 nike

我正在使用 AppCompact 21.0.3 编写应用程序

如何修复此错误,但我在代码中遇到 addonpagechangelistener 错误?

Faulty section:* pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()

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.ActionBarActivity;
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 ActionBarActivity {

//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);
}
}

最佳答案

试试这个。

viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

public void onPageSelected(int position) {
// Check if this is the page you want.
}

});

关于java - Appcompact 21.0.3 addOnPageChangeListener 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31097718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com