getAdapterPosition() 不返回项目在 RecyclerView 中的位置
getAdapterPosition() not returning position of item in RecyclerView
这是对 的一种跟进或补充。我试图在我的 RecyclerView 中获取一个项目的位置,但是 none 我尝试过的方法都有效。
我在我的 PersonViewHolder
构造函数中调用了 getAdapterPosition()
并将其值分配给整数位置,它在 UnitOneFragment
class 中用于相应地做一些事情(参见onClick()
方法)。但无论那是什么,它都不会发生,我的猜测是因为 getAdapterPosition()
方法不起作用或未正确使用。
我的适配器:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
Context mContext;
RVAdapter(Context context, List<Chapter> chapters) {
mContext = context;
this.chapters = chapters;
}
public static CardView cv;
public static int position;
public static String chapterNumberTitle;
public class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView chapterName;
TextView chapterNumber;
// ImageView chapterPhoto;
public PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
chapterName = (TextView) itemView.findViewById(R.id.chapter_name);
chapterNumber = (TextView) itemView.findViewById(R.id.chapter_number);
// chapterPhoto = (ImageView) itemView.findViewById(R.id.person_photo);
chapterNumberTitle = chapterNumber.getText().toString();
position = getAdapterPosition();
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
UnitOneFragment.chapterChecker();
Intent intent = new Intent(mContext, ChapterActivity.class);
mContext.startActivity(intent);
}
}
List<Chapter> chapters;
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.chapterName.setText(chapters.get(i).chapterName);
personViewHolder.chapterNumber.setText(chapters.get(i).chapterNumber);
// personViewHolder.chapterPhoto.setImageResource(persons.get(i).photoId);
}
@Override
public int getItemCount() {
return chapters.size();
}
}
其他class:
public class UnitOneFragment extends android.support.v4.app.Fragment {
private List<Chapter> chapters;
private RecyclerView rv;
private RecyclerView.LayoutManager llm;
public UnitOneFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_unit_one, container, false);
getActivity().setTitle("Unit One");
rv = (RecyclerView) v.findViewById(R.id.rv);
rv.setHasFixedSize(true);
llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
initializeData();
initializeAdapter();
return v;
}
private void initializeData() {
chapters = new ArrayList<>();
chapters.add(new Chapter("Human Prehistory to the Early Civilizations", "Chapter One"));
chapters.add(new Chapter("Classical China", "Chapter Two"));
chapters.add(new Chapter("Classical India", "Chapter Three"));
chapters.add(new Chapter("Classical Greece and Rome", "Chapter Four"));
chapters.add(new Chapter("Classical Period: Declines and Diversities", "Chapter Five"));
}
private void initializeAdapter() {
RVAdapter adapter = new RVAdapter(getActivity(), chapters);
rv.setAdapter(adapter);
}
public static void chapterChecker() {
if (RVAdapter.position == 0) {
SummaryFragment.summaryText.setText("Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.");
}
}
}
class Chapter {
String chapterName;
String chapterNumber;
// int photoId;
Chapter(String chaptername, String chapternumber) {
this.chapterName = chaptername;
this.chapterNumber = chapternumber;
// this.photoId = photoId;
}
}
我的代码似乎不言自明,但如果有任何令人困惑的地方,请告诉我,我会添加更多信息。再一次,问题是我无法获取项目在 RecyclerView 中的位置,然后对其进行操作。
在您的 onClick()
方法中调用 getAdapterPosition()
,因为在点击时您想知道项目的位置。如果您在创建时检查它(大多数时候)是不准确的。
我可以使用viewHolder.mView.setOnClickListener()
获取点击事件,然后使用viewHolder.getAdapterPosition ()
获取位置吗?我怎样才能从我单击的项目中获取一些数据,例如名称。
示例在这里完成:
@Override
public void onBindViewHolder(@NonNull CourseVideoAdapter.MyViewHolder holder, int position) {
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentPosition = holder.getAdapterPosition();
}
});
}
这是对
我在我的 PersonViewHolder
构造函数中调用了 getAdapterPosition()
并将其值分配给整数位置,它在 UnitOneFragment
class 中用于相应地做一些事情(参见onClick()
方法)。但无论那是什么,它都不会发生,我的猜测是因为 getAdapterPosition()
方法不起作用或未正确使用。
我的适配器:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
Context mContext;
RVAdapter(Context context, List<Chapter> chapters) {
mContext = context;
this.chapters = chapters;
}
public static CardView cv;
public static int position;
public static String chapterNumberTitle;
public class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView chapterName;
TextView chapterNumber;
// ImageView chapterPhoto;
public PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
chapterName = (TextView) itemView.findViewById(R.id.chapter_name);
chapterNumber = (TextView) itemView.findViewById(R.id.chapter_number);
// chapterPhoto = (ImageView) itemView.findViewById(R.id.person_photo);
chapterNumberTitle = chapterNumber.getText().toString();
position = getAdapterPosition();
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
UnitOneFragment.chapterChecker();
Intent intent = new Intent(mContext, ChapterActivity.class);
mContext.startActivity(intent);
}
}
List<Chapter> chapters;
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.chapterName.setText(chapters.get(i).chapterName);
personViewHolder.chapterNumber.setText(chapters.get(i).chapterNumber);
// personViewHolder.chapterPhoto.setImageResource(persons.get(i).photoId);
}
@Override
public int getItemCount() {
return chapters.size();
}
}
其他class:
public class UnitOneFragment extends android.support.v4.app.Fragment {
private List<Chapter> chapters;
private RecyclerView rv;
private RecyclerView.LayoutManager llm;
public UnitOneFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_unit_one, container, false);
getActivity().setTitle("Unit One");
rv = (RecyclerView) v.findViewById(R.id.rv);
rv.setHasFixedSize(true);
llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
initializeData();
initializeAdapter();
return v;
}
private void initializeData() {
chapters = new ArrayList<>();
chapters.add(new Chapter("Human Prehistory to the Early Civilizations", "Chapter One"));
chapters.add(new Chapter("Classical China", "Chapter Two"));
chapters.add(new Chapter("Classical India", "Chapter Three"));
chapters.add(new Chapter("Classical Greece and Rome", "Chapter Four"));
chapters.add(new Chapter("Classical Period: Declines and Diversities", "Chapter Five"));
}
private void initializeAdapter() {
RVAdapter adapter = new RVAdapter(getActivity(), chapters);
rv.setAdapter(adapter);
}
public static void chapterChecker() {
if (RVAdapter.position == 0) {
SummaryFragment.summaryText.setText("Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.");
}
}
}
class Chapter {
String chapterName;
String chapterNumber;
// int photoId;
Chapter(String chaptername, String chapternumber) {
this.chapterName = chaptername;
this.chapterNumber = chapternumber;
// this.photoId = photoId;
}
}
我的代码似乎不言自明,但如果有任何令人困惑的地方,请告诉我,我会添加更多信息。再一次,问题是我无法获取项目在 RecyclerView 中的位置,然后对其进行操作。
在您的 onClick()
方法中调用 getAdapterPosition()
,因为在点击时您想知道项目的位置。如果您在创建时检查它(大多数时候)是不准确的。
我可以使用viewHolder.mView.setOnClickListener()
获取点击事件,然后使用viewHolder.getAdapterPosition ()
获取位置吗?我怎样才能从我单击的项目中获取一些数据,例如名称。
示例在这里完成:
@Override
public void onBindViewHolder(@NonNull CourseVideoAdapter.MyViewHolder holder, int position) {
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentPosition = holder.getAdapterPosition();
}
});
}