今天主要实现viewPager切换 即可以点击并且可以滑动,下面有一条线也是可以一起滑动的。
效果图:
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:focusableInTouchMode="true" android:focusable="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" android:orientation="horizontal" > <TextView android:onClick="onClick" android:id="@+id/bt_drug_one1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:paddingBottom="10dp" android:paddingTop="10dp" android:text="全部" android:textSize="15dp" android:textColor="#CC0000" /> <TextView android:onClick="onClick" android:id="@+id/bt_drug_two2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:paddingBottom="10dp" android:paddingTop="10dp" android:text="Tab1" android:textSize="15dp" android:textColor="#474747" /> <TextView android:onClick="onClick" android:id="@+id/bt_drug_three3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:paddingBottom="10dp" android:paddingTop="10dp" android:text="Tab2" android:textSize="15dp" android:textColor="#474747" /> <TextView android:onClick="onClick" android:id="@+id/bt_drug_four4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:paddingBottom="10dp" android:paddingTop="10dp" android:text="Tab3" android:textSize="15dp" android:textColor="#474747" /> </LinearLayout> <LinearLayout android:background="#CCCCCC" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <View android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:id="@+id/show_one" android:layout_width="0dp" android:layout_height="2dp" android:layout_weight="1" android:background="#CC0000" /> <View android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:id="@+id/show_two" android:layout_width="0dp" android:layout_height="2dp" android:layout_weight="1" android:background="#CCCCCC" /> <View android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:id="@+id/show_three" android:layout_width="0dp" android:layout_height="2dp" android:layout_weight="1" android:background="#CCCCCC" /> <View android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:id="@+id/show_four" android:layout_width="0dp" android:layout_height="2dp" android:layout_weight="1" android:background="#CCCCCC" /> </LinearLayout> <android.support.v4.view.ViewPager android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/drugVp" > </android.support.v4.view.ViewPager> </LinearLayout> </RelativeLayout>下面的线是通过view实现的,长度是根据weight控制的。
MAinActivity中代码:
package com.example.dingbutab; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener { oneFragment allDrugFragment; twoFragment qiXieFragment; threeFragment baoJianFragment; fourFragment wuTangFragment; List<Fragment> fragmentList; ViewPager viewPager; MyfragmentAdpter myfragmentAdpter; private View showOne,showTwo,showThree,showFour; private TextView bt_drug_one1,bt_drug_two2,bt_drug_three3,bt_drug_four4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.drugVp); fragmentList= new ArrayList<>(); allDrugFragment=new oneFragment(); qiXieFragment=new twoFragment(); baoJianFragment=new threeFragment(); wuTangFragment=new fourFragment(); fragmentList.add(allDrugFragment); fragmentList.add(qiXieFragment); fragmentList.add(baoJianFragment); fragmentList.add(wuTangFragment); myfragmentAdpter=new MyfragmentAdpter(getSupportFragmentManager(),fragmentList); viewPager.setAdapter(myfragmentAdpter); viewPager.setOffscreenPageLimit(3); viewPager.addOnPageChangeListener(this); myfragmentAdpter.notifyDataSetChanged(); } public void onClick(View view){ switch (view.getId()) { case R.id.bt_drug_one1: changview(0); break; case R.id.bt_drug_two2: changview(1); break; case R.id.bt_drug_three3: changview(2); break; case R.id.bt_drug_four4: changview(3); break; default: break; } } private void changview(int page){ viewPager.setCurrentItem(page, true); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { showOne=findViewById(R.id.show_one); showTwo=findViewById(R.id.show_two); showThree=findViewById(R.id.show_three); showFour=findViewById(R.id.show_four); bt_drug_four4=(TextView)findViewById(R.id.bt_drug_four4); bt_drug_three3=(TextView)findViewById(R.id.bt_drug_three3); bt_drug_two2=(TextView)findViewById(R.id.bt_drug_two2); bt_drug_one1=(TextView)findViewById(R.id.bt_drug_one1); switch (position){ case 0: showOne.setBackgroundColor(getResources().getColor(R.color.tv_Red)); showTwo.setBackgroundColor(getResources().getColor(R.color.bg_Gray)); showThree.setBackgroundColor(getResources().getColor(R.color.bg_Gray)); showFour.setBackgroundColor(getResources().getColor(R.color.bg_Gray)); bt_drug_one1.setTextColor(getResources().getColor(R.color.tv_Red)); bt_drug_two2.setTextColor(getResources().getColor(R.color.shopping_druglist)); bt_drug_three3.setTextColor(getResources().getColor(R.color.shopping_druglist)); bt_drug_four4.setTextColor(getResources().getColor(R.color.shopping_druglist)); break; case 1: showOne.setBackgroundColor(getResources().getColor(R.color.bg_Gray)); showTwo.setBackgroundColor(getResources().getColor(R.color.tv_Red)); showThree.setBackgroundColor(getResources().getColor(R.color.bg_Gray)); showFour.setBackgroundColor(getResources().getColor(R.color.bg_Gray)); bt_drug_two2.setTextColor(getResources().getColor(R.color.tv_Red)); bt_drug_one1.setTextColor(getResources().getColor(R.color.shopping_druglist)); bt_drug_three3.setTextColor(getResources().getColor(R.color.shopping_druglist)); bt_drug_four4.setTextColor(getResources().getColor(R.color.shopping_druglist)); break; case 2: showOne.setBackgroundColor(getResources().getColor(R.color.bg_Gray)); showTwo.setBackgroundColor(getResources().getColor(R.color.bg_Gray)); showThree.setBackgroundColor(getResources().getColor(R.color.tv_Red)); showFour.setBackgroundColor(getResources().getColor(R.color.bg_Gray)); bt_drug_three3.setTextColor(getResources().getColor(R.color.tv_Red)); bt_drug_one1.setTextColor(getResources().getColor(R.color.shopping_druglist)); bt_drug_two2.setTextColor(getResources().getColor(R.color.shopping_druglist)); bt_drug_four4.setTextColor(getResources().getColor(R.color.shopping_druglist)); break; case 3: showOne.setBackgroundColor(getResources().getColor(R.color.bg_Gray)); showTwo.setBackgroundColor(getResources().getColor(R.color.bg_Gray)); showThree.setBackgroundColor(getResources().getColor(R.color.bg_Gray)); showFour.setBackgroundColor(getResources().getColor(R.color.tv_Red)); bt_drug_four4.setTextColor(getResources().getColor(R.color.tv_Red)); bt_drug_one1.setTextColor(getResources().getColor(R.color.shopping_druglist)); bt_drug_two2.setTextColor(getResources().getColor(R.color.shopping_druglist)); bt_drug_three3.setTextColor(getResources().getColor(R.color.shopping_druglist)); break; } } @Override public void onPageScrollStateChanged(int state) { } }oneFragment代码如下,其他几个fragment就不全部列出:
package com.example.dingbutab; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * 全部菜单列表 * Created by Administrator on 2016/2/24 0024. */ public class oneFragment extends Fragment { View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view=inflater.inflate(R.layout.one_fragment, container, false); return view; } }one_fragment.xml:
<?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"> <TextView android:layout_margin="10dp" android:textSize="25dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="one" android:background="@color/colorPrimary"/> </LinearLayout>fragment的适配器MyFragmentAdapter:
package com.example.dingbutab; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import java.util.List; public class MyfragmentAdpter extends FragmentStatePagerAdapter { private List<Fragment> fragmentList; public MyfragmentAdpter(FragmentManager fragmentManager, List<Fragment> fragmentList1){ super(fragmentManager); this.fragmentList=fragmentList1; } public Fragment getItem(int position){ return fragmentList.get(position); } public int getCount(){ return fragmentList.size(); }}其中颜色值和字体根据自己需要自行设置。
功能实现就是这样,有问题欢迎留言,大家一起进步。