ViewPager + Fragment 实现类微信界面

先上个效果图,我模仿的这个,实际项目稍有修改,参考文章地址:https://www.cnblogs.com/xiaojianli/p/5671196.html

,

1,下面贴我的代码,首先mainfest.xml注册Activity

        <activity android:name="com.dami.student.StudentActivity"
                   android:theme="@android:style/Theme.Black.NoTitleBar"></activity>

2,Activity代码

package com.dami.student;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dami.hello.R;

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

public class StudentActivity extends FragmentActivity implements View.OnClickListener{

    private ViewPager mViewpager;
    private StudentFragmentPageAdapter mStudentFragmentPageAdapter;
    private List<Fragment> mList = new ArrayList<Fragment>();

    private ImageView mStudentHomePageIM,mStudentChatIM,mStudentMineIM;//bottom
    private TextView  mStudentTitleTV;//top
    private final int STUDENT_HOME_PAGE = 0;
    private final int STUDENT_CHAT = 1;
    private final int STUDENT_MINE = 2;

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

        setContentView(R.layout.activity_student);
        inItFragmentList();
        inItViewPager();
        inIt();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.IM_student_home_page:
                updateTopTitle(STUDENT_HOME_PAGE);
                break;
            case R.id.IM_student_chat:
                updateTopTitle(STUDENT_CHAT);
                break;
            case R.id.IM_student_mine:
                updateTopTitle(STUDENT_MINE);
                break;
        }
    }

    private void inItFragmentList(){

        mList.add(new StudentHomePageFragment());
        mList.add(new StudentChatFragment());
        mList.add(new StudentMineFragment());
    }

    private void inItViewPager(){

        mViewpager = (ViewPager) findViewById(R.id.student_viewpage);
        mStudentFragmentPageAdapter = new StudentFragmentPageAdapter(getSupportFragmentManager(),mList);
        mViewpager.setAdapter(mStudentFragmentPageAdapter);
        mViewpager.setCurrentItem(0);

        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                updateTopTitle(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void inIt(){

        mStudentHomePageIM = (ImageView)findViewById(R.id.IM_student_home_page);
        mStudentChatIM = (ImageView)findViewById(R.id.IM_student_chat);
        mStudentMineIM = (ImageView)findViewById(R.id.IM_student_mine);
        mStudentHomePageIM.setOnClickListener(this);
        mStudentChatIM.setOnClickListener(this);
        mStudentMineIM.setOnClickListener(this);
        mStudentTitleTV = (TextView)findViewById(R.id.TV_student_title);

        updateTopTitle(0);
    }

    private void setBackground(){
        mStudentHomePageIM.setBackgroundResource(R.drawable.student_home_page_normal);
        mStudentChatIM.setBackgroundResource(R.drawable.student_chat_normal);
        mStudentMineIM.setBackgroundResource(R.drawable.student_mine_normal);
    }

    private void updateTopTitle(int position){
        setBackground();
        switch (position){
            case 0:
                mStudentTitleTV.setText(R.string.student_home_page);
                mStudentHomePageIM.setBackgroundResource(R.drawable.student_home_page_pressed);
                break;
            case 1:
                mStudentTitleTV.setText(R.string.student_chat);
                mStudentChatIM.setBackgroundResource(R.drawable.student_chat_pressed);
                break;
            case 2:
                mStudentTitleTV.setText(R.string.student_mine);
                mStudentMineIM.setBackgroundResource(R.drawable.student_mine_pressed);
                break;
        }

        mViewpager.setCurrentItem(position);
    }

}
3,activity布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/white">

    <include layout="@layout/student_top"></include>

    <android.support.v4.view.ViewPager
        android:id="@+id/student_viewpage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>

    <include layout="@layout/student_bottom"></include>
</LinearLayout>
4,fragment ,3个都一样,只贴出一个

public class StudentHomePageFragment extends Fragment {

    private View studentHomePageFragmentView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        studentHomePageFragmentView = inflater.inflate(R.layout.fragment_student_home_page, container, false);
        return studentHomePageFragmentView;
    }

}
下面是布局
<FrameLayout 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"
    tools:context="com.dami.hello.com.dami.student.StudentHomePageFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="首页" />

</FrameLayout>
5,viewpage的适配器

package com.dami.student;

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

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

/**
 * Created by Administrator on 2018/2/3.
 */

public class StudentFragmentPageAdapter extends FragmentPagerAdapter {

    private List<Fragment> mList = new ArrayList<Fragment>();

    public StudentFragmentPageAdapter(FragmentManager fm, List<Fragment> fragmentList) {
        super(fm);
        mList = fragmentList;
    }

    @Override
    public Fragment getItem(int position) {
        return mList.get(position);
    }

    @Override
    public int getCount() {
        return mList.size();
    }
}
下面发项目下载地址,犹豫我测试很多别的东西,没有分开,有用部分如下图:https://pan.baidu.com/s/1gg7idUV








阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_25815655/article/details/79261879
想对作者说点什么? 我来说一句

ViewPager结合Fragment

2016年03月17日 1.23MB 下载

没有更多推荐了,返回首页

不良信息举报

ViewPager + Fragment 实现类微信界面

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭