0

I want to add onClick() event for listview. Because i have listview inside listview. and i want when i click on parent listview item then open new activity, but when i click on child listview item , the code of parent listview item click is not working, How can I give onClick() event for child ListView.

 heightListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                startActivity(new Intent(LocationMapHistory.this,LocationHistoryActivity.class));
            }
        });

this code only working if i hide child listview into parent listview item.

3 Answers3

0

As per ChrLipp's answer, it is not recommended to create a Listview within a ListView. My workaround on this is to create a ViewGroup instead ie. LinearLayout and manually manage the Viewgroup children.

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    MyHolder myholder = (MyHolder)holder;
    View child = LayoutInflater.from(context).inflate(R.layout.child, myholder.viewgroup, false);
    child.setOnClickListener(yourListener);
    holder.viewgroup.addView(child);
}
Rick Royd Aban
  • 904
  • 6
  • 33
  • app crash : java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView because its not recyclerview , its listview –  Sep 06 '18 at 07:12
0

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;
 }
 }
Shahkar Raza
  • 355
  • 3
  • 11
0

using interface , i found it

create a interface then implement in parent adapter and pass with child adapter , while user will click child item then call the interface , its will notify parent item , and perform your code .