Key points for using Viewpager to implement sliding paging in Android app _android

Source: Internet
Author: User

In the past, if you want to do Tab paging, you must use a difficult to use the tabactivity, and the effect is very poor, flexibility is very small

Forget when to start, Google release Viewpager This good thing to replace the previously difficult to use the Gallery components, coupled with the Fragment from the honeycomb after finally able to simply make good-looking and useful Layout!

Here we use Pagertabstrip, the effect is as follows

Feature is the use of simple, out of the effect is currently displayed in the paging Tab text will be automatically placed in, and then display the previous/Next tab, respectively.
And when sliding Tab, the following pagination will automatically switch.
The implementation method is as follows:
Your_layout.xml

...
<android.support.v4.view.viewpager
  android:id= "@+id/pager"
  android:layout_width= "Match_parent"
  android:layout_height= "Wrap_content" >
 
  <android.support.v4.view.pagertabstrip
    android:id= "@+id/ Pagettab "
    android:layout_width=" match_parent "
    android:layout_height=" wrap_content "
    android:layout_" gravity= "Top"/>
  
</android.support.v4.view.ViewPager>

...
First in your Layout file, find the place where you place the Viewpager, insert the Pagertabstrip, where the android:layout_gravity can designate top or bottom see if you want him in VIEWP Above or below the ager.
Then in the Java code part.
Mainactivity.java

...
Pagertabstrip TabStrip = (pagertabstrip) Findviewbyid (r.id.pagettab);
Tabstrip.setdrawfullunderline (true);
Tabstrip.settabindicatorcolorresource (Android. R.color.holo_blue_dark);
Tabstrip.setbackgroundcolor (color.white);
...

Basically, you open the part of the indicator bar and then set the background and the color of the indicator bar.
It's done!

Prevent the viewpager from sliding around
If you want to prevent sliding, you override the Viewpager onintercepttouchevent (motionevent arg0) method and the Ontouchevent (Motionevent arg0) method. The return values of both methods are Boolean, and simply change the return value to false so that Viewpager does not consume the event that the finger slides, and then passes it to the upper view to handle it or the event terminates directly.

public class Myviewpager extends Viewpager {//private static final String TAG = "Viewpager";
 
  Whether to prohibit the left and right sliding private Boolean disablescroll=false;
  Public Myviewpager {Super (context);
  Public Myviewpager (context, AttributeSet attrs) {Super (context, attrs); @Override public boolean dispatchtouchevent (motionevent ev) {//Loghelper.i (TAG, "Myviewpager Dispatchtouchev
    ENT, ");
    if (getparent ()!= null) {GetParent (). Requestdisallowintercepttouchevent (True);
  return super.dispatchtouchevent (EV);
    @Override public boolean onintercepttouchevent (Motionevent arg0) {if (Disablescroll) {return false;
  Return super.onintercepttouchevent (arg0);
    @Override public boolean ontouchevent (Motionevent arg0) {if (disablescroll) return false;
  else return super.ontouchevent (arg0);
  public Boolean Isdisablescroll () {return disablescroll; } public void SetDisablescroll (Boolean disablescroll) {this.disablescroll = Disablescroll;
 }
 
}

 

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.