Android developer Headlines (a) launch page implementation _android

Source: Internet
Author: User
Tags object object


No more nonsense, start today's topic, take you to achieve the developer Headlines app launch page.



I. The old rules, first the effect chart






From the effect diagram we can see that the entire sliding interface is a Viewpager implementation, and then listen to Viewpager sliding events, change the bottom four small icon switch, and jump to the first page of the hidden display of the button.



Two. Code implementation



1). The entire layout file. Above is the Viewpager, below is the four small icon storage container.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FCF2E4">
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager_launcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <LinearLayout
            android:id="@+id/viewGroup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="30dp"
            android:gravity="center_horizontal"
            android:orientation="horizontal"/>
    </RelativeLayout>
</RelativeLayout>


2). Launcheractivity first to determine whether the first start, if not the first direct access to the first page, is the first time to start on the initialization Viewpager, set up the adapter, set Viewpager sliding monitoring, and then add the bottom of ... View. Change the selection of the bottom icon in the Viewpager listener function if the last page shows the "Open My Headline" button.


/**
 * First launch page
 *
 * @author Ansen
 * @create time 2016-04-15
 */
@SuppressLint("ResourceAsColor")
Public class LauncherActivity extends FragmentActivity implements ILauncherView {
    Private ViewPager viewpagerLauncher;
    Private launcherPagerAdapter adapter;

    Private ImageView[] tips;

    @Override
    Protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_luancher);

        If(!isFirst()){
            gotoMain();
        }
        viewpagerLauncher = (ViewPager) findViewById(R.id.viewpager_launcher);
        Adapter = new LauncherPagerAdapter(this, this);
        viewpagerLauncher.setOffscreenPageLimit(2);
        viewpagerLauncher.setCurrentItem(0);
        viewpagerLauncher.setOnPageChangeListener(changeListener);
        viewpagerLauncher.setAdapter(adapter);
        viewpagerLauncher.setOnPageChangeListener(changeListener);
        ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup);// Initialize the bottom display control
        Tips = new ImageView[4];
        For (int i = 0; i < tips.length; i++) {
            ImageView imageView = new ImageView(this);
            If (i == 0) {
                imageView.setBackgroundResource(R.drawable.page_indicator_focused);
            } else {
                imageView.setBackgroundResource(R.drawable.page_indicator_unfocused);
            }
            Tips[i] = imageView;
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            layoutParams.leftMargin = 10;// Set the dot left point of the view
            layoutParams.rightMargin = 10;// Set the right margin of the point view
            group.addView(imageView, layoutParams);
        }
    }

    Private OnPageChangeListener changeListener = new OnPageChangeListener() {
        @Override
        Public void onPageScrollStateChanged(int arg0) {}
        @Override
        Public void onPageScrolled(int arg0, float arg1, int arg2) {}
        @Override
        Public void onPageSelected(int index) {
            setImageBackground(index);// Change the effect of the point

            TextView tvStartHeadlines = (TextView) adapter.getViews().get(index).findViewById(R.id.tv_start_headlines);
            If (index == tips.length - 1) {// last
                tvStartHeadlines.setVisibility(View.VISIBLE);
            } else {
                tvStartHeadlines.setVisibility(View.INVISIBLE);
            }
        }
    };

    /**
     * Change the switching effect of a little bit
     * @param selectItems
     */
    Private void setImageBackground(int selectItems) {
        For (int i = 0; i < tips.length; i++) {
            If (i == selectItems) {
                Tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
            } else {
                Tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
            }
        }
    }

    @Override
    Public void gotoMain() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        Finish();
    }

    Private boolean isFirst() {
        SharedPreferences setting = getSharedPreferences("headlines", 0);
        Boolean user_first = setting.getBoolean("FIRST", true);
        If (user_first) {// first time
            Setting.edit().putBoolean("FIRST", false).commit();
            Return true;
        } else {
            Return false;
        }
    }
}


3). Launcherpageradapter Inherits Pageradapter, initializes all the pages to be displayed in the constructor, and the other is no different from the normal adapter, with two more rewriting methods, Destroyitem and Instantiateitem. Destroyitem is to delete a page, Instantiateitem is to load a page.


/**
 * ViewPager Adapter
 * @author ansen
 * @create time 2016-04-15
 */
Public class LauncherPagerAdapter extends PagerAdapter implements OnClickListener{
    Private ILauncherView launcherView;

    Private List<View> views;
    //The image displayed on each page
    Private int[] images=new int[]{R.drawable.tutorial_1, R.drawable.tutorial_2, R.drawable.tutorial_3, R.drawable.tutorial_4};

    Public LauncherPagerAdapter(Context context,ILauncherView launcherView){
        Views=new ArrayList<View>();
        this.launcherView=launcherView;
        / / Initialize the View displayed on each page
        For(int i=0;i<images.length;i++){
            View item=LayoutInflater.from(context).inflate(R.layout.activity_luancher_pager_item, null);
            ImageView imageview=(ImageView) item.findViewById(R.id.imageview);
            imageview.setImageResource(images[i]);
            item.findViewById(R.id.tv_start_headlines).setOnClickListener(this);
            Views.add(item);
        }
    }

    Public List<View> getViews() {
        Return views;
    }

    @Override
    Public int getCount() {
        Return views == null ? 0 : views.size();
    }

    @Override
    Public boolean isViewFromObject(View arg0, Object arg1) {
        Return arg0==arg1;
    }

    @Override
    Public void destroyItem(ViewGroup container, int position, Object object){
        ((ViewPager) container).removeView(views.get(position));
    }

    @Override
    Public Object instantiateItem(ViewGroup container, int position) {
        ((ViewPager) container).addView(views.get(position), 0);
        Return views.get(position);
    }

    @Override
    Public void onClick(View v) {
        launcherView.gotoMain();
    }
} 


4. There are also the item layouts for each page, and the background shape of the "open My Headline" button, which I will not post.



Developer headlines about Android Development (a) The launch page is really about so much. The next article introduces the developer of Android Development Headlines (ii) to achieve the left-sliding menu, interested friends to continue to pay attention to cloud Habitat community site!


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.