Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I communicate between a class and a fragment which uses it?

I'm using Android Studio. I haven't been able to find an answer online, so even a link to a solution would be helpful.

I have an Activity, which includes a number of Fragments. One of these Fragments is called BookGridFragment, which uses a class called BookGrid.

BookGridFragment looks like this (I've left out irrelevant bits):

public class BookGridFragment extends Fragment {

    BookGrid myBookGrid;

    public BookGridFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        // Inflate layout
        View rootView = inflater.inflate(
                R.layout.fragment_book_grid, container, false);

        myBookGrid = rootView.findViewById(book_grid);

        return rootView;
    }

    public void setBook(Book thisBook) {
        myBookGrid.setBook(thisBook);
    }
}

The BookGrid class is:

public class BookGrid extends View {

    private Book mBook;

    public BookGrid(Context thisContext, AttributeSet attrs) {
        super(thisContext, attrs);
    }

    public void setBook(Book newBook) {
        mBook = newBook;
    }

    protected void onDraw(Canvas canvas) {

        if (mBook == null) return; 

        canvas.save();

        draw_book_details(); 
        // draw_book_details() is a function which just takes 
        //  the book info and displays it in a grid

        canvas.restore();
    }

   public boolean onTouchEvent(MotionEvent event) {

       // This function responds to the user tapping a piece of
       //  book info within the grid

       // THIS IS WHERE I'M HAVING PROBLEMS
   }
}

So, that all works fine. The issue is, that I need the BookGridFragment to know when the user touches the BookGrid and to pass that information to another Fragment (via the Activity). So, I assume that when the onTouchEvent is reached, that should somehow notify the BookGridFragment that the BookGrid was touched, but I can't figure out how to do that.

Everything I've found online is about passing information between Fragments, but that approach doesn't work here as the BookGrid class doesn't "know" that it's within a BookGridFragment.

like image 874
Sharon Avatar asked Jun 20 '18 19:06

Sharon


1 Answers

You could use the same idea that is used to communicate a Fragment and an Activity. Create an interface:

public interface OnBookGridTouched{
    void onTouchGrid();
} 

Add a variable to your BookGrid:

private OnBookGridTouched mCallback;

Add a setter to this variable:

public void setCallback(OnBookGridTouched callback){
    mCallback = callback;
}

Then make your fragment implement the interface:

public class BookGridFragment extends Fragment implements OnBookGridTouched  {

You'll be forced to implement the method onTouchGrid

In your fragment onCreateView pass the fragment to your custom view:

myBookGrid.setCallback(this);

Finally, in your custom view you can call the callback to reference the fragment:

 public boolean onTouchEvent(MotionEvent event) {

       // This function responds to the user tapping a piece of
       //  book info within the grid

       // THIS IS WHERE I'M HAVING PROBLEMS
       mCallback.onTouchGrid();
   }
like image 100
Levi Moreira Avatar answered Nov 09 '22 07:11

Levi Moreira