You have to use Rx Event Bus for this. Event Bus utils make it easy to recognize which event is being called. For example in your case inside adapter's item holder you can get the position of item in the list view which is clicked.
heightListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
RXEventBusUtils.getInstance().postEvent(new StartActivityEvent(i));
}
});
RxEventBusUtils class
public class RXEventBusUtils {
private static RXEventBusUtils sRXEventBusUtils;
private PublishSubject<Object> mPublishSubject;
public static RXEventBusUtils getInstance() {
if (sRXEventBusUtils == null) {
sRXEventBusUtils = new RXEventBusUtils();
}
return sRXEventBusUtils;
}
public PublishSubject getSubject() {
if (null == mPublishSubject) {
mPublishSubject = PublishSubject.create();
}
return mPublishSubject;
}
public void postEvent(Object event) {
getSubject().onNext(event);
}
}
StartActivityEvent class
public class StartActivityEvent {
int position;
public StartActivityEvent () {
}
public int getPosition() {
return position;
}
}
and inside your activity you have to subscribe the Rx Event Bus Utils inside onCreate method.
disposable = RXEventBusUtils.getInstance().getSubject().subscribeOn(AndroidSchedulers.mainThread())
.subscribe(this::onReceiveEvent);
this line subscribe(register) the event bus and creates a method onReceiveEvent and passes event as object. Inside this method you can check which event is received here and so you can perform your desire function.
private void onReceiveEvent(Object event) {
if (event instanceof StartActivityEvent) {
// start activity
}
if (event instanceof DeleteItemEvent) {
/// delete item etc
}
}
And do not forget to add dependency in app level gradle file
implementation 'io.reactivex.rxjava2:rxjava:2.1.5'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
remember one thing you have to create separate event class for different kind of function.
so for child listview you have to simply register new event to perform click listener
childListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
RXEventBusUtils.getInstance().postEvent(new DeleteItemEvent(i));
}
});
DeleteItemEvent Class
public class DeleteItemEvent {
int position;
public DeleteItemEvent () {
}
public int getPosition() {
return position;
}
}