I have two views In a single activity StackView and Gridview and 2 corresponding adapters .when I clicked on StackView item it should flip the grid item in Gridview and vice versa?
Asked
Active
Viewed 1,455 times
-1
-
Are you using fragment to do so? – Keyur Thumar Apr 09 '17 at 03:01
-
No Activity !! It doesn't matter right !! – Trinadh Koya Apr 09 '17 at 04:42
-
No, But I want to answer accordingly. – Keyur Thumar Apr 09 '17 at 05:08
-
Yeah I am Using Activity and it should be Done in Activity Only!! need more clarity on this Question ? – Trinadh Koya Apr 09 '17 at 05:11
-
Your adaptor class is with in activity or its separate class? – Keyur Thumar Apr 09 '17 at 05:12
-
No It's Outside !! – Trinadh Koya Apr 09 '17 at 05:13
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141263/discussion-between-trinadh-koya-and-keyur-thumar). – Trinadh Koya Apr 09 '17 at 05:15
-
please do share what you have done. this does not seems to be a eligible question. Do try to solve of your own first before asking for help. – Sagar Nayak Apr 09 '17 at 06:50
2 Answers
-1
// activity classs
public class SampleActivity extends Activity implements
OnGridChangeListener {
public void onCreate(bundle){
// replace this with your adapter class.
Adapter adapter = new adapter(this);
}
@override
public void OnGridChange(){
//here you go.
// write code to do what you want.
}
// interface to communicate with activity
public interface OnGridChangeListener {
public void OnGridChange()
}
// adaptor class
public class Adaptor extends "you apapter class to extend"{
OnGridChangeListener onGridChangeListener ;
public Adapter(OnGridChangeListener listener){
onGridChangeListener =listener
}
public getView(){
public void onclick(){
onGridChangeListener.OnGridChange("pass you data");
}
}
}

Keyur Thumar
- 608
- 7
- 19
-
Here is sample idea to do what you want. you can define same for other adaptor too. – Keyur Thumar Apr 09 '17 at 05:24
-
have you worked with StackView?,When ever we Swipe a StackItem ,it should effect the Grid Item . – Trinadh Koya Apr 09 '17 at 05:29
-
Please try to answer with code that looks like it should compile – OneCricketeer Apr 09 '17 at 05:30
-
1+Keyur Thumar,I got your intention what you are trying to convey.But my request is quite varies – Trinadh Koya Apr 09 '17 at 05:32
-
@cricket_007 its sample idea. If you are genius,you could have answer it with what you said. :) – Keyur Thumar Apr 09 '17 at 05:35
-
-
@TRINADH KOYA What is your request. as per your question this is best and standard way to do it. What you want apart from it. – Keyur Thumar Apr 09 '17 at 05:39
-
In the Activity i divided the Layout in to 2 one for StackView and 1 for GridView.I make use of two adapters to hold the data.When i clicked the 1 position on StackView,it has to flip the Grid Item .and not vice versa please.Hope you understand it – Trinadh Koya Apr 09 '17 at 05:41
-
Your idea here is fine. The code, however, is very hard to read / copy into a format that is useful. That is my point. I'm not trying to be "a genius" – OneCricketeer Apr 09 '17 at 05:45
-
hey @cricket_007 ,can you please mention the user name to whom you are talking – Trinadh Koya Apr 09 '17 at 05:48
-2
As per your question this is what i get -
You have a Activity with two independent views which has their own adapters. So when there is changes in one of the adapter you want it to be reflected into another adapter.
The simple solution for your query would be -
- When there is a change in first adapter you reflect the change to the activity. after that call the function in the second adapter to reflect the change you want in second adapter.
- For this you have to define an interface in first adapter and implement this is activity.
- When the first adapter changes call the interface method and this will reflect in activity.
- Then call method in second adapter to do the changes you want.
Code example -
MainActivity.Java
public class MainActivity extends AppCompatActivity implements FirstAdapter.callBackMethods {
FirstAdapter firstAdapter;
SecondAdapter secondAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstAdapter = new FirstAdapter(MainAtivity.this.getApplicationContext());
//do all the declaration and operation of first adapter. Pass context along with your required params.
secondAdapter = new SecondAdapter();
//do all the declaration and operation of second adapter.
}
//callback method of first adapter
@override
public void callback(){
//changes have been done in FirstAdapter and this methos is fired.
//now do do the changes in SecondAdapter as per req.
if(secondAdapter != null){
secondAdapter.reflectChanges();
}
}
}
FirstAdapter.class
I am taking example of recylerview adapter.
public class FirstAdapter extends RecyclerView.Adapter<FirstAdapter.ViewHolder>{
public FirstAdapter(Context context){
this.context=context;
}
/*
all the boilerplate codes and business logic
*/
//when you want to reflect the changes
callBackMethods callBackMethods = (callBackMethods) context;
callBackMethods.callback();
//this will fireup the implementation in the MainActivity.
public interface callBackMethods{
public void callback();
}
}
SecondAdapter.class
This is where the changes will be reflected.
public class SecondAdapter extends RecyclerView.Adapter<SecondAdapter.ViewHolder>{
/*
all the boilerplate codes and business logic
*/
public void reflectChanges(){
/*
This method will be called from the MainActivity. pass whatever params you want to pass from Activity and do the changes you want to do in the SecondAdapter.
*/
}
}
I hope this solves your problem. Happy coding...

Sagar Nayak
- 2,138
- 2
- 19
- 52
-
-
Sure !! I will try !! I can't give you more than that .What i can ask is how to get the second adopter position in first adopter Call back without applying callback !! – Trinadh Koya Apr 09 '17 at 07:01
-
your question is too confusing buddy. just read the oops basics .. you will get the answer on communication between classes. – Sagar Nayak Apr 09 '17 at 08:18
-
if you don't understand my question,please Ask as many no of times.Don't try to underestimate others it does not make sense.May be you might have a great knowledge on what you are talking,so try to express .I asked you to give me a clue how to do the stuff .You have written your own code apart from my Question. – Trinadh Koya Apr 09 '17 at 08:24
-
In short,if you click on first position in First Adapter,it should effect(do some action) on the same position on second Adapter.So i have taken as Stackview and GridView. – Trinadh Koya Apr 09 '17 at 08:26
-
yes , for this you have to make communication between two adapters. and as the adapters can not make any direct communication you take help of mainactivity. and my answer says how to get that. you pass position from adapter to interface, then this position comes to activity. then pass that position to method in second activity, then make the changes in the position you got. – Sagar Nayak Apr 09 '17 at 08:58
-
-