Home > Net >  How can i perform onclickListener on two object using interface in android studio?
How can i perform onclickListener on two object using interface in android studio?

Time:12-18

I have a Fragment class which extends View.onClickListener and i have implement @overriden method i.e onClick. I have 2 imageView which performs onClickListerner.

  1. filterIcon.setOnClickListener(this)
  2. fab.setOnClickListener(this)

and set both activities in @overriden method i.e. onClick().

i want to know can this is possible that onClickListener will call the related clickEvent if yes, how ? if not why ?

Here's my code :

public class ExpenseFragment extends Fragment implements View.OnClickListener {
    private TextView totalExpenseTV;
    private FloatingActionButton fab;
    private RecyclerView recyclerView;
    private ImageView filterIcon;
    private PieChart pieChart;

    private Animation rotateForward, rotateBackward;

    private FirebaseRecyclerOptions<ExpenseModel> options;
    private MyAdapter adapter;

    private int calendarImgClick = 0;
    private int groceryClick = 0;
    private int bikeClick = 0;
    private int rentClick = 0;
    private int foodsClick = 0;
    private int beerClick = 0;
    private int setCalClick = 0;

    private String whichCategorySelected = "";
    private String selectedDate;

    public void onViewCreated(@NonNull View attachInParentView, Bundle savedInstanceState) {
        super.onViewCreated(attachInParentView, savedInstanceState);
        findViewByIDS(attachInParentView);
        setupRecyclerView();
        fabAnimation();
        fab.setOnClickListener(this);
        filterIcon.setOnClickListener(this);
        databaseListener();


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_expense, container, false);
    }

    @Override
    public void onClick(View view) {
        fab.startAnimation(rotateForward);
        rotateForward.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                addNote();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        filterIcon.setBackgroundResource(R.drawable.filter_off);
    }

CodePudding user response:

@Override
public void onClick(View view) {
    if (view.getId() == R.id.filterIcon) {
        // codul care se executează când este apăsată imaginea filterIcon
    } else if (view.getId() == R.id.fab) {
        // codul care se executează când este apăsată imaginea fab
    }
}
  • Related