@ContentView(R.layout.activity_main) public class MainActivity extends AppCompatActivity implements View.OnClickListener{ @ViewInject(R.id.hsv_title) private HorizontalScrollView hsvTitle; @ViewInject(R.id.ll_title) private LinearLayout llTitle; @ViewInject(R.id.vp_content) private ViewPager vpContent; @ViewInject(R.id.btn_main_login) private ImageView btnLogin; private List<String> titles; private SlidingMenu mMenu; //private List<Fragment> fragments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); x.view().inject(this); getSlidingMenu(); initData(); addTitleAndFragment(); vpContent.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return ContentFragment.newInstance(titles.get(position)); } @Override public int getCount() { return titles.size(); } }); vpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (position == 0){ mMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); }else { mMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE); } } @Override public void onPageSelected(int position) { changeTextColor(position); int margins = 20 * position; int widthTotle = 0; for (int j = 0;j < position;j++){ TextView txtV = (TextView) llTitle.getChildAt(j); widthTotle += txtV.getWidth(); } hsvTitle.scrollTo((margins + widthTotle),0); } @Override public void onPageScrollStateChanged(int state) { } }); } private void getSlidingMenu() { //初始化SlidingMenu mMenu = new SlidingMenu(this); mMenu.setMode(SlidingMenu.LEFT); mMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); mMenu.setShadowDrawable(R.color.colorAccent); mMenu.setShadowWidth(R.dimen.shadow_width); mMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset); mMenu.setFadeDegree(0.35f); mMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT); mMenu.setMenu(R.layout.left_menu); } private void changeTextColor(int position) { for (int i = 0;i < titles.size();i++){ TextView txtview = (TextView) llTitle.getChildAt(i); if (position == i){ txtview.setTextColor(Color.RED); }else { txtview.setTextColor(Color.BLACK); } } } private void addTitleAndFragment() { for (int i = 0;i < titles.size();i++){ TextView txt = new TextView(this); txt.setText(titles.get(i)); txt.setTextSize(14); if (i == 0){ txt.setTextColor(Color.RED); }else { txt.setTextColor(Color.BLACK); } txt.setId(i + 1000); txt.setOnClickListener(MainActivity.this); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.setMargins(10,5,10,5); txt.setLayoutParams(params); llTitle.addView(txt); // Fragment fragment = ContentFragment.newInstance(titles.get(i)); // fragments.add(fragment); } } private void initData() { titles = new ArrayList<>(); // fragments = new ArrayList<>(); titles.add("推荐"); titles.add("热点"); titles.add("北京"); titles.add("视频"); titles.add("社会"); titles.add("娱乐"); titles.add("科技"); titles.add("问答"); titles.add("汽车"); titles.add("财经"); titles.add("军事"); titles.add("体育"); titles.add("段子"); titles.add("美女"); titles.add("国际"); titles.add("趣图"); titles.add("健康"); titles.add("特卖"); titles.add("房产"); titles.add("育儿"); } @Override public void onClick(View v) { int id = v.getId(); // 拿到textview的position int position = id - 1000; vpContent.setCurrentItem(position); // 点击变色 changeTextColor(position); } @Event(R.id.btn_main_login) public void btnLoginClcik(View v){ Intent intent = new Intent(this,LoginActivity.class); startActivity(intent); }