public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button bt1; private Button bt2; private Button bt3; private Button bt4; private ViewPager viewpager; private int count=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewpager = (ViewPager) findViewById(R.id.viewpager); bt1 = (Button) findViewById(R.id.bt1); bt2 = (Button) findViewById(R.id.bt2); bt3 = (Button) findViewById(R.id.bt3); bt4 = (Button) findViewById(R.id.bt4); bt1.setOnClickListener(this); bt2.setOnClickListener(this); bt3.setOnClickListener(this); bt4.setOnClickListener(this); final List<Fragment> list=new ArrayList<Fragment>(); Fragment1 fragment1 = new Fragment1(); Fragment2 fragment2 = new Fragment2(); Fragment3 fragment3 = new Fragment3(); Fragment4 fragment4 = new Fragment4(); list.add(fragment1); list.add(fragment2); list.add(fragment3); list.add(fragment4); viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } @Override public void destroyItem(ViewGroup container, int position, Object object) { } }); viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { 滑动viewpager/Button随之滑动 aaa(position); } @Override public void onPageScrollStateChanged(int state) { } }); bt1.setTextColor(Color.RED); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.bt1: count=0; break; case R.id.bt2: count=1; break; case R.id.bt3: count=2; break; case R.id.bt4: count=3; break; } viewpager.setCurrentItem(count); aaa(count); } public void aaa(int count){ bt1.setTextColor(count==0? Color.RED:Color.BLACK); bt2.setTextColor(count==1? Color.RED:Color.BLACK); bt3.setTextColor(count==2? Color.RED:Color.BLACK); bt4.setTextColor(count==3? Color.RED:Color.BLACK); } } //Fragment举一例public class Fragment1 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment01,container,false); } } //布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.fragment.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_weight="8" android:layout_height="0dp"> </android.support.v4.view.ViewPager> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="头条" android:textSize="25dp" /> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="国内" android:textSize="25dp" /> <Button android:id="@+id/bt3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="军事" android:textSize="25dp" /> <Button android:id="@+id/bt4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="科技" android:textSize="25dp" /> </LinearLayout>
Fragment与Button对应滑动
最新推荐文章于 2019-02-15 10:56:44 发布