OnClick event doesn't work with nested list item control in android

I have 2 popups. On the first popup, I have a ListView with selectable items. When I click an item from the first list, a second popup appears, which also has a ListView with options for selection.

I implemented the elements of the first list View as custom View and subscribed to clicks inside the view designer, for example:

 class CustomListItem extends RelativeLayout{ public CustomListItem(){ ... //inflating stuff there ((Button)findViewById(R.id.listItemButton)).setOnClickListener( //This code not working as expected, but then could fire a lot of times v -> System.out.println("item clicked"); ); } } 

When I open the first dialog for the first time, the onClick handler fires as expected and a second popup appears. But when I close the second popup and go back (calling Dialog.dismiss() for the popup) to the first popup, then the onClick handler in the first list stops working. There are other things:

  • onTouch listener for the list view item is still working (for action=ACTION_DOWN and action=ACTION_UP );
  • when I set onItemClickListener for listview, it always called (The first time the popup window opened and when we get back to it);
  • When I click many times on an item in a list, sometimes onClick happens, and after that it calls as many times as I clicked before.

Do you have any ideas what might be causing the problem?

UPDATE

There seems to be a problem with the internal getView adapter. I tried to create a custom item control in it (or get it from the cache):

 @Override public View getView(int position, View view, ViewGroup parent) { if (!constructedViewCache.containsKey(position)) { constructedViewCache.put(position, new CustomListItem ()); } return constructedViewCache.get(position); } 

When I changed this code to the code below, everything works:

 @Override public View getView(int position, View view, ViewGroup parent) { if (view == null) { view = LayoutInflater.from(context).inflate(R.layout.list_view_item, null); } view.setOnClickListener(v -> { v -> System.out.println("item clicked"); }); return view; } 

Why is it not possible to create a view using new CustomListItem () ? Or, if possible, how can I do this?

+5
source share
1 answer

Try inserting the android:descendantFocusability="blocksDescendants" into the parent layout declaration of your list item, where listItemButton located.

Where is the android:descendantFocusability attribute android:descendantFocusability

Defines the relationship between the ViewGroup and its descendants when searching for a view to focus on.

And the constant blocksDescendants means that:

ViewGroup blocks its descendants from receiving focus.

Second question

You can create a view using the new CustomListItem (). See the example below:

 @Override public View getView(int i, View view, ViewGroup viewGroup) { return new CustomListItem(context); } 

EDIT

I have not seen your complete code, it seems like you are something wrong. Here is a working example:

 public class CustomAdapter extends BaseAdapter { private Context context; private HashMap<Integer, CustomListItem> constructedViewCache = new HashMap<>(); public CustomAdapter(Context context) { this.context = context; } @Override public int getCount() { return 20; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int position, View view, ViewGroup viewGroup) { if (!constructedViewCache.containsKey(position)) { constructedViewCache.put(position, new CustomListItem(context, position)); } return constructedViewCache.get(position); } } 

And the second class:

 public class CustomListItem extends RelativeLayout { private static final String TAG = "CustomListItem"; public CustomListItem(Context context, int position) { super(context); View view = LayoutInflater.from(context).inflate(R.layout.item_view, this); Button button = (Button) view.findViewById(R.id.button); button.setOnClickListener(view1 -> { Log.e(TAG, "CustomListItem: " + position ); FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager(); new CustomDialog().show(fragmentManager, "tag"); }); } } 

EDIT 2

I updated CustomListItem to display a dialog box when a button is clicked. There is custom DialogFragment code that uses a list of elements like layout and adapter like activity:

 public class CustomDialog extends DialogFragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_main, container, false); ListView listView = (ListView) view.findViewById(R.id.list); listView.setAdapter(new CustomAdapter(getActivity())); return view; } } 
0
source

All Articles