0

I have a main layout with a Viewpager and an adapter so that I have multiple layouts and I can swipe between them. For the main layout I have a Java class which calls the Adapter:

public class Was_ist_Java extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_was_ist_java);

        viewPager = findViewById(R.id.viewPager);
        progressBar = findViewById(R.id.progressBar);

        mPageAdapter = new Adapter(layouts, this);
        viewPager.setAdapter(mPageAdapter);   
    Uebung()

and should handle multiple onclickListeners:

    private void Uebung() {
       View third = getLayoutInflater().inflate(R.layout.third, null, false);

       ImageButton reload = third.findViewById(R.id.reload);

       if(reload != null) {
           Log.d("reload", "reload");
       }

       reload.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
               Log.d("hier", "called");
          }
      });

   }

Because the button is on the third layout I have to inflate the third layout. The button is found but the onclickListener not get called

Edit: Call Fragment:

Fragment test = new UebungWieFunktioniertJava();
        getSupportFragmentManager().beginTransaction().replace(R.id.viewPager,
                test).commit();

Fragment:

public class UebungWieFunktioniertJava extends Fragment implements View.OnClickListener {

Button one;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
    View myView = inflater.inflate(R.layout.third, container, false);

    return myView;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    one = getView().findViewById(R.id.compiler);
    one.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    Log.d("meistrer", "moiun");
}

}

julianpjp
  • 112
  • 1
  • 8
  • ImageButton reload = third.findViewById(R.id.reload); you are defining this ImageButton to be in the R.layout.main_was_ist_java. You cannot listen to a click on another fragment/activity from another. You will have to use a callback, etc. In your activity you will be able to listen if your user changed page, for instance, but not listen to a event inside the fragment/adapter like this. – Henrique Vasconcellos May 12 '20 at 21:20
  • Could you please give me an example or a link? – julianpjp May 13 '20 at 17:23
  • I believe the easiest way is to create a clicklistener on the fragment you are creating with your viewpager https://stackoverflow.com/questions/34630063/how-to-set-onclick-listener-for-a-fragment – Henrique Vasconcellos May 13 '20 at 17:40
  • Thanks for your reply but I have the same problem findviewbyid worked but the onclicklistener not. I've edit my question and added the new code. – julianpjp May 13 '20 at 20:28
  • I will post a answer that is easier to see what I will propose. – Henrique Vasconcellos May 13 '20 at 21:09

1 Answers1

0

Move this code:

one = getView().findViewById(R.id.compiler);
one.setOnClickListener(this);

to onCreateView() like this:

one = (Button) myView.findViewById(R.id.compiler);
one.setOnClickListener(this);

Also, make sure that in this fragment's .xml R.id.compiler is setup as a button!

Another thing, the point in using the viewpager is that it will manage/display the fragments, so it makes no sense to navigate to it manually. Is your view displyaing properly?

Henrique Vasconcellos
  • 1,144
  • 1
  • 8
  • 13
  • 1
    Thanks for your help I found another solution. [link](https://stackoverflow.com/questions/18413309/how-to-implement-a-viewpager-with-different-fragments-layouts) – julianpjp May 16 '20 at 12:07