画像の GridView を持つアプリに取り組んでいますが、GridView の最後のアイテムは、より多くのアイテムをグリッドにロードするボタンです。しかし、私は奇妙な問題に直面しています。それは、GridViewで非常に速く上下にスクロールすると、ボタンが表示される場合と表示されない場合があります。リサイクルの問題だと思いますが、直し方がわかりません。
これが私のコードです:-
public class ButtonGridAdapter extends BaseAdapter implements OnCheckedChangeListener, OnClickListener {
private ArrayList<String> thumbUrls;
private ArrayList<String> mainUrls;
private LayoutInflater layoutInflater;
private Context context;
private Intent intent;
private ArrayList<String> positions = new ArrayList<String>();
private static ArrayList<String> urlsToUpload = new ArrayList<String>();
private int width;
public ButtonGridAdapter(Context context, ArrayList<String> thumbUrls, ArrayList<String> mainUrls, String clicked)
{
this.context = context;
this.thumbUrls = thumbUrls;
this.mainUrls = mainUrls;
intent = new Intent(context, GalleryView.class);
this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity) context).getWindow().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
this.width = displaymetrics.widthPixels;
System.out.println(width+" width");
}
@Override
public int getCount() {
return thumbUrls.size()+1;
}
@Override
public Object getItem(int arg0) {
return thumbUrls.get(arg0);
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder
{
ImageView mainImage;
CheckBox checkBox;
Button button;
}
public static class ButtonHolder
{
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if(row==null || row.getTag().equals("button"))
{
row = layoutInflater.inflate(R.layout.griditem, parent, false);
holder = new ViewHolder();
holder.mainImage = (ImageView)row.findViewById(R.id.itemgrid);
holder.checkBox = (CheckBox)row.findViewById(R.id.itemselected);
holder.button = null;
row.setTag(holder);
}
else
{
holder = (ViewHolder)row.getTag();
}
if(position==thumbUrls.size())
{
int width = row.getWidth();
int height = row.getHeight()-70;
row = layoutInflater.inflate(R.layout.mybutton, parent, false);
holder.button = (Button)row.findViewById(R.id.gridbutton);
holder.button.setText("Load More");
LayoutParams params = holder.button.getLayoutParams();
params.height = height;
params.width = width;
holder.button.requestLayout();
holder.button.setOnClickListener(this);
System.out.println("Called");
row.setTag("button");
return row;
}
else
{
setImageSize(holder.mainImage);
System.out.println(position);
holder.mainImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intent.putExtra("position", position);
intent.putStringArrayListExtra("MainUrls", mainUrls);
intent.putStringArrayListExtra("Thumbnails", thumbUrls);
context.startActivity(intent);
}
});
holder.checkBox.setOnCheckedChangeListener(null);
holder.checkBox.setTag(position);
holder.checkBox.setChecked(positions.contains(thumbUrls.get(position)));
holder.checkBox.setOnCheckedChangeListener(this);
UrlImageViewHelper.setUrlDrawable(holder.mainImage, thumbUrls.get(position), R.drawable.extras_load);
return row;
}
}
private void setImageSize(ImageView imageView) {
LayoutParams params = imageView.getLayoutParams();
int leftWidth = width - 80;
params.width = leftWidth/3;
params.height = leftWidth/3;
imageView.requestLayout();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
positions.add(thumbUrls.get((Integer)buttonView.getTag()));
urlsToUpload.add(mainUrls.get((Integer)buttonView.getTag()));
}
else
{
positions.remove(thumbUrls.get((Integer)buttonView.getTag()));
urlsToUpload.remove(mainUrls.get((Integer)buttonView.getTag()));
}
}
public static ArrayList<String> getUrlsToUpload()
{
return urlsToUpload;
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.gridbutton)
{
InstagramImpl.getInstance().fetchMorePhotos();
}
}
}
どんな助けでも本当に感謝しています。
ありがとう。