如何在 MainActivity 中使用 Viewpager Adapter Button?我正在开发一个测验应用程序,并将按钮值与 MainActivity 进行比较。如何访问 MainActivity 中的 ViewPagerAdapter 类按钮?
@Override
protected void onStart() {
super.onStart();
Ref.child("Exam_Category").child("PRACTICE TEST").child("PREVIOUS YEAR PAPER'S").child(ct1).child(ct2).child("ENGLISH COMPREHENSION").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot data : dataSnapshot.getChildren()) {
key = data.getKey().toString();
Ref.child("Exam_Category").child("PRACTICE TEST").child("PREVIOUS YEAR PAPER'S").child(ct1).child(ct2).child("ENGLISH COMPREHENSION").child(String.valueOf(key))
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
final questions studentDetails = dataSnapshot.getValue(questions.class);
list.add(studentDetails);
adapter = new RecyclerViewAdapter(Previous_Yr_Quiz.this, list);
viewPager.setAdapter(adapter);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
//total_q.setText(Question_No +" / "+key);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
这是我的 ViewPager 适配器
public class RecyclerViewAdapter extends PagerAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
public List<questions> list;
public RecyclerViewAdapter(Context context, List<questions> TempList) {
this.context = context;
this.list = TempList;
}
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
// Declare Variables
TextView question;
final Button option1,option2,option3,option4;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.quiz_layout_listview, container,
false);
// Locate the TextViews in viewpager_item.xml
question = (TextView)itemView.findViewById(R.id.question);
option1 = (Button) itemView.findViewById(R.id.optiona);
option2 = (Button) itemView.findViewById(R.id.optionb);
option3 = (Button) itemView.findViewById(R.id.optionc);
option4 = (Button) itemView.findViewById(R.id.optiond);
// Capture position and set to the TextViews
questions studentDetails = list.get(position);
question.setText(studentDetails.getQuestion());
option1.setText(studentDetails.getOption1());
option1.setId(position);
option2.setText(studentDetails.getOption2());
option3.setText(studentDetails.getOption3());
option4.setText(studentDetails.getOption4());
option1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
option1.setBackgroundResource(R.drawable.wrong);
}
});
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
private int getItem(int i) {
return list.size();
}
}
如何在 MainActivity 中使用按钮选项 A、B、C、D OnClick,因为我将按钮值与 A、B、C、D 按钮进行比较....
最佳答案
你有两个选择:
1) 将 Button 设为类成员,并通过 get 方法从 Activity 访问它们。
2) 编写一个具有回调方法的接口(interface),这样您就知道哪个按钮被单击了。编写接口(interface)后,将其作为构造函数参数或通过 set 方法传递。像这样的东西:
interface QuizListener {
void answered(Button whichButton);
}
...
[RecyclerViewAdapter.java]
public RecyclerViewAdapter(Context context, List<questions> TempList, QuizListener quizListener) {
this.context = context;
this.list = TempList;
this.quizListener = quizListener;
}
...
option1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (quizListener != null) {
quizListener.answered(option1);
}
option1.setBackgroundResource(R.drawable.wrong);
}
});
关于android - 如何在 MainActivity 页面使用 ViewPager 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55301221/