Brian Brian - 6 months ago 404
Android Question

How can I implement OnClickListener of an ImageView in a fragment?


Can anyone kindly help me implement the opening of a new activity upon clicking of an ImageView. I have a code snippet displayed below.


public class TabFan extends Fragment {

//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
return inflater.inflate(R.layout.tab_fan, container, false);

// Onclick Listening
ImageView image = (ImageView) findViewById(R.id.image);
image.setOnClickListener(this);


}
public void onClick(View v) {

// Launching new Activity on hitting the image
Intent j = new Intent(getApplicationContext(), Activity2.class);
startActivity(j);
// End intent
}


}


Ok I have a code with three tabs, the following controls my tabs which is working right.


public class Fans extends AppCompatActivity implements TabLayout.OnTabSelectedListener{

//This is our tablayout
private TabLayout tabLayout;

//This is our viewPager
private ViewPager viewPager;
ImageView image;

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

//Adding toolbar to the activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

//Initializing the tablayout
tabLayout = (TabLayout) findViewById(R.id.tabLayout);

//Adding the tabs using addTab() method
tabLayout.addTab(tabLayout.newTab().setText("Fans"));
tabLayout.addTab(tabLayout.newTab().setText("Jersey"));
tabLayout.addTab(tabLayout.newTab().setText("Team"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.pager);

//Creating our pager adapter
Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());

//Adding adapter to pager
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});



}

@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}


}


I have another class pager


public class Pager extends FragmentStatePagerAdapter {

//integer to count number of tabs
int tabCount;

//Constructor to the class
public Pager(FragmentManager fm, int tabCount) {
super(fm);
//Initializing tab count
this.tabCount= tabCount;
}

//Overriding method getItem
@Override
public Fragment getItem(int position) {
//Returning the current tabs
switch (position) {
case 0:
TabFan tab1 = new TabFan();
return tab1;
case 1:
TabJersey tab2 = new TabJersey();
return tab2;
case 2:
TabTeam tab3 = new TabTeam();
return tab3;
default:
return null;
}
}

//Overriden method getCount to get the number of tabs
@Override
public int getCount() {
return tabCount;
}


}


Finally the interested raw class TabFan, now where exactly should that listener be implemented. I have tried the class Fans but apparently am getting some crush, TabFan seem not to work with the events too. Any help please.


public class TabFan extends Fragment {

//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
return inflater.inflate(R.layout.tab_fan, container, false);

// Onclick Listener
}


}

Answer

With the code that you have provided, you will need to do two things to properly link the image object to the onClick() method that you have written.

First, the Fragment class needs to implement the View.OnClickListener interface. This is what makes the onClick(View v)actually activate on a click when using setOnClickListener(this). Replace your class declaration line with:

public class TabFan extends Fragment implements View.OnClickListener {

Second, if you are going to add any more clickable objects to TabFan with setOnClickListener(this), then onClick(View v) needs to verify that it is dealing with the expected View:

@Override
public void onClick(View v) {
    if (v.getId() == R.id.image) {
        // Launching new Activity on hitting the image
        Intent j = new Intent(getActivity().getApplicationContext(), Activity2.class);
        startActivity(j);
        // End intent
    }
}
Comments