赞
踩
package com.bwei.headline; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.view.Gravity; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import com.bwei.adapter.MyFragmentAdapter; import com.bwei.fragment.FragmentFind; import com.bwei.fragment.FragmentPageHome; import com.bwei.fragment.FragmentSmallVideo; import com.bwei.fragment.FragmentVideo; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ViewPager fragment_view_pager; private RadioButton fragment_homepage; private RadioButton fragment_video; private RadioButton fragment_find; private RadioButton fragment_smallvideo; private RadioGroup fragment_radio_group; private FragmentPageHome fragmentPageHome; private FragmentVideo fragmentVideo; private FragmentFind fragmentFind; private FragmentSmallVideo fragmentSmallVideo; private ArrayList<Fragment> fragments; private MyFragmentAdapter myFragmentAdapter; private DrawerLayout drawerLayout; private ImageView icon; private LinearLayout cela; private TextView ziliao; private TextView gerenzhongxin; private TextView qianbao; private TextView wode; private TextView shezhi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化控件 initView(); // 服务代码 getServer(); // 侧拉布局中的点击时间 onClick1(); } // 初始化控件 private void initView() { fragment_view_pager = (ViewPager) findViewById(R.id.fragment_view_pager); fragment_homepage = (RadioButton) findViewById(R.id.fragment_homepage); fragment_video = (RadioButton) findViewById(R.id.fragment_video); fragment_find = (RadioButton) findViewById(R.id.fragment_find); fragment_smallvideo = (RadioButton) findViewById(R.id.fragment_smallvideo); fragment_radio_group = (RadioGroup) findViewById(R.id.fragment_radio_group); drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); icon = (ImageView) findViewById(R.id.icon); ziliao = (TextView) findViewById(R.id.ziliao); gerenzhongxin = (TextView) findViewById(R.id.gerenzhongxin); qianbao = (TextView) findViewById(R.id.qianbao); wode = (TextView) findViewById(R.id.wode); shezhi = (TextView) findViewById(R.id.shezhi); } // 服务代码 public void getServer() { // 创建Fragment fragmentPageHome = new FragmentPageHome(); fragmentVideo = new FragmentVideo(); fragmentFind = new FragmentFind(); fragmentSmallVideo = new FragmentSmallVideo(); // 创建集合 fragments = new ArrayList<>(); fragments.add(fragmentPageHome); fragments.add(fragmentVideo); fragments.add(fragmentFind); fragments.add(fragmentSmallVideo); // 创建适配器 myFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(), fragments); fragment_view_pager.setAdapter(myFragmentAdapter); // 滑动切换页面 fragment_view_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { fragment_radio_group.check(fragment_radio_group.getChildAt(position % fragments.size()).getId()); } @Override public void onPageScrollStateChanged(int state) { } }); // 点击按钮切换界面 fragment_radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i) { case R.id.fragment_homepage: fragment_view_pager.setCurrentItem(0); break; case R.id.fragment_video: fragment_view_pager.setCurrentItem(1); break; case R.id.fragment_find: fragment_view_pager.setCurrentItem(2); break; case R.id.fragment_smallvideo: fragment_view_pager.setCurrentItem(3); break; } } }); // 侧拉 首页跟着滑动 drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerSlide(View drawerView, float slideOffset) { View childAt = drawerLayout.getChildAt(0); float i = 1 - slideOffset; childAt.setTranslationX(drawerView.getMeasuredWidth() * (1 - i)); } @Override public void onDrawerOpened(View drawerView) { } @Override public void onDrawerClosed(View drawerView) { } @Override public void onDrawerStateChanged(int newState) { } }); icon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { drawerLayout.openDrawer(Gravity.LEFT); } }); } private void onClick1() { ziliao.setOnClickListener(this); gerenzhongxin.setOnClickListener(this); qianbao.setOnClickListener(this); wode.setOnClickListener(this); shezhi.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.ziliao: Toast.makeText(MainActivity.this,"xxx",Toast.LENGTH_SHORT); break; } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。