我是android和oop编程的新手。我只是想问一下如何缩短或更多地使用我的代码。我得到了一个在切换案例场景中调用的方法。
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(this, bookCover);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@SuppressLint("NewApi")
@Override
public void onPageSelected(int position) {
final int pos = position;
share.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if(pos == 0) {
Bundle params = new Bundle();
params.putString("name", "Brochure Nur1");
params.putString("caption", "This is a test for Brochure Nur.");
params.putString("description", "Brochure Nur is already available on android app!");
params.putString("link", "https://developers.facebook.com/android");
//params.putString("picture", "https://www.facebook.com/photo.php?fbid=10203610233686079&set=a.10203610230766006.1073741829.1523023247&type=1&theater");
params.putString("picture","https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(MainActivity.this,
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
// When the story is posted, echo the success
// and the post Id.
final String postId = values.getString("post_id");
if (postId != null) {
Toast.makeText(MainActivity.this,
"Successfuly posted!",
Toast.LENGTH_SHORT).show();
} else {
// User clicked the Cancel button
Toast.makeText(MainActivity.this,
"Publish cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(MainActivity.this,
"Publish cancelled",
Toast.LENGTH_SHORT).show();
} else {
// Generic, ex: network error
Toast.makeText(MainActivity.this,
"Error posting story",
Toast.LENGTH_SHORT).show();
}
}
})
.build();
feedDialog.show();
}
if(pos == 1) {
Bundle params = new Bundle();
params.putString("name", "Brochure Nur2");
params.putString("caption", "This is a test for Brochure Nur.");
params.putString("description", "Brochure Nur is already available on android app!");
params.putString("link", "https://developers.facebook.com/android");
//params.putString("picture", "https://www.facebook.com/photo.php?fbid=10203610233686079&set=a.10203610230766006.1073741829.1523023247&type=1&theater");
params.putString("picture","https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(MainActivity.this,
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
// When the story is posted, echo the success
// and the post Id.
final String postId = values.getString("post_id");
if (postId != null) {
Toast.makeText(MainActivity.this,
"Successfuly posted!",
Toast.LENGTH_SHORT).show();
} else {
// User clicked the Cancel button
Toast.makeText(MainActivity.this,
"Publish cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(MainActivity.this,
"Publish cancelled",
Toast.LENGTH_SHORT).show();
} else {
// Generic, ex: network error
Toast.makeText(MainActivity.this,
"Error posting story",
Toast.LENGTH_SHORT).show();
}
}
})
.build();
feedDialog.show();
}
有没有办法让它更短但功能更强?因为我尽可能不想重复代码。