0

Quick Question. I wrote some View Classes and included them in a Fragment via XML.These View Classes are getting inflated.

private void inflateView(Context context){
    inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.layout_motoreffiziens_view, this, true);
}

In the onCreateView of the my fragment I get the View reference by findViewById. To fullfill some task i implemented a listener for my View which I can add with my intern method addListeners(myCustomListener).

My Problem is now that I cant call methods (because its obviously a View and not a ClassReference).

My View Header

public class MotorSelection  extends RelativeLayout implements Spinner.OnItemSelectedListener {

private LayoutInflater inflater;
private SpinnerChange spinnerChangeListener = null ;

This View is nested in my Fragment XML :

<com.example.andy.einzelprojekt1.views.MotorSelection
    android:id="@+id/view_motor_selection"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

So my Question is how I can use my Methods from inflated Views? More specific:

In the onCreate of my Fragment I want to acess this view and add a listener.

Greetings

/e 20.5.16

Offset
  • 609
  • 1
  • 5
  • 22
  • I think you just need to cast your view to your class which extends View, but you should provide more code to be sure and to better help you – Lorenzo Barbagli May 20 '16 at 16:39

2 Answers2

0

Can you please post code of your View class?
Actually, inflated view is instance of your class, so you should be able to call view.yourMethod().

Another way (if I am missing something in your question), is to use Reflection.
For example, this.

Community
  • 1
  • 1
Goltsev Eugene
  • 3,325
  • 6
  • 25
  • 48
0

Okay stupid mistake. I castet my View to the extended Object (in my Case RelativeLayout). Because of that I couldnt access my methods declared in the new ViewClass.

motorselection = (MotorSelection) rootView.findViewById(R.id.view_motor_selection);

/answered

Offset
  • 609
  • 1
  • 5
  • 22