Skip to content

Commit

Permalink
Issue #5 底部导航通过RadioGroup管理check状态,简化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
tiansj committed Aug 26, 2015
1 parent ca37d2a commit 2cd3e5a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 132 deletions.
169 changes: 37 additions & 132 deletions app/src/main/java/com/yuzhi/fine/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,34 @@
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.yuzhi.fine.R;
import com.yuzhi.fine.fragment.BufferKnifeFragment;
import com.yuzhi.fine.fragment.MainPagerFragment;
import com.yuzhi.fine.fragment.MemberFragment;
import com.yuzhi.fine.ui.UIHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends BaseFragmentActivity {

public static final int SCROLL_VIEW_HOME = 0;
public static final int SCROLL_VIEW_IM = 1;
public static final int SCROLL_VIEW_INTEREST = 2;
public static final int SCROLL_VIEW_USER = 3;

public static int mCurSel = -1;

private RadioButton fbHome;
private RadioButton fbIm;
private RadioButton fbInterest;
private RadioButton fbUser;
private RadioGroup group;

private Fragment homeFragment = new MainPagerFragment();
private Fragment imFragment = new BufferKnifeFragment();
private Fragment interestFragment = new BufferKnifeFragment();
private Fragment memberFragment = new MemberFragment();
private List<Fragment> fragmentList;
private List<Fragment> fragmentList = Arrays.asList(homeFragment, imFragment, interestFragment, memberFragment);

private FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);

fragmentManager = getSupportFragmentManager();

mCurSel = -1;
initFragmentList();
initFootBar();
}

Expand All @@ -61,139 +46,59 @@ protected void onResume() {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment fragment = fragmentList.get(mCurSel);
int checkedId = group.getCheckedRadioButtonId();
int cur = 0;
switch (checkedId) {
case R.id.foot_bar_home: cur = 0; break;
case R.id.foot_bar_im: cur = 1; break;
case R.id.foot_bar_interest: cur = 2; break;
case R.id.main_footbar_user: cur = 3; break;
}
Fragment fragment = fragmentList.get(cur);
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}

private void initFragmentList() {
fragmentList = new ArrayList<Fragment>();
fragmentList.add(homeFragment);
fragmentList.add(imFragment);
fragmentList.add(interestFragment);
fragmentList.add(memberFragment);
}

private void initFootBar() {
fbHome = (RadioButton) findViewById(R.id.foot_bar_home);
fbIm = (RadioButton) findViewById(R.id.foot_bar_im);
fbInterest = (RadioButton) findViewById(R.id.foot_bar_interest);
fbUser = (RadioButton) findViewById(R.id.main_footbar_user);

fbHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setCurPoint(SCROLL_VIEW_HOME);
}
});

fbIm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setCurPoint(SCROLL_VIEW_IM);
}
});

fbInterest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setCurPoint(SCROLL_VIEW_INTEREST);
}
});

fbUser.setOnClickListener(new View.OnClickListener() {
group = (RadioGroup) findViewById(R.id.group);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onClick(View v) {
UIHelper.showLogin(MainActivity.this);
setCurPoint(SCROLL_VIEW_USER);
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.foot_bar_home: addFragmentToStack(0);
break;
case R.id.foot_bar_im:
addFragmentToStack(1);
break;
case R.id.foot_bar_interest:
addFragmentToStack(2);
break;
case R.id.main_footbar_user:
addFragmentToStack(3);
UIHelper.showLogin(MainActivity.this);
break;
}
}
});

fbHome.performClick();
}

public void setCurPoint(int index) {
if (mCurSel == index)
return;
mCurSel = index;
addFragmentToStack();
setFootBtnChecked();
}

private void setFootBtnChecked() {
fbHome.setChecked(mCurSel == SCROLL_VIEW_HOME);
fbIm.setChecked(mCurSel == SCROLL_VIEW_IM);
fbInterest.setChecked(mCurSel == SCROLL_VIEW_INTEREST);
fbUser.setChecked(mCurSel == SCROLL_VIEW_USER);
addFragmentToStack(0);
}

private void addFragmentToStack() {
private void addFragmentToStack(int cur) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (mCurSel == SCROLL_VIEW_HOME) {
if (!homeFragment.isAdded()) {
fragmentTransaction.add(R.id.fragment_container, homeFragment);
}
} else if (mCurSel == SCROLL_VIEW_IM) {
if (!imFragment.isAdded()) {
fragmentTransaction.add(R.id.fragment_container, imFragment);
}
} else if (mCurSel == SCROLL_VIEW_INTEREST) {
if (!interestFragment.isAdded()) {
fragmentTransaction.add(R.id.fragment_container, interestFragment);
}
} else if (mCurSel == SCROLL_VIEW_USER) {
if (!memberFragment.isAdded()) {
fragmentTransaction.add(R.id.fragment_container, memberFragment);
}
Fragment fragment = fragmentList.get(cur);
if (!fragment.isAdded()) {
fragmentTransaction.add(R.id.fragment_container, fragment);
}
toggleFragment(fragmentTransaction);
fragmentTransaction.commitAllowingStateLoss();
}

private void toggleFragment(FragmentTransaction fragmentTransaction) {
for (int i = 0; i < fragmentList.size(); i++) {
Fragment f = fragmentList.get(i);
if (i == mCurSel && f.isAdded()) {
if (i == cur && f.isAdded()) {
fragmentTransaction.show(f);
} else if (f != null && f.isAdded() && f.isVisible()) {
fragmentTransaction.hide(f);
}
}
}

public void switchFragment(int index) {
if (index == SCROLL_VIEW_HOME) {
fbHome.performClick();
fbHome.post(new Runnable() {
@Override
public void run() {
fbHome.performClick();
}
});
} else if (index == SCROLL_VIEW_IM) {
fbIm.post(
new Runnable() {
@Override
public void run() {
fbIm.performClick();
}
});
} else if (index == SCROLL_VIEW_INTEREST) {
fbInterest.post(new Runnable() {
@Override
public void run() {
fbInterest.performClick();
}
});
} else if (index == SCROLL_VIEW_USER) {
fbUser.post(new Runnable() {
@Override
public void run() {
fbUser.performClick();
}
});
}
fragmentTransaction.commitAllowingStateLoss();
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/yuzhi/fine/fragment/MemberFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.view.ViewGroup;

import com.yuzhi.fine.R;
import com.yuzhi.fine.ui.UIHelper;
import com.yuzhi.fine.ui.pulltozoomview.PullToZoomScrollViewEx;

public class MemberFragment extends Fragment {
Expand Down Expand Up @@ -39,7 +40,19 @@ void initView() {
scrollView.setZoomView(zoomView);
scrollView.setScrollContentView(contentView);

headView.findViewById(R.id.tv_register).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UIHelper.showLogin(getActivity());
}
});

headView.findViewById(R.id.tv_login).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UIHelper.showLogin(getActivity());
}
});


scrollView.getPullRootView().findViewById(R.id.tv_test1).setOnClickListener(new View.OnClickListener() {
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/main_footer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
android:gravity="center_vertical"
android:orientation="horizontal">

<RadioGroup
android:id="@+id/group"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">

<RadioButton
android:id="@+id/foot_bar_home"
style="@style/main_footbar_radio"
Expand All @@ -32,4 +38,5 @@
android:text="@string/main_navigation_user"
android:drawableTop="@drawable/widget_bar_user_bg"/>

</RadioGroup>
</LinearLayout>

0 comments on commit 2cd3e5a

Please sign in to comment.