每次在OnClick()方法中,getAdapterPosition()都返回-1

时间:2016-10-31 10:26:16

标签: java android google-maps android-recyclerview

今天我遇到了这个问题:AdapterPosition永远不会改变,当我点击我的recyclerview中的一个项目时,它总是-1。

public class RecyclerMapAdapter extends RecyclerView.Adapter<RecyclerMapAdapter.ViewHolder> {
    Context context;
    ArrayList<Features> features;
    ImageLoader imageLoader;
    public static String TAG = MapsActivity.class.getSimpleName();


    public RecyclerMapAdapter(Context context, ArrayList<Features> features) {
        this.context = context;
        this.features = features;
    }

    @Override
    public RecyclerMapAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.map_row, parent, false);
        final RecyclerMapAdapter.ViewHolder viewHolder = new RecyclerMapAdapter.ViewHolder(v);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListMapActivity mapsActivity = (ListMapActivity) context;
                Intent intent = new Intent(mapsActivity, DescriptionMapActivity.class);

                Features feature = features.get(viewHolder.getAdapterPosition());
                MyProperties myProperties = feature.getProperties();
                intent.putExtra("data", myProperties);
                mapsActivity.startActivity(intent);
            }
        });

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Features feature = features.get(position);
        MyProperties myProperties = feature.getProperties();
        holder.title_map.setText(myProperties.getName());

        imageLoader = ServerImageAdapter.getInstance(context).getImageLoader();
        imageLoader.get(myProperties.getImage_url(),
                ImageLoader.getImageListener(
                        holder.iv_map,//Server Image
                        R.mipmap.ic_launcher,//Before loading server image the default showing image.
                        android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
                )
        );

        holder.iv_map.setImageUrl(myProperties.getImage_url(), imageLoader);

    }

    @Override
    public int getItemCount() {
        return features.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        public TextView title_map;
        public TextView dist;
        public NetworkImageView iv_map;

        public ViewHolder(View itemView) {
            super(itemView);
            title_map = (TextView) itemView.findViewById(R.id.tv_map_list);
            dist = (TextView) itemView.findViewById(R.id.dist_map);
            iv_map = (NetworkImageView) itemView.findViewById(R.id.map_row_image);

        }
    }
}

为了帮助您了解上下文,我的内容如何运作:   我在带有标记的地图上,我可以通过单击每个标记的InfoWindow来打开新的活动,我还有一个菜单项,用所有标记打开recyclerview,当我点击列表中的项目时应该打开与点击infoWindow时相同的活动。 除了点击列表中的项目,因为getAdapterPosition()返回-1,我拥有一切。 我已经将recyclelerview的这种实现用于其他东西并且它工作正常唯一的不同之处是一个是片段(哪个有效)而另一个是活动(不是没有)。

1 个答案:

答案 0 :(得分:1)

  

您正在创建一个viewHolder,对其执行操作。但最后,您将全新的视图持有者实例发送到RecyclerView

@Override
public RecyclerMapAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.map_row, parent, false);
    final RecyclerMapAdapter.ViewHolder viewHolder = new RecyclerMapAdapter.ViewHolder(v);
    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ListMapActivity mapsActivity = (ListMapActivity) context;
            Intent intent = new Intent(mapsActivity, DescriptionMapActivity.class);

            Features feature = features.get(viewHolder.getAdapterPosition());
            MyProperties myProperties = feature.getProperties();
            intent.putExtra("data", myProperties);
            mapsActivity.startActivity(intent);
        }
    });

    return viewHolder;
}