检查图像没有显示在Gridview的项目上?

时间:2014-01-02 10:01:53

标签: android gridview

我尝试在Gridview上实现多项选择。

当我选择Gridview上的项目时,检查图像不会显示在该项目上。

getCheckedItemCount已添加此项目。我相信这个项目已被选中。

但我不知道为什么检查图像没有显示在该项目上。

以下是Fragment。

    public class FileBrowserFragment extends Fragment implements MultiChoiceModeListener{

        private GridView fileListView;
        private Map<Integer, Boolean> mSelectMap = new HashMap<Integer, Boolean>();
        private ArrayList<FileNode> mFileList = new ArrayList<FileNode>() ;
        private FileListAdapter mFileListAdapter ;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.browser, container, false) ;

            mFileListAdapter = new FileListAdapter(inflater, mFileList) ;

            mFileListAdapter.GridAdapter(getActivity());

            fileListView = (GridView) view.findViewById(R.id.browserList) ;
            fileListView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
            fileListView.setMultiChoiceModeListener((MultiChoiceModeListener) this);    
            fileListView.setNumColumns(3);

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub       
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // TODO Auto-generated method stub
            mFileListAdapter.notifyDataSetChanged();
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub

            return false;
        }

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position,
                long id, boolean checked) {
            // TODO Auto-generated method stub
            Log.i(TAG, "position = " + position);
            Log.i(TAG, "checked = " + checked);
            mSelectMap.put(position, checked);
            mode.invalidate();

        }

        public class FileListAdapter extends BaseAdapter {

            private Context mContext;
            private LayoutInflater mInflater ;
            private ArrayList<FileNode> mFileList ;
            private static final String TAG = "MJPEG Player" ;

            public FileListAdapter(LayoutInflater inflater, ArrayList<FileNode> fileList) {             
                mInflater = inflater ;
                mFileList = fileList ;          
            }

            public void GridAdapter(Context ctx) {
                mContext = ctx;
            }

            @Override
            public int getCount() {     
                return mFileList == null ? 0 : mFileList.size() ;
            }

            @Override
            public Object getItem(int position) {   
                return mFileList == null ? null : mFileList.get(position) ; 
            }

            @Override
            public long getItemId(int position) {
                return position ;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                ViewTag viewTag ;
                if (convertView == null) {

LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

                    convertView = mInflater.inflate(R.layout.filelist_row, null) ;

                    viewTag = new ViewTag(mContext , (ImageView) convertView.findViewById(R.id.fileListThumbnail) , mFileList.get(position));
                    convertView.setTag(viewTag) ;
                } else {
                    viewTag = (ViewTag) convertView.getTag() ;

                }

                viewTag.mFileNode = mFileList.get(position) ;
                viewTag.setChecked(mSelectMap.get(position) == null ? false : mSelectMap.get(position));
                Log.i(TAG, "mSelectMap.get(position) = " + mSelectMap.get(position));

                return convertView ;
            }


        }

    }

以下的ViewTag类有助于设置检查状态

public class ViewTag extends RelativeLayout implements Checkable{

    private static final String TAG = "MJPEG Player" ;
    ImageView mThumbnail ;
    private Context mContext;
    private ImageView mImgView = null;
    private ImageView mSecletView = null;
    private boolean mChecked;

    AsyncTask<ViewTag, Integer, Bitmap> mThumbnailTask ;

    FileNode mFileNode ;

    public ViewTag(Context context, ImageView thumbnail, FileNode fileNode) {
        this(context, null, 0, thumbnail, fileNode);
    }

    public ViewTag(Context context, AttributeSet attrs, int defStyle, ImageView thumbnail, FileNode fileNode) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.grid_item, this);
        mImgView = (ImageView) findViewById(R.id.img_view);
        mSecletView = (ImageView) findViewById(R.id.select);
        mThumbnail = thumbnail ;
        mFileNode = fileNode ;
    }

    @Override
    public boolean isChecked() {
        // TODO Auto-generated method stub
        Log.i(TAG, "isChecked");
        return mChecked;

    }

    @Override
    public void setChecked(boolean checked) {
        // TODO Auto-generated method stub
        mChecked = checked;
        mSecletView.setVisibility(checked ? View.VISIBLE : View.GONE);
        Log.i(TAG, "setChecked = " + mChecked);
    }


    @Override
    public void toggle() {
        // TODO Auto-generated method stub
        setChecked(!mChecked);
        Log.i(TAG, "toggle");
    }


}

id的{​​{1}}为check imageview且位于select

grid_item.xml的项目是GridviewfileListThumbnail。{/ p>

filelist_row.xml的{​​{1}}位于id

我省略了一些代码,但它仍然很长......

所以,也许有人可以教我方向。

问题是:

为什么检查图像视图会将GridView设置为browser.xml,但它不会显示setVisibility的项目。

VISIBLE是否可能被上图阻挡?

提前致谢。

0 个答案:

没有答案