Fragment系列之(1)---Fragment 和ViewPager的使用简介

Fragment是Android开发中常用的组件。而配合ViewPager使用,更是使Fragment如虎添翼,给用户提供一种很享受的极致体验。而且开发起来特别方便,仅仅需要很少的代码,就可以完成需求。
但是,凡事有利有弊,Fragment+ViewPager的模式,减少了开发工作量,但当遇到问题,如fragment的回收和重建等问题时,就会让人无从下手了。
今天,这个Fragment系列,我将从Fragment和ViewPager的使用入手,通过Adapter的浅析Fragment和ViewPager的原理。

废话不多说,下面贴一下Fragment使用的主要代码:

首先上一下代码项目的代码:
Activity 的代码

package com.gradle.demo.fragmentdemo.liferecycle.activity;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.gradle.demo.fragmentdemo.R;
import com.gradle.demo.fragmentdemo.liferecycle.adapter.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class LifeRecycleActivity extends AppCompatActivity {
    ViewPager mViewPager;
    ViewPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_life_recycle);


        mViewPager = (ViewPager) findViewById(R.id.view_pager);

        List list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add("this is fragment index :" + i);
        }

        mAdapter = new ViewPagerAdapter(getSupportFragmentManager(), list);
        mViewPager.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }
}

*** activity中布局 ***






    


 

Adapter 的代码

package com.gradle.demo.fragmentdemo.liferecycle.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.gradle.demo.fragmentdemo.liferecycle.fragment.LifeRecycleFragment;
import com.gradle.demo.fragmentdemo.viewpager.EvenFragment;
import com.gradle.demo.fragmentdemo.viewpager.OddFragment;

import java.util.List;

/**
 * Created by fanguanggcheng on 2017/7/3.
 */

public class ViewPagerAdapter extends FragmentPagerAdapter { //FragmentPagerAdapter   // FragmentStatePagerAdapter

    private List mList;
    public ViewPagerAdapter(FragmentManager fm, List list) {
        super(fm);
        mList = list;
    }

    @Override
    public Fragment getItem(int position) {
        Bundle bundle = new Bundle();
        bundle.putString("fragment_desc", mList.get(position));
        bundle.putInt("fragment_index", position);
        LifeRecycleFragment fragment = new LifeRecycleFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }
}

***Fragment 的代码 ***

package com.gradle.demo.fragmentdemo.liferecycle.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.gradle.demo.fragmentdemo.R;

/**
 * Created by fanguanggcheng on 2017/7/5.
 */

public class LifeRecycleFragment extends Fragment {

    private static final String FRAGMENT_TAG = "fragment_tag";
    private String mFragmentDesc = "";
    private int    mFragmentIndex;

    public LifeRecycleFragment() {
        super();
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initParams();

        Log.d(FRAGMENT_TAG, "onCreate ,         index:  " + mFragmentIndex);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d(FRAGMENT_TAG, "onCreateView ,     index:  " + mFragmentIndex);

        View view = getFragmentView(inflater, container);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d(FRAGMENT_TAG, "onViewCreated ,    index:  " + mFragmentIndex);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(FRAGMENT_TAG, "onStart ,          index:  " + mFragmentIndex);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(FRAGMENT_TAG, "onResume ,         index:  " + mFragmentIndex);
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(FRAGMENT_TAG, "onPause ,          index:  " + mFragmentIndex);
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(FRAGMENT_TAG, "onStop ,           index:  " + mFragmentIndex);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(FRAGMENT_TAG, "onDestroyView ,    index:  " + mFragmentIndex);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(FRAGMENT_TAG, "onDestroy ,        index:  " + mFragmentIndex);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d(FRAGMENT_TAG, "onDetach ,         index:  " + mFragmentIndex);
    }

    private void initParams() {
        Bundle bundle = getArguments();
        if (null == bundle) {
            return;
        }
        mFragmentDesc = bundle.getString("fragment_desc", "this is even fragment");
        mFragmentIndex = bundle.getInt("fragment_index", 0);
    }

    private View getFragmentView(LayoutInflater inflater, @Nullable ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_life_recycle_layout, container, false);
        TextView textView = (TextView)view.findViewById(R.id.tv_fragment_desc);
        textView.setText(mFragmentDesc);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LifeRecycleFragment.this.getContext(), mFragmentDesc, Toast.LENGTH_SHORT).show();
            }
        });
        if (mFragmentIndex % 2 == 0) {
            textView.setBackgroundColor(getActivity().getResources().getColor(R.color.green_color));
        } else {
            textView.setBackgroundColor(getActivity().getResources().getColor(R.color.red_color));
        }
        return view;
    }
}

Fragment中的布局文件代码



    


接下来看下效果图:

Fragment系列之(1)---Fragment 和ViewPager的使用简介_第1张图片
效果图.gif

你可能感兴趣的:(Fragment系列之(1)---Fragment 和ViewPager的使用简介)