RecyclerView.Adapter 获取我列表中的项目位置
RecyclerView.Adapter get item position of my list
我创建了一个 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) {
}
}
所以函数 onCreateViewHolder
没有得到正确的 ID 总是相同的 ID.. 我该如何解决这个问题或实现以获得 mi 列表的正确位置?列表中的项目数量正确,但始终是相同的项目,即第一个。我想这很简单,但我不知道如何实现它。
谢谢!!
我不确定您为什么需要列表的位置。您可以在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...
}
}
}
我已经解决了实现我自己的 ViewHolder 的问题,我有点困惑。下面是正常工作的代码。
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
List<Result> _contents;
public RecyclerViewAdapter(List<Result> contents) {
this._contents = contents;
}
@Override
public int getItemCount() {
return _contents.size();
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_card_small, parent, false);
return new RecyclerViewAdapter.ViewHolder(view);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Result tempResult = _contents.get(position);
holder.mTextView.setText(tempResult.getInfo().getCity().getName());
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.text_city_name);
}
}
}
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);
}
}
}
我创建了一个 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) {
}
}
所以函数 onCreateViewHolder
没有得到正确的 ID 总是相同的 ID.. 我该如何解决这个问题或实现以获得 mi 列表的正确位置?列表中的项目数量正确,但始终是相同的项目,即第一个。我想这很简单,但我不知道如何实现它。
谢谢!!
我不确定您为什么需要列表的位置。您可以在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...
}
}
}
我已经解决了实现我自己的 ViewHolder 的问题,我有点困惑。下面是正常工作的代码。
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
List<Result> _contents;
public RecyclerViewAdapter(List<Result> contents) {
this._contents = contents;
}
@Override
public int getItemCount() {
return _contents.size();
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_card_small, parent, false);
return new RecyclerViewAdapter.ViewHolder(view);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Result tempResult = _contents.get(position);
holder.mTextView.setText(tempResult.getInfo().getCity().getName());
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.text_city_name);
}
}
}
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);
}
}
}