public class MainActivity extends AppCompatActivity {
private ViewPager vpage;
private RadioGroup radio;
private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb3;
private DrawerLayout dawlaout;
private ListView lvs;
private ArrayList<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
dawlaout=(DrawerLayout)findViewById(R.id.drawble);
vpage=(ViewPager)findViewById(R.id.vpage);
lvs=(ListView)findViewById(R.id.lvs);
radio=(RadioGroup)findViewById(R.id.radio);
rb1=(RadioButton)findViewById(R.id.rb1);
rb2=(RadioButton)findViewById(R.id.rb2);
rb3=(RadioButton)findViewById(R.id.rb3);
vpage.setAdapter(new MyAdapter(getSupportFragmentManager()));
lvs.setAdapter(new MyAdapter2(this,list));
vpage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
switch(position){
case 0:
rb1.setTextColor(Color.RED);
rb2.setTextColor(Color.BLACK);
rb3.setTextColor(Color.BLACK);
break;
case 1:
rb2.setTextColor(Color.RED);
rb1.setTextColor(Color.BLACK);
rb3.setTextColor(Color.BLACK);
break;
case 2:
rb3.setTextColor(Color.RED);
rb2.setTextColor(Color.BLACK);
rb1.setTextColor(Color.BLACK);
break;
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (checkedId){
case R.id.rb1:
vpage.setCurrentItem(0);
break;
case R.id.rb2:
vpage.setCurrentItem(1);
break;
case R.id.rb3:
vpage.setCurrentItem(2);
break;
}
}
});
lvs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
vpage.setCurrentItem(0);
break;
case 1:
vpage.setCurrentItem(1);
break;
case 2:
vpage.setCurrentItem(2);
break;
}
dawlaout.closeDrawer(lvs);
}
});
}
private void initData() {
list=new ArrayList<>();
list.add("首页");
list.add("搜索");
list.add("我的");
}
}
===========================================================================
public class Fragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment1, null);
ViewPager vp= (ViewPager) view.findViewById(R.id.vp);
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
vp.setAdapter(new MyAdapter3(getActivity().getSupportFragmentManager()));
//标题和viewpager进行关联
tabs.setViewPager(vp);
return view;
}
}
----------------------------------------------------------------------------------------
import com.example.mran.myapplication.R;
/**
* Created by MrAn on 2017/12/21.
*/
public class Fragment1_1 extends Fragment {
public View onCreateView(String name, Context context, AttributeSet attrs) {
View view = View.inflate(getActivity(), R.layout.fragment1_1,null);
return view;
}
}
-------------------------------------------------------------------------------------------
public class Fragment1_2 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment1_2, null);
return view;
}
}
-----------------------------------------------------------------------------------------
public class Fragment1_3 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment1_3, null);
return view;
}
}
----------------------------------------------------------------------------
public class Fragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getContext(), R.layout.fragment2, null);
return view;
}
}
-------------------------------------------------------------------
public class Fragment3 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getContext(), R.layout.fragment3, null);
return view;
}
}
=======================================================================================
public class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
switch (position){
case 0:
fragment= new Fragment1();
break;
case 1:
fragment=new Fragment2();
break;
case 2:
fragment=new Fragment3();
break;
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
}
-----------------------------------------------------------------------
public class MyAdapter2 extends BaseAdapter {
private List<String> list;
private Context con;
public MyAdapter2(Context con,List<String> list) {
this.con = con;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv=new TextView(con);
tv.setText(list.get(position));
tv.setTextSize(35);
return tv;
}
}
------------------------------------------------------------------------
public class MyAdapter3 extends FragmentPagerAdapter {
private String [] arr ={"页面一","页面二","页面三"};
public MyAdapter3(FragmentManager fm) {
super(fm);
}
/**
*
* @param position
* @return
*/
@Override
public CharSequence getPageTitle(int position) {
return arr[position];
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
switch (position){
case 0:
fragment=new Fragment1_1();
break;
case 1:
fragment=new Fragment1_2();
break;
case 2:
fragment=new Fragment1_3();
break;
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
}
=================================================================================
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawble"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.day16_20171215_1.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/vpage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RadioGroup
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radio"
android:orientation="horizontal">
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="30dp"
android:id="@+id/rb1"
android:text="首页"
android:gravity="center_horizontal"
android:button="@null" />
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="30dp"
android:id="@+id/rb2"
android:text="搜索"
android:gravity="center_horizontal"
android:button="@null" />
<RadioButton
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="30dp"
android:id="@+id/rb3"
android:text="我的"
android:gravity="center_horizontal"
android:button="@null" />
</RadioGroup>
</RelativeLayout>
<ListView
android:background="#ff00ff"
android:layout_gravity="start"
android:id="@+id/lvs"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</android.support.v4.widget.DrawerLayout>
-----------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.astuetz.PagerSlidingTabStrip
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/tabs"/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vp"></android.support.v4.view.ViewPager>
</LinearLayout>
-----------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="搜索"
android:textSize="35dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
---------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="ssee"
android:textSize="35dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="wode"
android:textSize="35dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
-------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试数据2"/>
</LinearLayout>
-------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试数据3"/>
</LinearLayout>