我的Android应用程序中的分享按钮有问题 - 例如,如果我从第1侧到第2侧 - 它共享第3面的文本 - 如果我从第3侧到第2侧,它共享文本这是我的共同意图的问题,但我无法找到它。
public class MainActivity extends Activity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
int maxcount;
private ShareActionProvider mShareActionProvider;
TextView textview;
TextView sideview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider)shareItem.getActionProvider();
return true;
}
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new DemoFragment();
Bundle args = new Bundle();
args.putInt("page_position", position+1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
maxcount = 10;
return maxcount;
}
public class DemoFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Bundle args = getArguments();
int side = args.getInt("page_position");
textview = (TextView) rootView.findViewById(R.id.section_label);
sideview = (TextView) rootView.findViewById(R.id.section_label2);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
switch(side) {
case 1: textview.setText(R.string.text1);
break;
case 2: textview.setText(R.string.text2);
break;
case 3: textview.setText(R.string.text3);
break;
case 4: textview.setText(R.string.text4);
break;
case 5: textview.setText(R.string.text5);
break;
case 6: textview.setText(R.string.text6);
break;
case 7: textview.setText(R.string.text7);
break;
case 8: textview.setText(R.string.text8);
break;
case 9: textview.setText(R.string.text9);
break;
case 10: textview.setText(R.string.text10);
break;
}
shareIntent.putExtra(Intent.EXTRA_TEXT, textview.getText());
setShareIntent(shareIntent);
sideview.setText("" + args.getInt("page_position") + "/" + maxcount);
return rootView;
}
}
}
}