ViewHolder getAdapterLocation没有得到解决

时间:2017-02-08 09:19:30

标签: android android-recyclerview

我使用版本25的RecylerView,build.gradle显示为:

compile 'com.android.support:design:25.1.0'
compile 'com.android.support:recyclerview-v7:25.1.0'

sdk版本 25.0.1 ,一切顺利,功能正常。 现在,我试图通过在getAdapterPosition()内的onClick方法内调用onBindViewHolder来查看项目的位置,但它没有显示该方法。

据我所知,getAdapterPosition是在第22版中引入的,应该可行。请建议什么是错的。

2 个答案:

答案 0 :(得分:1)

你可以尝试一下

@Override
public void onBindViewHolder(CartViewHolder holder, int position) {
    // Call getAdapterPosition() with holder context 
    holder.getAdapterPosition()
}

答案 1 :(得分:0)

/* main.cpp */

#include "smart_handle.h"
#include <unordered_map>
#include <Windows.h>

struct MetadataStruct
{
    MetadataStruct& operator=(MetadataStruct&& other)
    {
        if (this != &other)
        {
            handle = std::move(other.handle);
            // Invalidate other.handle somehow...
        }
        return *this;
    }

    Util::SmartHandle handle;
    // Also has other members.
};

std::unordered_map<DWORD, MetadataStruct> metadataStructs;

Util::SmartHandle GetHandle()
{
    // Doing lots of stuff here to get the handle, then finally wraps
    // it before returning, hence the need for its own function.
    const HANDLE openedFileHandle = (HANDLE)-1; // Just an example handle.
    return std::move(Util::SmartHandle(openedFileHandle, NULL));
}

void F()
{
    MetadataStruct metadataStruct;
    metadataStruct.handle = GetHandle();

    metadataStructs.emplace(0, std::move(metadataStruct));
}

int main(int argc, char** argv)
{
    F();
    return 0;
}

/* smart_handle.h */
#pragma once

#include <stdexcept>
#include <Windows.h>

namespace Util
{
    class SmartHandle
    {
    public:
        SmartHandle() = default;
        SmartHandle(HANDLE handle, HANDLE invalidHandleValue);

        SmartHandle(const SmartHandle& other) = delete;
        SmartHandle& operator=(const SmartHandle& other) = delete;

        SmartHandle(SmartHandle&& other);
        SmartHandle& operator=(SmartHandle&& other);

        ~SmartHandle();

        HANDLE GetValue() const;
        bool IsValid() const;
        void Close();
    private:
        static const HANDLE uninitializedHandleValue;

        HANDLE handle = uninitializedHandleValue;
        HANDLE invalidHandleValue = uninitializedHandleValue;

        void Set(HANDLE handle, HANDLE invalidHandleValue, bool throwIfInvalid = true);
    };
}

/* smart_handle.cpp */

#include "smart_handle.h"

namespace Util
{
    const HANDLE SmartHandle::uninitializedHandleValue = (HANDLE)-2;

    SmartHandle::SmartHandle(const HANDLE handle, const HANDLE invalidHandleValue)
    {
        Set(handle, invalidHandleValue);
    }

    SmartHandle::SmartHandle(SmartHandle&& other)
    {
        handle = other.handle;
        invalidHandleValue = other.invalidHandleValue;

        other.handle = uninitializedHandleValue;
        other.invalidHandleValue = uninitializedHandleValue;
    }

    SmartHandle& SmartHandle::operator=(SmartHandle&& other)
    {
        if (this != &other)
        {
            handle = other.handle;
            invalidHandleValue = other.invalidHandleValue;

            other.handle = uninitializedHandleValue;
            other.invalidHandleValue = uninitializedHandleValue;
        }
        return *this;
    }

    SmartHandle::~SmartHandle()
    {
        Close();
    }

    void SmartHandle::Set(const HANDLE handle, const HANDLE invalidHandleValue, const bool throwIfInvalid)
    {
        this->handle = handle;
        this->invalidHandleValue = invalidHandleValue;

        if (throwIfInvalid && !IsValid())
        {
            this->handle = uninitializedHandleValue;
            this->invalidHandleValue = uninitializedHandleValue;
            throw std::invalid_argument("The handle used to initialize the object is not a valid handle");
        }
    }

    HANDLE SmartHandle::GetValue() const
    {
        if (handle == uninitializedHandleValue)
        {
            throw std::exception("Handle value not initialized");
        }

        return handle;
    }

    bool SmartHandle::IsValid() const
    {
        return handle != uninitializedHandleValue && handle != invalidHandleValue;
    }

    void SmartHandle::Close()
    {
        const bool isPseudoHandle = handle == (HANDLE)-1;

        if (IsValid() && !isPseudoHandle)
        {
            if (!CloseHandle(handle))
            {
                throw std::exception("CloseHandle failed");
            }

            handle = invalidHandleValue;
        }
    }
}