0

我想通过 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;
}
}  
4

1 回答 1

0

我得到了我的问题的解决方案 我改变了我的 Share.java班级如下..

分享.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:"));

}

}
于 2013-06-25T14:12:38.550 回答