android - 如何在 MainActivity 页面使用 ViewPager 按钮?

标签 android firebase-realtime-database android-viewpager

如何在 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/

相关文章:

android - 从广播接收器启动的 Activity 是否使用与应用程序相同的堆?

android - 无法运行示例 rhodes 应用程序

javascript - 更改 reactjs 组件状态中字段的值(使用从 firebase 获取的数据)

java - 如何返回 DataSnapshot 值作为方法的结果?

android - 测验图像答案

android - 在我的 Android 应用程序中接收来电通知

android - 将数据保存在模型类或来自 Firebase 的列表中

android - FragmentPagerAdapter 与 OffScreenLimit 设置为 1 的 ViewPager 和 FragmentStatePagerAdapter 之间的区别?

Android ViewPager 根据屏幕尺寸显示多个 fragment

java - 从 ViewPager fragment 刷新 Activity