Android:详情界面左右滑动看更多,ViewPager动态添加Fragment,仿51job详情界面

在项目中遇到一个需求:在邮件列表中任意一个进入邮件详情,在详情界面中,可以左右滑动查看上一条或下一条邮件详情。


Android:详情界面左右滑动看更多,ViewPager动态添加Fragment,仿51job详情界面_第1张图片

Android:详情界面左右滑动看更多,ViewPager动态添加Fragment,仿51job详情界面_第2张图片

Android:详情界面左右滑动看更多,ViewPager动态添加Fragment,仿51job详情界面_第3张图片


界面简单,主要是实现效果



这时我们第一时间想到的就是ViewPager+Fragment,但是我的数据可能会有上百条,这就要考虑Fragment的回收问题,开始自己折腾了很久,后来网上查到了用FragmentStatePagerAdapter 代替 FragmentPagerAdapter, Android:FragmentStatePagerAdapter分页。这样实现起来非常简单切更可靠。具体实现看如下代码:


activity_main.xml




    




MainActivity.java

package com.hmy;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

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

public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private ViewPager mViewPager;

    private DetailPagerAdapter mAdapter;
    private List mList;
    private int TOTAL_SIZE = 26;
    private final int PAGE_SIZE = 10;
    private int mCount = 0;

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

        initData();
        initView();
    }

    private void initData() {
        mContext = this;
        mList = new ArrayList<>();
        for (int i = 0; i < PAGE_SIZE; i++) {
            mCount++;
            mList.add(mCount);
        }
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.vp_detail);

        mAdapter = new DetailPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(new OnFragmentPageChangeListener());
        mViewPager.setCurrentItem(mList.size() / 2);
    }


    private class DetailPagerAdapter extends FragmentStatePagerAdapter {

        public DetailPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return DetailFragment.newInstance(position);
        }

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

    private class OnFragmentPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float offsetPerc,
                                   int offsetPixel) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }

        @Override
        public void onPageSelected(int position) {
            int size = mList.size();
            if (position == size - 1 && TOTAL_SIZE > size) {
                //实际项目中是网络请求下一页的列表数据
                getMoreDetailList();
            }

        }
    }

    private void getMoreDetailList() {
        for (int i = mCount; i < TOTAL_SIZE; i++) {
            mCount++;
            mList.add(mCount);
        }
        mAdapter.notifyDataSetChanged();
    }
}


fragment_detail.xml



    



DetailFragment.java

package com.hmy;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DetailFragment extends Fragment {
    private TextView mIdTv;

    private static final String ARG_PARAM1 = "param1";
    private int mParam1;

    public DetailFragment() {
        // Required empty public constructor
    }

    public static DetailFragment newInstance(int param1) {
        DetailFragment fragment = new DetailFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getInt(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_detail, container, false);
        mIdTv = (TextView) view.findViewById(R.id.tv_id);

        mIdTv.setText("-----" + mParam1 + "-----");
        return view;
    }


}


Demo代码下载: http://download.csdn.net/detail/hmyang314/9261467

你可能感兴趣的:(Android,Android_控件相关)