首先是几个包是必须的
base ui(activity fragment) view presenter
view中的 SwitchMainViewI
public interface SwitchMainViewI { void swichPages(); void showFragment(FragmentManager fm, Fragment fragment,String str); }
base中的 BaseActivity
``public abstract class BaseActivity extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
}
public abstract void initView();
}``
然后创建Fragment类并加上布局
在要实现的类中实现切换的效果
``public class OneActivity extends BaseActivity implements SwitchMainViewI {
private String[] tags={"one","two","three"};
private FragmentManager fm;
private OneFragment f1;
private TwoFragment f2;
private ThreeFragment f3;
private RadioGroup rg;
private RadioButton r1,r2,r3;
@Override
public void initView() {
setContentView(R.layout.activity_one);
rg= (RadioGroup) findViewById(R.id.rg);
r1= (RadioButton) findViewById(R.id.r1);
r2= (RadioButton) findViewById(R.id.r2);
r3= (RadioButton) findViewById(R.id.r3);
swichPages();
showFragment(fm,f1,"one");
}
@Override
public void initData() {
}
@Override
public void swichPages() {
fm=getSupportFragmentManager();
f1=new OneFragment();
f2=new TwoFragment();
f3=new ThreeFragment();
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
clean();
switch (checkedId){
case R.id.r1:
showFragment(fm,f1,"one");
r1.setTextColor(getResources().getColor(R.color.colorAccent));
break;
case R.id.r2:
showFragment(fm,f2,"two");
r2.setTextColor(getResources().getColor(R.color.colorAccent));
break;
case R.id.r3:
showFragment(fm,f3,"three");
r3.setTextColor(getResources().getColor(R.color.colorAccent));
break;
}
}
});
}
@Override
public void showFragment(FragmentManager fm, Fragment fragment, String str) {
FragmentTransaction ft;
ft = fm.beginTransaction();
for (String tag:tags){
if (!tag.equals(str)){
Fragment fragmentTemp = fm.findFragmentByTag(tag);
if (fragmentTemp!=null){
ft.hide(fragmentTemp);
}
}
}
if (fm.findFragmentByTag(str)==null){
ft.add(R.id.mFl, fragment, str);
}
ft.show(fragment);
ft.commit();
}
public void clean(){
r1.setTextColor(getResources().getColor(R.color.colorPrimary));
r2.setTextColor(getResources().getColor(R.color.colorPrimary));
r3.setTextColor(getResources().getColor(R.color.colorPrimary));
}
}``
这是mvp部分首个导航的部分使用3个View来实现的
三个布局分别是三张图片,其中第三张图片加入跳转和倒数的布局
然后在主布局中加入底部的小黑点和上面的ViewPager
``<android.support.v4.view.ViewPager
android:id="@+id/mVp"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp"/>
<LinearLayout
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp">
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@mipmap/dian"
/>
<ImageView
android:id="@+id/iv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@mipmap/undian"
/>
<ImageView
android:id="@+id/iv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@mipmap/undian"
/>
</LinearLayout>``
然后主的activity中的逻辑是
``public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
private ViewPager mVp;
private TextView mTv,mNum;
private boolean isFirstIn=false;
private Intent in;
private ImageView iv1, iv2, iv3;
private View v1, v2, v3;
private List<View> list;
private int number=3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initList();
}
private void initList() {
list = new ArrayList<View>();
list.add(v1);
list.add(v2);
list.add(v3);
MyAdapter adapter = new MyAdapter(list);
mVp.setAdapter(adapter);
mVp.setOnPageChangeListener(MainActivity.this);
mTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// handler.sendEmptyMessage(0);
in=new Intent(MainActivity.this,DengluActivity.class);
startActivity(in);
finish();
}
});
}
private void initView() {
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
mVp=(ViewPager)findViewById(R.id.mVp);
v1 = View.inflate(this, R.layout.item1, null);
v2 = View.inflate(this, R.layout.item2, null);
v3 = View.inflate(this, R.layout.item3, null);
mTv=(TextView) v3.findViewById(R.id.mTv);
mNum= (TextView) v3.findViewById(R.id.mNum);
}
public class MyAdapter extends PagerAdapter {
private List<View> list;
public MyAdapter(List<View> list) {
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(list.get(position));
return list.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView(list.get(position));
}
}
@Override
public void onPageScrollStateChanged(int arg0) {//在状态改变的时候调用
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {//页面在滑动的时候会调用此方法
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int arg0) {//页面跳转完后得到调用
// TODO Auto-generated method stub
clear();
switch (arg0) {
case 0:
iv1.setImageResource(R.mipmap.dian);
break;
case 1:
iv2.setImageResource(R.mipmap.dian);
break;
case 2:
iv3.setImageResource(R.mipmap.dian);
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 3; i < 4; i--) {
Message msg = hd.obtainMessage();
msg.what = 1;
msg.obj = i;
hd.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i == 0) {
Intent in = new Intent(MainActivity.this, DengluActivity.class);
startActivity(in);
finish();
}
}
}
}).start();
break;
}
}
public void clear(){
iv1.setImageResource(R.mipmap.undian);
iv2.setImageResource(R.mipmap.undian);
iv3.setImageResource(R.mipmap.undian);
}
private Handler hd = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
int s = (int) msg.obj;
mNum.setText(s + "");
}
}
};
}``
最后是要实现的登录的部分
``public class DengluActivity extends AppCompatActivity {
private String name="15041193314";
private String code="123456";
private EditText mEt1,mEt2;
private String s1,s2;
private Button mBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_denglu);
inintView();
}
private void inintView() {
mEt1= (EditText) findViewById(R.id.mEt1);
mEt2= (EditText) findViewById(R.id.mEt2);
mBtn= (Button) findViewById(R.id.mBtn);
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
s1=mEt1.getText().toString();
s2=mEt2.getText().toString();
if (s1.equals("")&&s2.equals("")){
Toast.makeText(DengluActivity.this,"信息不能为空",Toast.LENGTH_SHORT).show();
}else if (!s1.equals(name)&&!s2.equals(code)){
Toast.makeText(DengluActivity.this,"请再次确认信息",Toast.LENGTH_SHORT).show();
}else if (s1.equals(name)||s2.equals(code)){
Toast.makeText(DengluActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(DengluActivity.this,OneActivity.class);
startActivity(intent);
finish();
}
}
});
}
}``