Some time ago, I found an example of Sliding between left and right on the Internet, which is widely used in well-known software such as uc and moji weather. I implemented a layout class on the Internet (for details about the code, see the attachment) and inherited FrameLayout, the statement is as follows:
Public class FlingGallery extends FrameLayout;
The declared variables of this class are as follows:
Private int mGalleryWidth = 0;
Private boolean mIsTouched = false;
Private boolean mIsDragging = false;
Private float mCurrentOffset = 0.0f;
Private long mScrollTimestamp = 0;
Private int mFlingDirection = 0;
Private int mCurrentPosition = 0;
Private int mCurrentViewNumber = 0;
Private Context mContext;
Private Adapter mAdapter;
Private FlingGalleryView [] mViews;
Private FlingGalleryAnimation mAnimation;
Private GestureDetector mGestureDetector;
Private Interpolator mDecelerateInterpolater;
The main variables are mCurrentPosition: The current index.
MCurrentViewNumber: Index of the current view.
MViews is used to store a group of sliding views.
MAnimation animation action.
Class also encapsulates common switching methods similar to jump to the next view and the previous view, as well as some common layout methods.
You can use the following in activity:
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
MGallery = new FlingGallery (this );
// MGallery. setPaddingWidth (5 );
MGallery. setAdapter (mBabyListAdapter );
MGallery. setIsGalleryCircular (true );
LinearLayout layout = new LinearLayout (getApplicationContext ());
Layout = (LinearLayout) PublicFunctionAndUnit. getMainView (
GalleryTest. this, layout, mGallery );
Layout. setBackgroundResource (R. drawable. main_bg );
SetContentView (layout );
}
Put the views to be displayed in the adapter. The Code is as follows:
BaseAdapter mBabyListAdapter = new BaseAdapter (){
@ Override
Public int getCount (){
Return 7;
}
@ Override
Public Object getItem (int position ){
Return null;
}
@ Override
Public long getItemId (int position ){
Return 0;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// GalleryViewItem item = new GalleryViewItem (GalleryTest. this,
// Position );
LinearLayout item = getLinearLayout (GalleryTest. this, position );
Return item;
}
};
Private int [] itemViewIds = new int [] {R. layout. item1, R. layout. item2,
R. layout. item3, R. layout. item4, R. layout. item5, R. layout. item6,
R. layout. item7 };
Private LinearLayout getLinearLayout (Context context, int position ){
LinearLayout layout = new LinearLayout (context );
LayoutInflater factory = LayoutInflater. from (context );
View item = factory. inflate (itemViewIds [position], null );
Layout. addView (item, new LinearLayout. LayoutParams (
LinearLayout. LayoutParams. FILL_PARENT,
LinearLayout. LayoutParams. FILL_PARENT ));
Return layout;
}
During usage, it is found that this class only has the method to jump to the previous and next views. You cannot specify to jump to a view. After carefully observing the code, this method can be implemented using the nextd provided by it. The Code is as follows:
Public void moveToViewbyId (int id)
{
Int num = id-mCurrentPosition;
If (num! = 0)
{
If (num> 0)
{
For (int I = 0; I <num; I ++)
{
MoveNext ();
}
}
Else
{
For (int I = 0; I <(0-num); I ++)
{
MovePrevious ();
}
}
}
}
In this case, this method is actually used from the previous step and the next method. The current id is subtracted from the id to be redirected, and the number of jumps is obtained, if you thoroughly modify the moveNext method or the movePrevious method in this class, you can also perform this operation, but this method is not concise and clear.
This article is from the "HDDevTeam" blog