RecyclerView.Adapter获取列表的项目位置

时间:2015-08-03 23:02:46

标签: android android-recyclerview android-adapter

我创建了一个from matplotlib import gridspec import matplotlib.pyplot as plt fig = plt.figure(figsize=(13,13)) gs = gridspec.GridSpec(nrows=3, ncols=1, height_ratios=[3,1,1]) for i in xrange (0,3): ax = fig.add_subplot(gs[i]) box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.5, box.height]) plt.legend(loc='upper left', bbox_to_anchor=(1, 1)) #fig.autofmt_xdate() plt.show() ,我想填写一个列表..所以我现在的问题是我不知道如何实现列表的获取位置。

你可以在下面看到我的代码:

RecyclerView.Adapter

所以函数public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { List<Result> _contents; public RecyclerViewAdapter(List<Result> contents) { this._contents = contents; } @Override public int getItemCount() { return _contents.size(); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) { View view = null; view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.list_item_card_small, parent, false); Result tempResult = _contents.get(position); TextView temp = (TextView) view.findViewById(R.id.text_city_name); temp.setText(tempResult.getInfo().getCity().getName()); return new RecyclerView.ViewHolder(view) { }; } @Override public long getItemId(int position) { return position; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { } } 没有得到正确的id总是相同的id ..如何解决这个问题或者实现以获得mi列表的正确位置?¿现在发生的事情是list具有正确数量的项目,但始终是相同的项目,第一项。我想这很简单,但我无法弄清楚如何实现它。

谢谢!

3 个答案:

答案 0 :(得分:3)

我不确定你为什么需要列表的位置。您可以在 onBindViewHolder 中获得该职位。但是,您可以将任何数据放在ViewHolder中,稍后再使用它们。

public class RecyclerViewAdapter extends RecyclerView.Adapter<ResultViewHolder> {
    List<Result> _contents;

    public RecyclerViewAdapter(List<Result> contents) {
        this._contents = contents;
    }

    public Result getItem(int position) {
        return _contents != null ? _contents.get(position) : null;
    }

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

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

    @Override
    public ResultViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_card_small, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ResultViewHolder holder, int position;) {
        Result result = getItem(position);
        holder.position = position;
        holder.result = result;
        holder.cityName.setText(result.getInfo().getCity().getName());
    }

    public static class ResultViewHolder extends RecyclerView.ViewHolder implements
            View.OnClickListener {
        int position = -1;
        Result result;
        TextView cityName;

        public ResultViewHolder(View view) {
            super(view);
            cityName = (TextView) view.findViewById(R.id.text_city_name);
            view.setOnClickListener(this);
        }

        public void onClick(View v) {
            // do whatever you want with list posisiton and result...
        }
    }
}

答案 1 :(得分:3)

我已经修复了实现我自己的ViewHolder的问题,我有点困惑。以下是正常运行的代码。

<UserControl.Resources>
   <!--First template-->
   <DataTemplate DataType="local:Tab1VM">
       <!-- I will have a DataGrid here-->
   </DataTemplate>

   <!--Second template-->
   <DataTemplate DataType="local:Tab2VM">
       <!-- I will have details of one item of the DataGrid-->
   </DataTemplate>
</UserControl.Resources>

答案 2 :(得分:1)

onBindViewHolder方法接收位置作为第二个参数。你可以用这种方法操纵你的视图。

package com.virgooll.avaa.shop.views;


import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.virgooll.avaa.R;
import com.virgooll.avaa.detailedIndex.model.StoreItem;

import java.util.List;

public class MasonryAdapter extends RecyclerView.Adapter<MasonryAdapter.StoreItemViewHolder> {
List<StoreItem> items;
int itemsIndex;

public MasonryAdapter(List<StoreItem> items) {
    this.items = items;
}

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

@Override
public MasonryAdapter.StoreItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    itemsIndex = i;
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.detailed_index_list_item, viewGroup, false);
    MasonryAdapter.StoreItemViewHolder pvh = new MasonryAdapter.StoreItemViewHolder(v);
    return pvh;
}

@Override
public void onBindViewHolder(MasonryAdapter.StoreItemViewHolder holder, int position) {
    holder.setImage(position);
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

public class StoreItemViewHolder extends RecyclerView.ViewHolder {
    StoreItemViewHolder(View itemView) {
        super(itemView);
    }

    public void setImage(int position) {
        ImageView image = (ImageView) itemView.findViewById(R.id.detailed_index_fragment_inner_list_view_image);
        image.setImageResource(items.get(position).photoId);
    }
}
}