Android Fragment instance Listfragment listiew pull-down refresh pull-up load horizontal sliding event listening

Source: Internet
Author: User

Android Fragment instance Listfragment listiew pull-down refresh pull-up load horizontal sliding event listening
Android Fragment development instance and other color functions

The code is too long, please download the attachment source code: http://yunpan.cn/cs24a2Z7C5kRk (extraction code: a0cf) Have Doubts welcome to contact QQ972910164


I,CustomListView description:You can pull down and refresh, pull up and load, and call back the horizontal sliding interface,
/**
* Functions are similar to ListView, and the following are the features of Tianji:
* 1. pull-down refresh and load up to implement the CustomListView. OnPullListener Interface
* 3. Slide switching to implement the CustomListView. OnSlideListener Interface
* @ Author zhanghao 20141029
*/
(Prompt text to be customized)
Refreshing
Loading
Release to refresh
Pull down to refresh
Latest update time: % 1 $ s


Ii. CustomListFragment Function Description: pull-down and refresh, pull-up and load, horizontal sliding interface callback,


It has all ListFragment functions, the drop-down refresh and pull-up loading functions of the Tianji, and the Slide sliding switching callback interface;


Usage:
You can use your own Fragment to inherit CustomListFragment and rewrite the onCreateView function:
1. Return custom_list_fragment_content.xml Layout directly,
For example
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ){
If (container = null ){
// We have different layouts, and in one of them this
// Fragment's containing frame doesn't exist. The fragment
// May still be created from its saved state, but there is
// No reason to try to create its view hierarchy because it
// Won't be displayed. Note this is not needed -- we cocould
// Just run the code below, where we wocould create and return
// The view hierarchy; it wocould just never be used.
Return null;
}
View root = inflater. inflate (R. layout. custom_list_fragment_content, null );
Return root;
}


2. You need to add the custom layout of the control myfragment. xml:
(Including custom_list_fragment_content.xml)
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
>
Android: id = "@ + id/last_synctime_message"
Android: layout_width = "match_parent"
Android: layout_height = "@ dimen/error_message_height"
Android: paddingLeft = "8dip"
Android: paddingRight = "8dip"
Android: gravity = "right | center_vertical"
Android: text = "@ string/no_last_synctime"
Android: singleLine = "true"
Android: ellipsize = "end"
Android: background = "@ color/synctime_bar_background"
Android: visibility = "gone"
/>
Android: layout_width = "match_parent"
Android: layout_height = "1px"
Android: background = "? Android: attr/listDivider "/>

Layout = "@ layout/custom_list_fragment_content"
Android: id = "@ + id/list_panel"
Android: layout_width = "match_parent"
Android: layout_height = "0dip"
Android: layout_weight = "1"
/>






Then,
Public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ){
View root = inflater. inflate (R. layout. myfragment, null );
Return root;
}


If you are interested, you can check the source code:
Package com. custom. listfragment. demo;


Import android. annotation. SuppressLint;
Import android. app. Fragment;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. view. View;
Import android. view. animation. AnimationUtils;
Import android. widget. AdapterView;
Import android. widget. ListAdapter;
Import android. widget. ListView;
Import android. widget. TextView;
/**
* Functions are similar to ListFragment, and the following features are provided by Tianji:
* 1. You can pull down and refresh to implement the CustomListView. OnPullListener interface.
* 2. Pull up and load (disabled by default)
* 3. Slide switching to implement the CustomListView. OnSlideListener Interface
* @ Author zhanghao 20141029
*/
@ SuppressLint ("NewApi") public class CustomListFragment extends Fragment {
Final private Handler mHandler = new Handler ();


Final private Runnable mRequestFocus = new Runnable (){
Public void run (){
MList. focusableViewAvailable (mList );
}
};


Final private AdapterView. OnItemClickListener mOnClickListener
= New AdapterView. OnItemClickListener (){
Public void onItemClick (AdapterView Parent, View v, int position, long id ){
OnListItemClick (ListView) parent, v, position, id );
}
};


ListAdapter mAdapter;
CustomListView mList;
View mEmptyView;
TextView mStandardEmptyView;
View mProgressContainer;
View mListContainer;
CharSequence mEmptyText;
Boolean mListShown;


Public CustomListFragment (){
}


/**
* Provide default implementation to return a simple list view. Subclasses
* Can override to replace with their own layout. If doing so,
* Returned view hierarchy MustHave a ListView whose id
* Is {@ link android. R. id # list android. R. id. list} and can optionally
* Have a sibling view id {@ link android. R. id # empty android. R. id. empty}
* That is to be shown when the list is empty.
*
*

If you are overriding this method with your own custom content,
* Consider including the standard layout {@ link android. R. layout # list_content}
* In your layout file, so that you continue to retain all of the standard
* Behavior of ListFragment. In particle, this is currently the only
* Way to have the built-in indeterminant progress state be shown.
*/


/**
* Attach to list view once the view hierarchy has been created.
*/
@ Override
Public void onViewCreated (View view, Bundle savedInstanceState ){
Super. onViewCreated (view, savedInstanceState );
EnsureList ();
}


/**
* Detach from list view.
*/
Public void onDestroyView (){
MHandler. removeCallbacks (mRequestFocus );
MList = null;
MListShown = false;
MEmptyView = mProgressContainer = mListContainer = null;
MStandardEmptyView = null;
Super. onDestroyView ();
}


/**
* This method will be called when an item in the list is selected.
* Subclasses shocould override. Subclasses can call
* GetListView (). getItemAtPosition (position) if they need to access
* Data associated with the selected item.
*
* @ Param l The ListView where the click happened
* @ Param v The view that was clicked within the ListView
* @ Param position The position of the view in the list
* @ Param id The row id of the item that was clicked
*/
Public void onListItemClick (ListView l, View v, int position, long id ){
}


/**
* Provide the cursor for the list view.
*/
Public void setListAdapter (ListAdapter adapter ){
Boolean hadAdapter = mAdapter! = Null;
MAdapter = adapter;
If (mList! = Null ){
MList. setAdapter (adapter );
If (! MListShown &&! HadAdapter ){
// The list was hidden, and previusly didn't have
// Adapter. It is now time to show it.
SetListShown (true, getView (). getWindowToken ()! = Null );
}
}
}


/**
* Set the currently selected list item to the specified
* Position with the adapter's data
*
* @ Param position
*/
Public void setSelection (int position ){
EnsureList ();
MList. setSelection (position );
}


/**
* Get the position of the currently selected list item.
*/
Public int getSelectedItemPosition (){
EnsureList ();
Return mList. getSelectedItemPosition ();
}


/**
* Get the cursor row ID of the currently selected list item.
*/
Public long getSelectedItemId (){
EnsureList ();
Return mList. getSelectedItemId ();
}


/**
* Get the activity's custom list view widget.
*/
Public CustomListView getListView (){
EnsureList ();
Return mList;
}


/**
* The default content for a ListFragment has a TextView that can
* Be shown when the list is empty. If you wowould like to have it
* Shown, call this method to supply the text it shoshould use.
*/
Public void setEmptyText (CharSequence text ){
EnsureList ();
If (mStandardEmptyView = null ){
Throw new IllegalStateException ("Can't be used with a custom content view ");
}
MStandardEmptyView. setText (text );
If (mEmptyText = null ){
MList. setEmptyView (mStandardEmptyView );
}
MEmptyText = text;
}


/**
* Control whether the list is being displayed. You can make it not
* Displayed if you are waiting for the initial data to show in it.
* This time an indeterminant progress indicator will be shown instead.
*
*

Applications do not normally need to use this themselves. The default
* Behavior of ListFragment is to start with the list not being shown, only
* Showing it once an adapter is given with {@ link # setListAdapter (ListAdapter )}.
* If the list at that point had not been shown, when it does get shown
* It will be do without the user ever seeing the hidden state.
*
* @ Param shown If true, the list view is shown; if false, the progress
* Indicator. The initial value is true.
*/
Public void setListShown (boolean shown ){
SetListShown (shown, true );
}


/**
* Like {@ link # setListShown (boolean)}, but no animation is used when
* Transitioning from the previous state.
*/
Public void setListShownNoAnimation (boolean shown ){
SetListShown (shown, false );
}


/**
* Control whether the list is being displayed. You can make it not
* Displayed if you are waiting for the initial data to show in it.
* This time an indeterminant progress indicator will be shown instead.
*
* @ Param shown If true, the list view is shown; if false, the progress
* Indicator. The initial value is true.
* @ Param animate If true, an animation will be used to transition to
* New state.
*/
Private void setListShown (boolean shown, boolean animate ){
EnsureList ();
If (mProgressContainer = null ){
Throw new IllegalStateException ("Can't be used with a custom content view ");
}
If (mListShown = shown ){
Return;
}
MListShown = shown;
If (shown ){
If (animate ){
MProgressContainer. startAnimation (AnimationUtils. loadAnimation (
GetActivity (), android. R. anim. fade_out ));
MListContainer. startAnimation (AnimationUtils. loadAnimation (
GetActivity (), android. R. anim. fade_in ));
} Else {
MProgressContainer. clearAnimation ();
MListContainer. clearAnimation ();
}
MProgressContainer. setVisibility (View. GONE );
MListContainer. setVisibility (View. VISIBLE );
} Else {
If (animate ){
MProgressContainer. startAnimation (AnimationUtils. loadAnimation (
GetActivity (), android. R. anim. fade_in ));
MListContainer. startAnimation (AnimationUtils. loadAnimation (
GetActivity (), android. R. anim. fade_out ));
} Else {
MProgressContainer. clearAnimation ();
MListContainer. clearAnimation ();
}
MProgressContainer. setVisibility (View. VISIBLE );
MListContainer. setVisibility (View. GONE );
}
}


/**
* Get the ListAdapter associated with this activity's ListView.
*/
Public ListAdapter getListAdapter (){
Return mAdapter;
}


Private void ensureList (){
If (mList! = Null ){
Return;
}
View root = getView ();
If (root = null ){
Throw new IllegalStateException ("Content view not yet created ");
}
If (root instanceof CustomListView ){
MList = (CustomListView) root;
} Else {
MStandardEmptyView = (TextView) root. findViewById (android. R. id. empty );
If (mStandardEmptyView = null ){
MEmptyView = root. findViewById (android. R. id. empty );
} Else {
MStandardEmptyView. setVisibility (View. GONE );
}
MProgressContainer = root. findViewById (R. id. progressContainer );
MListContainer = root. findViewById (R. id. listContainer );
View rawListView = root. findViewById (android. R. id. list );
If (! (RawListView instanceof ListView )){
If (rawListView = null ){
Throw new RuntimeException (
"Your content must have a ListView whose id attribute is" +
"'Android. R. id. list '");
}
Throw new RuntimeException (
"Content has view with id attribute 'android. R. id. list '"
+ "That is not a ListView class ");
}
MList = (CustomListView) rawListView;
If (mEmptyView! = Null ){
MList. setEmptyView (mEmptyView );
} Else if (mEmptyText! = Null ){
MStandardEmptyView. setText (mEmptyText );
MList. setEmptyView (mStandardEmptyView );
}
}
MListShown = true;
MList. setOnItemClickListener (mOnClickListener );
If (mAdapter! = Null ){
ListAdapter adapter = mAdapter;
MAdapter = null;
SetListAdapter (adapter );
} Else {
// We are starting without an adapter, so assume we won't
// Have our data right away and start with the progress indicator.
If (mProgressContainer! = Null ){
SetListShown (false, false );
}
}
MHandler. post (mRequestFocus );
}


Public void setListViewOnPullListener (CustomListView. OnPullListener onPullListener ){
MList. setOnPullListener (onPullListener );
}


Public void setListViewOnSlideListener (CustomListView. OnSlideListener onSlideListener ){
MList. setOnSlideListener (onSlideListener );
}


Public void onRefrshComplete (){
MList. onRefreshComplete ();
}


Public void onLoadComplete (){
MList. onLoadComplete ();
}


Public void setPullUpLoadMode (CustomListView. LoadMode loadMode ){
MList. setLoadMode (loadMode );
}
}

Related Article

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.