只有在不断地学习和整理大脑硬盘中的知识才能有所提升,给大家贡献一个VIewpager实现的侧滑切换界面的DEMO!
废话不多少,直接上代码!
主界面
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private TextView framework_tvo;
private View frameworko;
private TextView framework_tvt;
private View framework_ot;
private View view;
private TextView frameworkt_tv;
List<Fragment> mFragmentList = new ArrayList<Fragment>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
init();
}
private void initView() {
//初始化控件
vp = (ViewPager) findViewById(R.id.ff);
//1
framework_tvo = (TextView) findViewById(R.id.framework_tvo);
frameworko = findViewById(R.id.frameworko);
//2
framework_tvt = (TextView) findViewById(R.id.framework_tvt);
framework_ot = findViewById(R.id.framework_ot);
//3
view = findViewById(R.id.framework_t);
frameworkt_tv = (TextView) findViewById(R.id.frameworkt_tv);
}
private void init() {
//初始化Fragment参数
TwoFragment tf = new TwoFragment();
OneFragment of = new OneFragment();
MeFragment mf = new MeFragment();
mFragmentList.add(tf);
mFragmentList.add(of);
mFragmentList.add(mf);
//适配器
FragmentPagerAdapter mFragmentAdapter = new FragmentPagerAdapter(
this.getSupportFragmentManager(), mFragmentList);
vp.setAdapter(mFragmentAdapter);
vp.setCurrentItem(0);
//Viewpager的滑动监听
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//Viewpager侧滑切换文字颜色,和底部颜色
Log.i("ppp",position+"");
if (position==1){
framework_tvo.setTextColor(MainActivity.this.getResources().getColor(R.color.btn_gray_pressed_status));
frameworko.setVisibility(View.GONE);
frameworkt_tv.setTextColor(MainActivity.this.getResources().getColor(R.color.btn_gray_pressed_status));
view.setVisibility(View.GONE);
framework_tvt.setTextColor(MainActivity.this.getResources().getColor(R.color.blue));
framework_ot.setVisibility(View.VISIBLE);
}else if(position==0){
framework_tvo.setTextColor(MainActivity.this.getResources().getColor(R.color.blue));
frameworko.setVisibility(View.VISIBLE);
frameworkt_tv.setTextColor(MainActivity.this.getResources().getColor(R.color.btn_gray_pressed_status));
view.setVisibility(View.GONE);
framework_tvt.setTextColor(MainActivity.this.getResources().getColor(R.color.btn_gray_pressed_status));
framework_ot.setVisibility(View.GONE);
}else if(position==2){
framework_tvo.setTextColor(MainActivity.this.getResources().getColor(R.color.btn_gray_pressed_status));
frameworko.setVisibility(View.GONE);
frameworkt_tv.setTextColor(MainActivity.this.getResources().getColor(R.color.blue));
view.setVisibility(View.VISIBLE);
framework_tvt.setTextColor(MainActivity.this.getResources().getColor(R.color.btn_gray_pressed_status));
framework_ot.setVisibility(View.GONE);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
ViewPager的适配器
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import java.util.List;
/**
* Created by TP_zhao on 2016/10/28.
* Viewpager的适配器
*/
public class FragmentPagerAdapter extends android.support.v4.app.FragmentPagerAdapter {
FragmentManager mmanagerl;
List<Fragment> mfrgmentList;
public FragmentPagerAdapter(FragmentManager manager, List<Fragment> frgmentList){
super(manager);
mmanagerl = manager;
mfrgmentList = frgmentList;
}
public Fragment getItem(int position) {
return mfrgmentList.get(position);
}
@Override
public int getCount() {
return mfrgmentList.size();
}
}
三个Fragment
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by TP_zhao on 2016/10/28.
*/
public class OneFragment extends Fragment {
//第一个聊天界面
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.chat_fragment,container,false);
return view;
}
}
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by TP_zhao on 2016/10/28.
*/
public class TwoFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.viewpager_fragment,container,false);
return view;
}
}
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by TP_zhao on 2016/10/28.
*/
public class MeFragment extends Fragment {
private View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.me_fragment,container,false);
return view;
}
}
其他的xml文件什么的,在这里就暂时不贴了,如果有需要请 点击下载源码!