我设计了一个android应用程序。我遇到了问题。我在列表视图中显示字符串,当用户单击列表视图时,字符串在ViewPager中显示。我想通过短信分享我的字符串或文本。当我单击“共享”按钮时,字符串始终共享在第一单击位置文本中。我滑动了viewpager页面,它会更改当前位置文本,但不会更改可共享文本。
Main_Activity上的ListView:
public class MainActivity extends FragmentActivity {
PagerFragment pagerFragment;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<PageData> data = new ArrayList<PageData>();
data.add(new PageData("1", R.drawable.store, "Picture 1 jsdj kjdkjk kjk kalskfj sk ksjfksj kjfksjfk "));
data.add(new PageData("2", R.drawable.home, "Picture 2"));
data.add(new PageData("3", R.drawable.help, "Picture 3"));
data.add(new PageData("4", R.drawable.ic_launcher_background, "Picture 4"));
final ListView lv = (ListView) findViewById(R.id.list_view);
final ListViewAdapter lva = new ListViewAdapter(this, R.layout.item_list, data);
lv.setAdapter(lva);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String Category = "Hello Nasir";
final int positionview = lv.getPositionForView(view);
PagerFragment pagerFragment = new PagerFragment();
Bundle bundle = new Bundle();
bundle.putInt("CURRENT_POSITION", positionview);
bundle.putParcelableArrayList("DATA_LIST", data);
bundle.putString("Category", Category);
pagerFragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, pagerFragment, "swipe_view_fragment").commit();
}
});
}
@Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentByTag("swipe_view_fragment");
if (f != null) {
fm.beginTransaction().remove(f).commit();
} else {
super.onBackPressed();
}
}
}
ViewPager:
public class PagerFragment extends Fragment{
private List<PageData> data;
private int currentPosition;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pager, container, false);
final ViewPager mViewPager = v.findViewById(R.id.pager_view);
currentPosition = getArguments().getInt("CURRENT_POSITION");
data = getArguments().getParcelableArrayList("DATA_LIST");
ImageButton share = v.findViewById(R.id.share);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareString = data.get(currentPosition).getTitle();
// String shareString = mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1).;
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareString);
startActivity(Intent.createChooser(sharingIntent, "Share Application Via"));
}
});
FragmentItemPagerAdapter fragmentItemPagerAdapter = new FragmentItemPagerAdapter(getFragmentManager(), data);
mViewPager.setAdapter(fragmentItemPagerAdapter);
mViewPager.setCurrentItem(currentPosition);
return v;
}
}
ViewPagerAdapter:
public class FragmentItemPagerAdapter extends FragmentStatePagerAdapter {
private List<PageData> data;
public FragmentItemPagerAdapter(FragmentManager fm, List<PageData> data) {
super(fm);
this.data = data;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putString(PageFragment.COUNT, data.get(position).getCount());
args.putString(PageFragment.TITLE, data.get(position).getTitle());
args.putInt(PageFragment.PICTURE, data.get(position).getPicture());
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return data.size();
}
public static class PageFragment extends Fragment {
public static final String COUNT = "count";
public static final String TITLE = "title";
public static final String PICTURE = "picture";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item, container, false);
TextView title = rootView.findViewById(R.id.item_label);
title.setText(getArguments().getString(TITLE));
ImageView Picture = rootView.findViewById(R.id.image_view);
Picture.setImageResource(getArguments().getInt(PICTURE));
TextView count = rootView.findViewById(R.id.Nasir);
count.setText("SMS : " + getArguments().getString(COUNT) + " of 20");
return rootView;
}
}
}