I have a question from last few days. I basically have three fragments in a TabActivity, Fragment A, Fragment B and Fragment C. All three Fragments contain a list of CardView of approximately 14 CardViews in single Fragment. This three Fragment share same layouts with same cardview with same id. There is a button in each CardView and i want to start different activity's when clicked on that button. But I cant do it because id for button is same and I want to start activity's when that button is clicked from different Fragments. I am using RecyclerView for this. It means I have 14 buttons in a Fragment and there are three Fragments.
Asked
Active
Viewed 964 times
0
-
Okay, but where is the button click listener ? – intellignt_idiot Jul 03 '16 at 12:46
-
If your project is small, you can send me your project zip and I will add the required code in your project – intellignt_idiot Jul 03 '16 at 12:46
-
Where i can send you my project? – confidante Jul 03 '16 at 16:44
-
send me a dropbox link here after uploading to dropbox – intellignt_idiot Jul 03 '16 at 16:47
-
These three files are my whole project. There is nothing more to share. – confidante Jul 03 '16 at 16:54
-
Okay. I will be back in 5 minutes. Thanks for you help in advance. – confidante Jul 03 '16 at 16:55
-
I have updated my answer, let me know if it helps. – intellignt_idiot Jul 03 '16 at 17:02
-
https://www.dropbox.com/sh/gleox5zyhiki16h/AAA4Ay1SmX29Alo1aGDiT8wVa?dl=0 – confidante Jul 03 '16 at 17:06
-
I have added a link of my all java classes and layout files in it. – confidante Jul 03 '16 at 17:07
-
Did you check my updated answer ? I am afraid, if you can't send me the whole project zip, I cant help you. I can't create a project from your files. – intellignt_idiot Jul 03 '16 at 17:12
-
No, its not project zip. I need the project which I can directly open and do the changes and send you back, not these separate files. – intellignt_idiot Jul 03 '16 at 17:16
-
Wait I will send you zip now. My net is slow. size is 52mb. – confidante Jul 03 '16 at 17:18
-
https://www.dropbox.com/s/xk7mevq178cel97/Notes.zip?dl=0 – confidante Jul 03 '16 at 17:37
-
I have added zip. Its 25 MB. – confidante Jul 03 '16 at 17:38
-
Okay, I will check it now – intellignt_idiot Jul 03 '16 at 17:38
-
What do you want to do on clicks of each fragments ? – intellignt_idiot Jul 03 '16 at 17:49
-
There are three Framents. Each fragment have 6, 6 and 3 Cardview respectively with exactly same layout. I have 15 other activities which i want to open when user click on that button on each cardview. – confidante Jul 03 '16 at 18:05
-
Here is the updated project :https://www.dropbox.com/s/qw4i6f14d763abv/Notes.zip?dl=0 Please mark my answer accepted if it helps you. I have updated my answer. – intellignt_idiot Jul 03 '16 at 18:12
-
@intellignt_idiot Thanks a lot. Your every code was running and working. You have helped me a lot and solved my problem in just one day. But I also have made some changes in my file. I will tell you in my updated question above. – confidante Jul 03 '16 at 18:59
-
I am glad to hear that it worked. If you have any other questions, please post a separate question. Good luck for the rest of your project – intellignt_idiot Jul 04 '16 at 05:04
2 Answers
1
Implement on click listener in your activity. On each button click programatically do whatever changes you want to do in the layout..
Example below
public void onClick(View v)
{
switch(v.getId())
{
case R.id.button1:
// Do something
break;
case R.id.button2:
// Do something
break;
case R.id.button3:
// Do something
}
}
0
If the button is in recyclerView, you can set a click listener of that button in XML, which will be called in Your Activity. Now your recyclerView adapter , set a tag which will be the position of the cell to you your button.
holder.imageButton.setTag(position);
Now, in your main activity, create an integer variable named
int currentSelectedTab = 0;
and then do this on your TabChange listener.;
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
selectedTab = position;
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
Now you will always have the current selected tabPosition.
And in clicklistener in your Activity do this ::
public void myOnClick(View v) {
if(current selectedTab == 1)
{
if(v.getId === R.id.btn1)
{
// it means btn of Fragment 1 is clicked
// same goes for other button clicks
// and for the index of button
// v.getTag() it will you which cell's button was clicked
}
In your cardView xml :
<ImageButton
android:id="@+id/chapter_description"
android:layout_width="36dp"
android:onClick="myOnClick"
android:layout_height="36dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="@drawable/description"
android:background="@drawable/ripple_effect"/>

intellignt_idiot
- 1,962
- 2
- 16
- 23
-
hey can you now help me out. I have posted my code now. Please. It will be a great help. – confidante Jul 03 '16 at 12:33
-
The button that you are talking about is the one whose id is chapter_description ? – intellignt_idiot Jul 03 '16 at 14:02
-
Yeah, It is a button i am using three different fragments. I want them to do different things. But the layout I am using for three fragments is same. – confidante Jul 03 '16 at 16:41