我有一个网格片段,其图像有点像Instagram个人资料的这一部分。当前,当您单击图像时,无论您单击网格中的哪个图像,它都将带您到ArrayList中的第一张图像,通常您可以从那里向后滑动到第四张图像。我无法弄清楚如何使其相对于ArrayList中的索引单击的图像。因此,如果用户想要拉出网格上的最后一个图像,则必须从头开始并一直滑动到ArrayList的末尾。 PortraitPagerActivity中的InstantiateItem()方法的行为非常奇怪。当您第一次单击图库片段时,它实际上运行了两次,第一次位置为0,第二次位置为1。有时向后滑动,它不会运行,并且position的值不是应该的,而是向后轻扫,第四步行为应有。
public class PortraitPagerAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
ArrayList<String> arrayList;
public PortraitPagerAdapter(Context context, ArrayList<String> arrayList) {
this.context = context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.arrayList = arrayList;
Log.d("ThisTag", "PortraitPagerAdapter's constructor just finished");
}
@Override
public int getCount() {
if(arrayList != null){
return arrayList.size();
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = layoutInflater.inflate(R.layout.image_viewpager_layout, container, false);
TouchImageView imageView = itemView.findViewById(R.id.viewPagerItem_image1);
Log.d("ThisTag", "PortraitPagerAdapter value of position: " + position);
Picasso.get().load(arrayList.get(position)).into(imageView);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
public class CriminalGalleryFragment extends Fragment {
private ProgressBar mProgressBar;
private static final String SERVER_URL = "server_inflate_gallery";
private static final String BASE_URL = "server_300px_size_images";
private GridView mGridView;
private static final int NUM_GRID_COLUMNS = 3;
ArrayList<String> imgURLs = new ArrayList<>();
OnGridImageSelectedListener mOnGridImageSelectedListener = new OnGridImageSelectedListener() {
@Override
public void onGridImageSelected(String photo) {
Intent intent = new Intent(getActivity(), PortraitPagerActivity.class);
intent.putExtra("portraits", imgURLs);
intent.putExtra("selected_portrait", photo);
startActivity(intent);
}
};
OnDataPass dataPasser;
public interface OnGridImageSelectedListener{
void onGridImageSelected(String photo);
}
public interface OnDataPass {
public void onDataPass(ArrayList<String> imgURLs);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
dataPasser = (OnDataPass) context;
}
public void passData(ArrayList<String> imgURLs) {
dataPasser.onDataPass(imgURLs);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
UniversalImageLoader universalImageLoader = new UniversalImageLoader(getActivity());
ImageLoader.getInstance().init(universalImageLoader.getConfig());
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_criminal_gallery, container, false);
mGridView = v.findViewById(R.id.gridView);
mProgressBar = v.findViewById(R.id.gridImageProgressbar);
if (savedInstanceState == null) {
UniversalImageLoader universalImageLoader = new UniversalImageLoader(getActivity());
ImageLoader.getInstance().init(universalImageLoader.getConfig());
final int criminal_ID = getArguments().getInt("criminal_ID");
StringRequest stringRequest =
new StringRequest(
Request.Method.POST,
SERVER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++) {
String portraitFromPHP = array.getString(i);
String fullImageURL = BASE_URL + portraitFromPHP;
imgURLs.add(fullImageURL);
}
setUpImageGrid(imgURLs);
passData(imgURLs);
} catch (JSONException e) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("criminal_ID", String.valueOf(criminal_ID));
return params;
}
};
MySingleton.getInstance(getActivity()).addToRequestQueue(stringRequest);
} else {
setUpImageGrid(imgURLs);
}
return v;
}
private void setUpImageGrid(final ArrayList<String> imgURLs) {
Activity activity = getActivity();
if(activity != null){
int gridWidth = getResources().getDisplayMetrics().widthPixels;
int imageWidth = gridWidth/NUM_GRID_COLUMNS;
mGridView.setColumnWidth(imageWidth);
GridImageAdapter adapter = new GridImageAdapter(getActivity(), R.layout.layout_grid_imageview, "", imgURLs);
mGridView.setAdapter(adapter);
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mOnGridImageSelectedListener.onGridImageSelected(imgURLs.get(position));
Log.d("ThisTag", "CriminalGalleryFragment value of position: " + position);
}
});
}
}
}
public class PortraitPagerActivity extends AppCompatActivity {
private static ViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_portrait);
Intent intent = getIntent();
ArrayList<String> portraits;
portraits = intent.getStringArrayListExtra("portraits");
ArrayList<String> fullPortraits = new ArrayList<>();
for (String portrait : portraits) {
fullPortraits.add(portrait.replace("portrait300", "portrait"));
}
PortraitPagerAdapter portraitPagerAdapter = new PortraitPagerAdapter(PortraitPagerActivity.this, fullPortraits);
mPager = findViewById(R.id.portraitPager);
mPager.setAdapter(portraitPagerAdapter);
Log.d("ThisTag", "onCreate in PortraitPagerActivity just finished");
}
}