在线直播源码,开发app实现滑动切换界面

发布时间 2023-08-22 14:07:57作者: 云豹科技-苏凌霄

在线直播源码,开发app实现滑动切换界面

step1:首先新建一个的.java文件,为主activity,本设计中以Main.java为例,代码如下:

 


public class Main extends AppCompatActivity implements View.OnClickListener{
    private TextView title,item_favourite,item_query,item_mine;
    private ViewPager vp;
    private Favourite favourite;//调用推荐界面
    private Query query;//调用查询界面
    private Mine mine;//调用我界面
    private List<Fragment> mFragmentList=new ArrayList<Fragment>();
    private FragmentAdapter mFragmentAdapter;
    String[] titles= new String[]{"推荐","查询","我"};//设置标题内容,本设计有三个界面,分别是推荐、查询、我
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initViews();
        mFragmentAdapter=new FragmentAdapter(this.getSupportFragmentManager(), mFragmentList) ;
        vp.setOffscreenPageLimit(3);//有几个界面就写几个
        vp.setAdapter(mFragmentAdapter);
        vp.setCurrentItem(0);
        item_favourite.setTextColor(Color.parseColor("#66CDAA"));//设置标题颜色
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
            @Override
            public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels){
            }
            @Override
            public void onPageSelected(int position){
                title.setText(titles[position]);
                changeTextColor(position);
            }
            @Override
            public void onPageScrollStateChanged(int state){
            }
        });
    }
    private void initViews() {
        title = (TextView) findViewById(R.id.title);//标题文本
        item_favourite=(TextView)findViewById(R.id.item_favourite);//推荐 文本
        item_query = (TextView) findViewById(R.id.item_query);//查询 文本
        item_mine = (TextView) findViewById(R.id.item_mine);//我 文本
        item_favourite.setOnClickListener(this);
        item_query.setOnClickListener(this);
        item_mine.setOnClickListener(this);
        vp = (ViewPager) findViewById(R.id.mainViewPager);
        favourite = new Favourite();
        query= new Query();
        mine=new Mine();
        //给FragmentList添加数据
        mFragmentList.add(favourite);
        mFragmentList.add(query);
        mFragmentList.add(mine);
    }
    //点击底部Text动态修改ViewPager内容
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.item_favourite:
                vp.setCurrentItem(0,true);
                break;
            case R.id.item_query:
                vp.setCurrentItem(1,true);
                break;
            case R.id.item_mine:
                vp.setCurrentItem(2,true);
                break;
        }
    }
    public class FragmentAdapter extends FragmentPagerAdapter{
        List<Fragment> fragmentList=new ArrayList<Fragment>();
        public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList){
            super(fm);
            this.fragmentList=fragmentList;
        }
        @Override
        public Fragment getItem(int position){
            return fragmentList.get(position);
        }
        @Override
        public int getCount(){
            return fragmentList.size();
        }
    }
    //修改底部导航颜色
    private void changeTextColor(int position){
        if (position==0){
            item_favourite.setTextColor(Color.parseColor("#66CDAB"));
            item_query.setTextColor(Color.parseColor("#ff000000"));
            item_mine.setTextColor(Color.parseColor("#ff000000"));
        }else if (position==1){
            item_favourite.setTextColor(Color.parseColor("#ff000000"));
            item_query.setTextColor(Color.parseColor("#66CDAB"));
            item_mine.setTextColor(Color.parseColor("#ff000000"));
        }
        else if (position==2){
            item_favourite.setTextColor(Color.parseColor("#ff000000"));
            item_query.setTextColor(Color.parseColor("#ff000000"));
            item_mine.setTextColor(Color.parseColor("#66CDAB"));
        }
    }
}
 

step2:接下来新建一个main.xml,用于在Main.java中显示

代码如下:

 


<?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">
<!--顶部导航设置-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/holo_green_dark">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="推荐"
            android:id="@+id/title"
            android:layout_centerInParent="true"
            android:textColor="@android:color/white"
            android:textSize="20sp"/>
    </RelativeLayout>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/mainViewPager"/>
<!--底部导航栏设置-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="1dp"
        android:background="@android:color/white"
        android:baselineAligned="false"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingTop="5dp">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/item_favourite"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal"
            android:text="推荐"
            android:textColor="@android:color/black"
            android:textSize="18dp" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/item_query"
            android:layout_gravity="center_horizontal"
            android:text="查询"
            android:layout_weight="1"
            android:textColor="@android:color/black"
            android:textSize="18dp" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/item_mine"
            android:layout_gravity="center_horizontal"
            android:text="我"
            android:textColor="@android:color/black"
            android:layout_weight="1"
            android:textSize="18dp" />
    </LinearLayout>
</LinearLayout> 

 

以上就是 在线直播源码,开发app实现滑动切换界面,更多内容欢迎关注之后的文章