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; } }