Android is scattered and has a few minutes of work

Source: Internet
Author: User

Android is scattered and has a few minutes of work
2016-03-13Android: Nested GridView in scattered ListView

To implementGroup listEffect: Click the group name in ListView to expand the group and display its projects.
Public class UnfoldGridView extends GridView {
Public UnfoldGridView (Context context ){
Super (context );
}

Public UnfoldGridView (Context context, AttributeSet attrs ){
Super (context, attrs );
}

Public UnfoldGridView (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
}

@ Override
Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec ){
Int expandSpec = View. MeasureSpec. makeMeasureSpec (900000, View. MeasureSpec. AT_MOST );
Super. onMeasure (widthMeasureSpec, expandSpec );
}
}

In makeMeasureSpec, assign a large size and use AtMost to keep it large enough.

SetAdapter, addHeaderView, and addFooterView

You can use addHeaderView and addFooterView to add a personalized view at the beginning and end of the ListView. Both of them can be called multiple times to add multiple headers and footers.

When first introduced, this method cocould only be called before setting the adapter with setAdapter (ListAdapter ). starting with KITKAT, this method may be called at any time. if the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.

Therefore, to maintain compatibility, make sure that the addHeaderView and addFooterView are executed before or after API 19. If addHeaderView is executed after setAdapter, an error is returned for versions earlier than API 19. If addFooterView is executed after setAdapter, it will not cause a runtime error -- but it is even more confusing that the added view cannot be seen.
GetItemViewType of ListView

When you want to display multiple different lists consecutively or display multiple views in an interval, you need to use two ListView methods:

@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}

The getItemViewType method must note that the value range of an integer constant such as ITEM_XX must be0~getViewTypeCount()-1The following describes the returned values of getItemViewType:

An integer representing the type of View. Two views should share the same type if one can be converted to the other in getView. Note: Integers must be in the range 0 to getViewTypeCount - 1. IGNORE_ITEM_VIEW_TYPE can also be returned.

The value of viewType outside the interval may cause the indexoutofboundexception error during running. This is the limitation of ListView itself.

StartActivityForResult and launchMode OF THE ACTIVITY

After Activity_A starts Activity_ B, Activity_ B needs to return some data after the operation is completed:

// Activity_A
Private final int REQUEST_CODE_EDIT_ITEM = 2;
Public void startPageBForEdit (){
Intent start = new Intent (this, EditActivity. class );
StartActivityForResult (start, REQUEST_CODE_EDIT_ITEM );
}
// Activity_ B
Public void setResult (){
Intent data = new Intent ();
Data. putExtra ("itemDelete", true );
SetResult (RESULT_ OK, data );
}

Receive data in Activity_A:

Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
If (requestCode = REQUEST_CODE_EDIT_ITEM & resultCode = RESULT_ OK ){
// The result is returned successfully.
}
}

The preceding is a typical code snippet. Note that after startActivityForResult is used, the onActivityResult of the current Activity is executed immediately. The resultCode is RESULT_CANCEL and the data is null.
Service onStartCommand Return Value

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}

The flags and returned values in onStartCommand are closely related to Service startup. The Service is located like an Activity without a View. If the application is in the background for a long time, the Service may be temporarily killed (with the process being killed) and then started again by the system.
Each time startService is called to execute some actions, onStartCommand is executed. Its intent parameter indicates the intent description data distributed over the process (intnt is an object carrying information about "what to do ).
StartService is used to start a running, starting Service. When a Service is restarted, the system may call onStartCommand in different Service states.
// To be continued...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.