无法实现:如果在MainActivity中点击图像 - 此图像必须在PreviewActivity全屏幕中打开,我可以左右滚动使用ViewPager浏览阵列中的下一个或上一个图像。现在,当我点击图片,如果在PageFragment
- resultView.setImageResource(pageNumber);
中使用 - 然后总是在我的数组中打开firts图像,如果使用resultView.setImageURI(source);
- 然后打开真实图像但是如果滑动 - 总是显示这一个图像。
PageFragment - 可能在这个课程中出错了
public class PageFragment extends Fragment {
private static final String ARGUMENT_PAGE_NUMBER = "id_test";
private int pageNumber;
private Uri source;
static PageFragment newInstance(int page) {
PageFragment pageFragment = new PageFragment();
Bundle arguments = new Bundle();
arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
pageFragment.setArguments(arguments);
return pageFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getActivity().getIntent();
pageNumber = i.getExtras().getInt("id_test");
source = Uri.parse("android.resource://com.test.nico/drawable/" + Images.loadImages[pageNumber]);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view_pager, null);
ImageView resultView = (ImageView) view.findViewById(R.id.result_image);
// resultView.setImageURI(source);
pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER);
resultView.setImageResource(pageNumber);
return view;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private Context mContext;
private RecyclerView mRecyclerView;
private ImageAdapter mImageAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mImageAdapter = new ImageAdapter(mContext, Images.loadImages);
mLayoutManager = new GridLayoutManager(this, 2);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mImageAdapter);
}
}
PreviewActivity
public class PreviewActivity extends FragmentActivity {
private ViewPager viewPager;
private PagerAdapter pagerAdapter;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preview);
viewPager = (ViewPager) findViewById(R.id.preview_pager);
pagerAdapter = new PreviewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
}
}
ImageAdapter
class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.Holder> {
private int[] mItemList;
private Context mContext;
ImageAdapter(Context context, int[] itemList) {
this.mContext = context;
this.mItemList = itemList;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_list, null);
return new Holder(layoutView);
}
@Override
public void onBindViewHolder(Holder holder, int position) {
Picasso.with(mContext).load(Images.loadImages[position]).placeholder(R.drawable.ic_stub).resize(400, 750).into(holder.view);
}
@Override
public int getItemCount() {
return this.mItemList.length;
}
class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView view;
Holder(View itemView) {
super(itemView);
mContext = itemView.getContext();
view = new ImageView(mContext);
view = (ImageView) itemView.findViewById(R.id.recycler_view);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
final Intent intent = new Intent(mContext, PreviewActivity.class);
intent.putExtra("id_test", getAdapterPosition());
mContext.startActivity(intent);
}
}
}
PreviewPagerAdapter
class PreviewPagerAdapter extends FragmentStatePagerAdapter {
private int imagesCount = Images.loadImages.length;
private int[] images = Images.loadImages;
PreviewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return imagesCount;
}
@Override
public Fragment getItem(int position) {
return PageFragment.newInstance(images[position]);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
答案 0 :(得分:1)
在你的PreviewActivity
中,你必须“跳”到你经过的位置。
viewpager.setCurrentItem(getIntent().getExtras().getInt("id_test"));