我的活动
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final Adapter devicePageAdaptor = new Adapter(getSupportFragmentManager(), getApplicationContext());
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(devicePageAdaptor);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
片段
public class fragment extends Fragment {
public static final String ProverbKey = "proverbKey" ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_device, container, false);
Bundle bundle = getArguments();
if (bundle != null) {
String proverb = bundle.getString(ProverbKey);
setValues(view,proverb);
}
return view;
}
private void setValues(View view, String proverb) {
TextView textView = (TextView) view.findViewById(R.id.textView_proverb);
textView.setText(proverb);
}
}
扩展fragmentStateAdapter的适配器
public class Adapter extends FragmentStatePagerAdapter {
String[] proverb;
public Adapter(android.support.v4.app.FragmentManager fm,Context context) {
super(fm);
Resources resources = context.getResources();
proverb = resources.getStringArray(R.array.proverb);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString(fragment.ProverbKey, proverb[position]);
fragment deviceFragment = new fragment();
deviceFragment.setArguments(bundle);
return deviceFragment;
}
@Override
public int getCount() {
return proverb.length;
}
}
我的每个片段中的String Array都被命名为谚语
<string-array name="proverb">
<item>A young idler, an old beggar.</item>
<item>Action speaks louder than words.</item>
<item>A hungry man is an angry man.</item>
<item>A living dog is better than a dead lion.</item>
<item>A hedge between keeps friendship green.</item>
<item>A heavy purse gives to a light heart.</item>
<item>A handful of patience is worth more than a bushel of brains</item>
<item>A guilty conscience needs no accuser.</item>
<item>A growing youth has a wolf in his belly.</item>
<item>A good name is better than a good face.</item>
<item>A good beginning makes a good end.</item>
<item>A drop of ink may make a million think.</item>
<item>A dry March, a wet April and a cool May may fill barn and cellar and bring much hay.</item>
</string-array>
答案 0 :(得分:0)
//Global Variable
String[] proverb;
//Init on onCreate()
proverb = resources.getStringArray(R.array.proverb);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = viewPager.getCurrentItem();
String itemToShare = proverb[pos];
share(itemToShare);
}
});
private void share(String item) {
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
share.putExtra(Intent.EXTRA_TEXT, item);
startActivity(Intent.createChooser(share, "Sharing Item!"));
}
尝试此操作并在工作时更新我