我有一个viewPager在我的应用程序中的视图之间滑动但是当一个onclick监听器被触发时,它有时会因为一些奇怪的原因更新下一页。
public class GoalsPagerAdapter extends android.support.v4.view.PagerAdapter {
int position;
EditText goalTitle,goalDesc,goalReading,visTime;
User user = MainActivity.getSingleton();
FloatingActionButton saveChangesButton;
Goal g;
DBHandler db;
Switch aSwitch;
boolean saveChanges;
Hashtable fonts;
Context context;
String goalImageUri;
LayoutInflater inflater;
TextView goalNameLabel,goalDescLabel,visLengthLabel,visReadingLabel;
ImageView goalImage;
byte[] imageBytes;
Button saveChangesDetailedButton,discardChangesDetailedButton;
public GoalsPagerAdapter(Context context,int position){
this.position =position;
this.context =context;
}
@Override
public int getCount() {
return user.getGoals().size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.detailed_goal_layout,null);
goalNameLabel = (TextView) container.findViewById(R.id.goalNameLabel);
saveChanges=false;
db = new DBHandler(context);
g = user.getGoals().get(position);
fonts = user.getFonts();
goalImage = (ImageView) view.findViewById(R.id.goalImageView);
if(g.getGoalImage()!=null){
byte[] image = user.getGoals().get(position).getGoalImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(image,0,image.length);
goalImage.setImageBitmap(bitmap);
}
goalTitle = (EditText) view.findViewById(R.id.goalTitleEditText);
goalDesc = (EditText) view.findViewById(R.id.goalDescriptionEditText);
goalReading = (EditText) view.findViewById(R.id.goalVisReadingEditText);
visTime = (EditText) view.findViewById(R.id.visTimeEditText);
saveChangesDetailedButton = (Button) view.findViewById(R.id.saveChangesDetailedButton);
discardChangesDetailedButton = (Button) view.findViewById(R.id.discardChangesDetailedButton);
saveChangesDetailedButton.setVisibility(View.INVISIBLE);
discardChangesDetailedButton.setVisibility(View.INVISIBLE);
aSwitch = (Switch) view.findViewById(R.id.switch3);
aSwitch.setChecked(g.getSwitchState());
visLengthLabel = (TextView) view.findViewById(R.id.visLengthLabel);
visLengthLabel.setTypeface((Typeface) fonts.get("Socialize"));
visReadingLabel = (TextView) view.findViewById(R.id.visReadingLabel);
visReadingLabel.setTypeface((Typeface) fonts.get("Socialize"));
goalNameLabel = (TextView) view.findViewById(R.id.goalNameLabel);
goalNameLabel.setTypeface((Typeface) fonts.get("Socialize"));
goalDescLabel = (TextView) view.findViewById(R.id.goalDescLabel);
goalDescLabel.setTypeface((Typeface) fonts.get("Socialize"));
final Drawable originalDrawable = visTime.getBackground();
visTime.setClickable(false);
visTime.setFocusable(false);
visTime.setBackground(null);
visTime.setTypeface((Typeface) fonts.get("Socialize_Circle"));
goalDesc.setClickable(false);
goalDesc.setFocusable(false);
goalDesc.setTypeface((Typeface) fonts.get("Socialize_Circle"));
goalDesc.setBackground(null);
goalTitle.setClickable(false);
goalTitle.setFocusable(false);
goalTitle.setTypeface((Typeface) fonts.get("Socialize"));
goalTitle.setBackground(null);
goalReading.setClickable(false);
goalReading.setFocusable(false);
goalReading.setTypeface((Typeface) fonts.get("Socialize_Circle"));
goalReading.setBackground(null);
//set position
//position =
goalTitle.setText(user.getGoals().get(position).getName());
goalDesc.setText(user.getGoals().get(position).getDescription());
goalReading.setText(user.getGoals().get(position).getVisReader());
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
String temp = "true";
db.updateGoalSwitchState(g,temp);
user.getGoals().get(position).setSwitchState(true);
}
else{
String temp = "false";
db.updateGoalSwitchState(g,temp);
user.getGoals().get(position).setSwitchState(false);
}
}
});
saveChangesButton = (FloatingActionButton) view.findViewById(R.id.saveChangesButton);
saveChangesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!saveChanges) {
visTime.setClickable(true);
visTime.setFocusable(true);
visTime.setFocusableInTouchMode(true);
visTime.setBackgroundResource(android.R.drawable.edit_text);
saveChangesButton.setBackgroundResource(android.R.drawable.ic_menu_add);
goalDesc.setClickable(true);
goalDesc.setFocusable(true);
goalDesc.setFocusableInTouchMode(true);
goalDesc.setBackgroundResource(android.R.drawable.edit_text);
goalTitle.setClickable(true);
goalTitle.setFocusable(true);
goalTitle.setFocusableInTouchMode(true);
goalTitle.setBackgroundResource(android.R.drawable.edit_text);
goalReading.setClickable(true);
goalReading.setFocusableInTouchMode(true);
goalReading.setFocusable(true);
goalReading.setBackgroundResource(android.R.drawable.edit_text);
saveChanges=true;
// goalImage.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
// photoPickerIntent.setType("image/*");
// startActivityForResult(photoPickerIntent,1);
// }
// });
saveChangesDetailedButton.setVisibility(View.VISIBLE);
saveChangesDetailedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newGoalTitle, newGoalDesc, newVisReading;
int newGoalVisTime;
newGoalTitle = goalTitle.getText().toString();
newGoalDesc = goalDesc.getText().toString();
newVisReading = goalReading.getText().toString().trim();
newGoalVisTime = Integer.parseInt(visTime.getText().toString());
Goal g = user.getGoals().get(position);
if (!g.getName().equals(newGoalTitle) || !g.getDescription().equals(newGoalDesc) || g.getVisTime() != newGoalVisTime || newVisReading !=g.getVisReader() ) {
Goal newGoal = new Goal(newGoalTitle, newGoalDesc, "12/12/12", newGoalVisTime, true, newVisReading);
if(imageBytes!=null){
newGoal.setGoalImage(imageBytes);
}
newGoal.setGoalImage(g.getGoalImage());
user.getGoals().set(position,newGoal);
db.updateGoalData(newGoal, g.getName());
Toast.makeText(context, "Goal updated successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "No changes were made", Toast.LENGTH_SHORT).show();
}
saveChanges=false;
discardChangesDetailedButton.setVisibility(View.INVISIBLE);
saveChangesDetailedButton.setVisibility(View.INVISIBLE);
setUnClickable();
}
});
discardChangesDetailedButton.setVisibility(View.VISIBLE);
discardChangesDetailedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goalTitle.setText(g.getName());
goalDesc.setText(g.getDescription());
goalReading.setText(g.getVisReader());
visTime.setText(g.getVisTime()+"");
setUnClickable();
saveChanges=false;
discardChangesDetailedButton.setVisibility(View.INVISIBLE);
saveChangesDetailedButton.setVisibility(View.INVISIBLE);
}
});
} else {
saveChanges=false;
saveChangesDetailedButton.setVisibility(View.INVISIBLE);
discardChangesDetailedButton.setVisibility(View.INVISIBLE);
setUnClickable();
}
}
});
container.addView(view);
return view;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
public void setUnClickable(){
visTime.setClickable(false);
visTime.setFocusable(false);
visTime.setBackgroundResource(0);
goalDesc.setClickable(false);
goalDesc.setFocusable(false);
goalDesc.setBackgroundResource(0);
goalTitle.setClickable(false);
goalTitle.setFocusable(false);
goalTitle.setBackgroundResource(0);
goalReading.setClickable(false);
goalReading.setFocusable(false);
goalReading.setBackgroundResource(0);
saveChangesButton.setBackgroundResource(android.R.drawable.ic_menu_edit);
goalImage.setOnClickListener(null);
}
}
这是我的ViewPager类:
public class GoalsViewPagerActivity extends Activity {
int pos;
ViewPager viewPager;
GoalsPagerAdapter gpa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.goals_view_pager_layout);
Intent prevIntetn = getIntent();
pos = prevIntetn.getIntExtra("position", 0);
viewPager = (ViewPager) findViewById(R.id.goalsViewPager);
gpa = new GoalsPagerAdapter(this, pos);
viewPager.setAdapter(gpa);
viewPager.setCurrentItem(pos);
}
}
如果你们在我的代码中看到错误,请告诉我。 提前感谢任何帮助:)