带有基本适配器的GridView无法响应GridView中第一个项目的第一次单击,直到我滚动

时间:2016-04-18 08:08:14

标签: android gridview baseadapter android-viewholder

使用为网格创建子视图的基本适配器,持有者视图有多个可单击控件,每个都执行一个操作。

这里是getView()方法代码

public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.book_item_to_download, parent, false);
            convertView.setFocusable(true);
            convertView.setClickable(true);

            holderGridView = new BooksViewHolder();

            holderGridView.BookImage = (ImageView) convertView.findViewById(R.id.bookBackground);
            holderGridView.BookName = (TextView) convertView.findViewById(R.id.bookName);
            holderGridView.BookItemWraper = (LinearLayout) convertView.findViewById(R.id.book_item_wraper);
            holderGridView.BookDownload=(ImageView) convertView.findViewById(R.id.download);
            holderGridView.BookProgress=(CircularProgressBar) convertView.findViewById(R.id.demo_mpc);

            Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Isra-Thin.ttf");
            holderGridView.BookName.setTypeface(custom_font);
            convertView.setTag(holderGridView);
        } else {
            holderGridView = (BooksViewHolder) convertView.getTag();
        }

        holderGridView.BookItemWraper.setLayoutParams(new android.widget.AbsListView.LayoutParams((int) Math.round(ScreenWidth / 2.1), (int) Math.round(ScreenHeight / 2.5)));
        holderGridView.BookImage.setImageDrawable(getResources().getDrawable(R.drawable.book_background));
        holderGridView.BookName.setText(bookCovers.get(position).getBookTitle());

        ImageView image = holderGridView.BookImage;

        //DisplayImage function from ImageLoader Class
        imageLoader.DisplayImage(bookCovers.get(position).getBookImageURL(), image);

            if(bookCovers.get(position).isDownloaded)
            {
                holderGridView.BookDownload.setVisibility(View.GONE);
                holderGridView.BookProgress.setVisibility(View.GONE);
                holderGridView.BookItemWraper.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for(int i=0;i<app.bis.size();++i)
                        {
                            if(app.bis.get(i).identifier.equals(bookCovers.get(position).getBookID()+""))
                            {
                                openBookViewer(app.bis.get(i));
                                break;
                            }
                        }
                    }
                });
            }
            else if(bookCovers.get(position).isDownloading)
            {
                holderGridView.BookDownload.setVisibility(View.GONE);
                holderGridView.BookProgress.setVisibility(View.VISIBLE);
                holderGridView.BookProgress.setProgress(bookCovers.get(position).downloadProgress);
                holderGridView.BookItemWraper.setOnClickListener(null);
            }
            else {
                holderGridView.BookDownload.setVisibility(View.VISIBLE);
                holderGridView.BookProgress.setVisibility(View.GONE);

                holderGridView.BookItemWraper.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent openDetails = new Intent(getActivity(), WaitingBookDetails_Activity.class);
                        openDetails.putExtra(Constants.BOOK_COVER, Parcels.wrap(bookCovers.get(position)));
                        startActivity(openDetails);
                    }
                });

                holderGridView.BookDownload.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new downloadEPUB_Task(bookCovers.get(position), position).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                    }
                });
            }

        return  convertView;
    }

当GridView首次初始化时,如果我点击持有人视图中的任何控件上的第一项,它将不会响应,直到我稍微滚动GridView,注意:只有第一次点击第一项时最初的应用程序打开比其余的点击很好,我正在使用LazyLoading将图像加载到持有人视图中的ImageView,即使在慢速设备上滚动性能也很好,所以我猜它不是一个记忆。 提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

从getView()方法中删除以下行并尝试;

convertView.setFocusable(true);
convertView.setClickable(true);

在代码的其他部分之外添加以下点击列表器并尝试,

holderGridView.BookItemWraper.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent openDetails = new Intent(getActivity(), WaitingBookDetails_Activity.class);
                    openDetails.putExtra(Constants.BOOK_COVER, Parcels.wrap(bookCovers.get(position)));
                    startActivity(openDetails);
                }
            });

            holderGridView.BookDownload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new downloadEPUB_Task(bookCovers.get(position), position).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                }
            });