First of all, let's talk about the main problems that have arisen.
1.listview has invalid click controls and no response to onItemClick events. In the process of customizing item views, gridview, checkbox, imageButton and other controls are added. Then this problem may arise that onItemClickListener cannot be triggered.
My solution is to add the label android: descendant Focusability = "blocks Descendants" to the outermost layouts of my defined item layout and click on it. What does that mean? It's like covering all the controls that grab the focus of the viewgroup in the layout first, which means that the custom layout won't lose the click time because of the influence of the child controls, I think so.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/white" android:descendantFocusability="blocksDescendants" android:orientation="vertical" > ...........
2. When nesting gridview, GridView displays only one row of problems. I find this problem in use, and then try to specify the height of gridview, and find that it only shows one line at all. I think this is because the nested outer listview has scroll function, so that listview does not care about internal controls, such as the height of gridview, and does not increase the height of gridview, so the solution is to increase the height of gridview. Simply, we have to rewrite gridview's onMeasue() method.
I did this by inheriting Gridvewi and rewriting onMeasure. The following code:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); }
3. After nesting the GridView, it was found that there was no trigger click in the empty area of the graph, neither the onItemClick of the girdview nor the onItemClick of the Listview. Later, it was found that the trigger event of the invalid area of the GridView could be monitored by itself.
The following is the gridview code I implemented:
package com.example.testlistview; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.GridView; public class MyGridView extends GridView { private OnTouchInvalidPositionListener onTouchInvalidPositionListener; public MyGridView(Context context) { super(context); } public MyGridView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } @Override public boolean onTouchEvent(MotionEvent ev) { //Create a listening interface first, and once you click on the invalid area, implement the onTouchInvalidPosition method, and return true or false to confirm that you consumed the event. if(onTouchInvalidPositionListener!=null){ if(!isEnabled()){ return isClickable()||isLongClickable(); } int motionPosition = pointToPosition((int)ev.getX(), (int)ev.getY()); if(ev.getAction()==MotionEvent.ACTION_UP&&motionPosition == INVALID_POSITION){ super.onTouchEvent(ev); return onTouchInvalidPositionListener.onTouchInvalidPosition(motionPosition); } } return super.onTouchEvent(ev); } public void setOnTouchInvalidPositionListener( OnTouchInvalidPositionListener onTouchInvalidPositionListener) { this.onTouchInvalidPositionListener = onTouchInvalidPositionListener; } public interface OnTouchInvalidPositionListener{ public boolean onTouchInvalidPosition(int motionEvent); } }