我想分享ViewPager上显示的TextView内容 FragmentPagerAdapter但是当我分享之前共享的内容时 专注于TextView内容。
当我点击“分享”按钮时,它会分享最后一个内容 片段,我来自当前片段。
我的代码
Share.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;
}
}
答案 0 :(得分:0)
我解决了我的问题
我更改了我的Share.java
课程如下..
<强> Share.java 强>
public class ShareNew extends FragmentActivity {
private MyPager mPager;
static int pages = 4;
Button share;
private TextView textView;
private int i;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpagerlayout);
mPager = (MyPager) findViewById(R.id.myviewpager);
// mPager.setAdapter(mAdapter);
share = (Button) findViewById(R.id.btnShare);
for (i = 0; i < pages; i++) {
textView = new TextView(this);
switch (i) {
case 0:
textView.setText(R.string.share1);
break;
case 1:
textView.setText(R.string.share2);
break;
case 2:
textView.setText(R.string.share3);
break;
case 3:
textView.setText(R.string.share4);
break;
}
textView.setTag(i);
textView.setBackgroundColor(Color.BLUE);
textView.setTextColor(Color.WHITE);
textView.setTextSize(30);
mPager.addPage(textView);
}
}
public void onShareText(View view) {
TextView tvShare;
tvShare = (TextView) mPager.findViewWithTag(mPager.getCurrentItem());
String string = tvShare.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:"));
}
}