public class MyAdapter extends BaseAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
View view = mLayoutInflater.inflate(R.layout.my_listview_item, null);
TextView myTv = (TextView) view.findViewById(R.id.myTv);
myTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// I want to replace another fragment
}
});
return view;
}
I can't find a solution in stackoverflow...
Can I replace a fragment?? Help me..
There are two ways to call fragment from adapter class :
1) You can make a interface for it.
2) You can register your own BroadcastReceiver
with particular action.
Direct call fragment from button click is not a good and right , clean way. So i am just going to post answer for my second approach.
On button click in your adapter class
context.sendBroadcast(new Intent("call.myfragment.action"))
This is the simple things in android is by sending broadcast messages.
urButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
context.sendBroadcast(new Intent("call.myfragment.action"))
}
});
Now in your activity all you need to register a BroadcastReceiver
with the "call.myfragment.action"
and that's it inside of it just call your fragment:
context.registerReceiver(mBroadcastReceiver, new IntentFilter("call.myfragment.action")) // This code is in your Activity will be in onCreate() or in onResume() method.
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Now just call your Fragment here now.
}
};