Pulltorefresh encountered problems
First, when XScrollView layout is used, the content layout control cannot be placed in the layout. xml file. If it is placed, an error is reported,
<com.markmao.pulltorefresh.widget.XScrollView android:id="@+id/scroll_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/page_top" android:fillViewport="true" android:scrollbars="none" >
</com.markmao.pulltorefresh.widget.XScrollView>
XScrollView. by looking at the following code, you will find that a child control has been dynamically added to the control during initialization. If you place the content layout again, an error will be returned, because the default setting of android for ScrollView is to allow only unique sub-Spaces
public class XScrollView extends ScrollView implements OnScrollListener {private LinearLayout mLayout; private LinearLayout mContentLayout;public XScrollView(Context context) { super(context); initWithContext(context); }
public XScrollView(Context context, AttributeSet attrs) { super(context, attrs); initWithContext(context); } public XScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initWithContext(context); } private void initWithContext(Context context) { mLayout = (LinearLayout) View.inflate(context, R.layout.vw_xscrollview_layout, null); mContentLayout = (LinearLayout) mLayout.findViewById(R.id.content_layout);this.addView(mLayout); }
How does the source Master of this control allow users to nest User-Defined la s? Let's look at it first.
R. layout. vw_xscrollview_layout: Inside the layout file, we don't need to worry about the header and the top. Let's look at the center. The ID value is content_layout. By default, our custom layout is nested in it.
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/header_layout" android:layout_gravity="center_horizontal|top" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" /> <LinearLayout android:id="@+id/content_layout" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" /> <LinearLayout android:id="@+id/footer_layout" android:layout_gravity="center_horizontal|bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:tag="ttttt" android:orientation="vertical" /></LinearLayout>
public void setContentView(ViewGroup content) { if (mLayout == null) return; if (mContentLayout == null) mContentLayout = (LinearLayout) mLayout.findViewById(R.id.content_layout); if (mContentLayout.getChildCount() > 0) mContentLayout.removeAllViews(); mContentLayout.addView(content); } public void setView(View content) { if (mLayout == null) return; if (mContentLayout == null) mContentLayout = (LinearLayout) mLayout.findViewById(R.id.content_layout); mContentLayout.addView(content); }
External import. There are two functions for setting content: setContentView and setView.
View content = LayoutInflater.from(this).inflate(R.layout.vw_scroll_view_content, null);
scrollview.setContentView()content;
To address this situation, I think it's okay if I think it's okay to add a layout file. My obsessive-compulsive disorder is so bad that I don't like it. It's not concise, so I simply modified it.
The following layout file still uses a layout file that uses XScrollView. The content layout is also stored in this file, but it is not a parent-child relationship with XScrollView, but at the same level. The ID value is xcollview_content, is the content layout. Next let's look at the code's
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/test_parent" android:layout_width="match_parent" android:layout_height="match_parent" > <com.markmao.pulltorefresh.widget.XScrollView android:id="@+id/scroll_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/page_top" android:fillViewport="true" android:scrollbars="none" > </com.markmao.pulltorefresh.widget.XScrollView> <LinearLayout android:id="@+id/xcollview_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/scroll_view" android:background="@color/transparent" android:orientation="vertical" > <ListView android:id="@+id/content_list" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#00000000" android:scrollbars="none" /> </LinearLayout></RelativeLayout>
Code Change: Add a new function in XScrollView. Note that only one parent control is allowed in one space. This step ends the first problem.
public void setDView(View content) { if (mLayout == null) return; if (mContentLayout == null) mContentLayout = (LinearLayout) mLayout.findViewById(R.id.content_layout);
ViewParent parent = this.getParent(); if (parent instanceof RelativeLayout) { RelativeLayout r_parent = (RelativeLayout) parent; r_parent.removeView(content); } if (parent instanceof LinearLayout) { LinearLayout l_parent = (LinearLayout) parent; l_parent.removeView(content); } mContentLayout.addView(content); }