我想通过 FragmentPagerAdapter 共享在 ViewPager 上显示的 TextView 内容,但是当我共享内容时,它会共享以前关注的 TextView 内容。
当我单击“共享”按钮时,它会共享我来自当前片段的最后一个片段的内容。
我的代码
分享.java
public class Share extends FragmentActivity {
private MyAdapter mAdapter;
private ViewPager mPager;
static int pages=4;
Button share;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.vpShare);
mPager.setAdapter(mAdapter);
share=(Button)findViewById(R.id.btShare);
share.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView textView = (TextView) findViewById(R.id.tv);
String string = textView.getText().toString();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, string);
startActivity(Intent.createChooser(intent, "Share with:"));
}
});
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return pages;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ShowFragment1();
case 1:
return new ShowFragment2();
case 2:
return new ShowFragment3();
case 3:
return new ShowFragment4();
default:
return null;
}
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/vpShare"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/sunset" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center"
android:measureWithLargestChild="true"
android:orientation="horizontal" >
<Button
android:id="@+id/btShare"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="Share" />
</LinearLayout>
</LinearLayout>
screen_slide.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="italic" />
</ScrollView>
ShowFragment1.java
public class ShowFragment1 extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.screen_slide, container, false);
TextView textView = (TextView) view.findViewById(R.id.tv);
textView.setText("fragment1");
return view;
}
}