我想从ViewPager分享当前项目,但我无法获取当前图片并进行分享。
分享按钮仅在促销页面的操作栏上,它将与ViewPager共享当前项目。
这是我的适配器:
class CustomPagerAdapter extends PagerAdapter {
int[] mResources = {
R.drawable.abc1,
R.drawable.abc2
};
Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mResources.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.slidingimages_layout, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
这是我的Fragment Java代码:
public class PromotionFragment extends Fragment {
Menu menu;
private PopupWindow Pro_popup;
public PromotionFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.toolbar_menu, menu);//Menu Resource, Menu
super.onCreateOptionsMenu(menu,inflater);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_promotion, container, false);
CustomPagerAdapter adapter = new CustomPagerAdapter(getActivity());
ViewPager viewpager = (ViewPager) view.findViewById(R.id.pager);
viewpager.setAdapter(adapter);
LinePageIndicator titleIndicator = (LinePageIndicator)view.findViewById(R.id.indicator);
titleIndicator.setViewPager(viewpager);
return view;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
final int height = size.y;
RelativeLayout pro_popup = (RelativeLayout) getActivity().findViewById(R.id.pro_popup);
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
View pro_popupview = inflater.inflate(R.layout.promotion_popup, null);
TextView qr_title = (TextView) pro_popupview.findViewById(R.id.qr_title);
TextView qr_con = (TextView) pro_popupview.findViewById(R.id.qr_con);
RelativeLayout popupbg2 = (RelativeLayout) pro_popupview.findViewById(R.id.prepopup);
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qr_code);
Button scan_btn = (Button) pro_popupview.findViewById(R.id.scan_btn);
scan_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
Button dismiss = (Button) pro_popupview.findViewById(R.id.propop_dismiss);
dismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Pro_popup.dismiss();
}
});
switch (item.getItemId()) {
case R.id.qr_scan:
qr_title.setText(R.string.propop_title);
qr_con.setText(R.string.propop_con);
Pro_popup = new PopupWindow(pro_popupview);
Pro_popup.setWidth(width - 80);
Pro_popup.setHeight(height - 400);
Pro_popup.setOutsideTouchable(true);
Pro_popup.setFocusable(true);
Pro_popup.showAtLocation(pro_popup, Gravity.CENTER, 0, -60);
return true;
case R.id.toolbarshare:
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
startActivity(Intent.createChooser(shareIntent, "Share image using"));
CustomPagerAdapter adapter = new CustomPagerAdapter(getActivity());
ViewPager viewpager = (ViewPager)getActivity().findViewById(R.id.pager);
viewpager.setAdapter(adapter);
int position = viewpager.getCurrentItem();
if(position == 0)
{
final File photoFile = new File(getActivity().getFilesDir(), "abc1.png");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
}
else
{
final File photoFile = new File(getActivity().getFilesDir(), "abc1.png");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
}
return true;
}
return false;
}
答案 0 :(得分:0)
当用户点击分享按钮时,您正在viewpager中设置适配器
CustomPagerAdapter adapter = new CustomPagerAdapter(getActivity());
ViewPager viewpager = (ViewPager)getActivity().findViewById(R.id.pager);
viewpager.setAdapter(adapter);
删除此代码。
当用户点击分享按钮时,您只需获得ViewPager
的位置
viewpager.getCurrentItem();
确保全局定义viewpager变量,以便您可以在fragment中的任何位置访问它。
还有一件事
startActivity(Intent.createChooser(shareIntent, "Share image using"));
它应该在你的条件结束之前返回陈述。