activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <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.mrzhao.examdemo2.MainActivity"> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></FrameLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray"></View> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/home_bt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_weight="1" android:button="@null" android:drawableTop="@android:drawable/ic_dialog_info" android:gravity="center" android:text="首页" android:textSize="20sp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="@android:color/darker_gray"></View> <RadioButton android:id="@+id/mine_bt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_weight="1" android:button="@null" android:drawableTop="@android:drawable/ic_dialog_email" android:gravity="center" android:text="我的" android:textSize="20sp" /> </RadioGroup> </LinearLayout>
fragment_home_layout.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" android:background="@color/colorPrimary" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="200dp"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="200dp" android:background="@android:color/holo_purple"></android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/indicator_layout" android:layout_width="match_parent" android:layout_height="20dp" android:layout_alignParentBottom="true" android:gravity="center" android:orientation="horizontal"></LinearLayout> </RelativeLayout> </LinearLayout>
fragment_mine_layout.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"> <LinearLayout android:layout_width="match_parent" android:layout_height="200dp" android:background="@android:color/holo_orange_dark" android:orientation="vertical"> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:layout_gravity="right" android:layout_margin="10dp" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:src="@mipmap/ic_launcher" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray"></View> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="总资产(元)" android:textColor="@android:color/white" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="0.00元" android:textColor="@android:color/white" android:textSize="20sp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_margin="10dp" android:gravity="center" android:text=">" android:textColor="@android:color/white" android:textSize="30sp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="@android:color/darker_gray"></View> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="总资产(元)" android:textColor="@android:color/white" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="0.00元" android:textColor="@android:color/white" android:textSize="20sp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_margin="10dp" android:gravity="center" android:text=">" android:textColor="@android:color/white" android:textSize="30sp" /> </LinearLayout> </LinearLayout> <Button android:id="@+id/dialog_bt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_margin="10dp" android:background="@android:color/holo_orange_dark" android:text="评一评" /> </RelativeLayout>
dialog_layout.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" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="200dp" android:background="@android:color/holo_blue_light"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:gravity="center" android:text="评价一下吧!" android:textColor="@android:color/white" android:textSize="30sp" android:textStyle="bold" /> <ImageView android:id="@+id/close_bt" android:layout_width="20dp" android:layout_height="20dp" android:layout_alignParentRight="true" android:src="@mipmap/ic_launcher" /> </RelativeLayout> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center" android:padding="20dp" android:text="喜欢吗?给个五行好评?鼓励我们做的更好!" android:textSize="20sp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray"></View> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:id="@+id/loser_tv" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="我要吐槽" android:textSize="20sp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="@android:color/darker_gray"></View> <TextView android:id="@+id/winner_tv" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="五星好评" android:textColor="@android:color/holo_blue_light" android:textSize="20sp" /> </LinearLayout> </LinearLayout>
适配器:
public class MyAdapter extends PagerAdapter { private List<ImageView> list; public MyAdapter(List<ImageView> list) { this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); return list.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // super.destroyItem(container, position, object); container.removeView(list.get(position)); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } }
HomeFragment:
public class HomeFragment extends Fragment { private ViewPager viewPager; private LinearLayout indicatorLy; private List<ImageView> list = new ArrayList<>(); private int[] resources = {R.mipmap.aaa, R.mipmap.bbb, R.mipmap.ccc, R.mipmap.ddd, R.mipmap.eee, R.mipmap.fff, R.mipmap.ggg}; private MyAdapter myAdapter; private List<View> indicators = new ArrayList<>(); private int pre = 0; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_home_layout, container, false); viewPager = ((ViewPager) view.findViewById(R.id.viewPager)); indicatorLy = ((LinearLayout) view.findViewById(R.id.indicator_layout)); initViewpager(); return view; } //初始化ViewPager private void initViewpager() { for (int i = 0; i < resources.length; i++) { ImageView view = new ImageView(getActivity()); view.setImageResource(resources[i]); list.add(view); View indicator = new View(getActivity()); indicator.setBackgroundColor(Color.WHITE); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(10, 10); lp.leftMargin = 10; indicator.setLayoutParams(lp); indicators.add(indicator); indicatorLy.addView(indicator); } indicators.get(0).setBackgroundColor(Color.BLACK); myAdapter = new MyAdapter(list); viewPager.setAdapter(myAdapter); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { indicators.get(pre).setBackgroundColor(Color.WHITE); indicators.get(position).setBackgroundColor(Color.BLACK); pre = position; } @Override public void onPageScrollStateChanged(int state) { } }); } }
MineFragment:
public class MineFragment extends Fragment implements View.OnClickListener{ private AlertDialog dialog; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_mine_layout, container, false); view.findViewById(R.id.dialog_bt).setOnClickListener(this); return view; } @Override public void onClick(View v) { switch (v.getId()){ case R.id.dialog_bt: initDialog(); break; case R.id.close_bt: Toast.makeText(getActivity(), "关闭", Toast.LENGTH_SHORT).show(); dialog.dismiss(); break; case R.id.winner_tv: Toast.makeText(getActivity(), "五行好评", Toast.LENGTH_SHORT).show(); dialog.dismiss(); break; case R.id.loser_tv: Toast.makeText(getActivity(), "我要吐槽", Toast.LENGTH_SHORT).show(); dialog.dismiss(); break; } } //初始化Dialog弹出框 public void initDialog(){ AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_layout, null); view.findViewById(R.id.close_bt).setOnClickListener(this); view.findViewById(R.id.loser_tv).setOnClickListener(this); view.findViewById(R.id.winner_tv).setOnClickListener(this); builder.setView(view); dialog = builder.show(); } }
MainActivity:
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { private HomeFragment homeFragment; private MineFragment mineFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((RadioGroup) findViewById(R.id.radioGroup)).setOnCheckedChangeListener(this); homeFragment = new HomeFragment(); mineFragment = new MineFragment(); //初始化的事儿 getSupportFragmentManager() .beginTransaction() .add(R.id.container, homeFragment) .add(R.id.container, mineFragment) .hide(mineFragment) .commit(); } @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); switch (checkedId) { case R.id.home_bt: //首页 transaction.show(homeFragment).hide(mineFragment).commit(); break; case R.id.mine_bt: //我的 transaction.show(mineFragment).hide(homeFragment).commit(); break; } } }