|
|
|
@ -1,13 +1,17 @@
|
|
|
|
|
package com.example.leudaemialikeme;
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
|
import androidx.fragment.app.FragmentManager;
|
|
|
|
|
import androidx.fragment.app.FragmentTransaction;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
@ -28,11 +32,118 @@ public class MainActivity extends AppCompatActivity {
|
|
|
|
|
private TextView textCommunity;
|
|
|
|
|
private TextView textMessage;
|
|
|
|
|
private TextView textMy;
|
|
|
|
|
//底部导航点击事件监听器
|
|
|
|
|
private View.OnClickListener onClickListener = new View.OnClickListener() {
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
//先将四个 ImageButton 置为灰色
|
|
|
|
|
resetImgs();
|
|
|
|
|
//根据点击的 Tab 切换不同的页面及设置对应的 ImageButton 为绿色
|
|
|
|
|
switch (v.getId()) {
|
|
|
|
|
case R.id.tab_home:
|
|
|
|
|
selectTab(0);
|
|
|
|
|
break;
|
|
|
|
|
case R.id.tab_community:
|
|
|
|
|
selectTab(1);
|
|
|
|
|
break;
|
|
|
|
|
case R.id.tab_message:
|
|
|
|
|
selectTab(2);
|
|
|
|
|
break;
|
|
|
|
|
case R.id.tab_my:
|
|
|
|
|
selectTab(3);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
getSupportActionBar().hide();
|
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
initFragments(); //初始化数据
|
|
|
|
|
initViews(); //初始化控件
|
|
|
|
|
initEvents(); //初始化事件
|
|
|
|
|
initFirstRun(0);//第一次运行初始化界面,第一个碎片
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initFirstRun(int i) {
|
|
|
|
|
resetImgs(); //重置所有 Tab
|
|
|
|
|
selectTab(i); //显示第 i 个碎片
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void selectTab(int i) {
|
|
|
|
|
//根据点击的 Tab 设置对应的 ImageButton 为绿色
|
|
|
|
|
switch (i) {
|
|
|
|
|
case 0:
|
|
|
|
|
imgHome.setImageResource(R.mipmap.img_home_pressed);
|
|
|
|
|
textHome.setTextColor(getResources().getColor(R.color.hair_grey));
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
imgCommunity.setImageResource(R.mipmap.img_community_pressed);
|
|
|
|
|
textCommunity.setTextColor(getResources().getColor(R.color.hair_grey));
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
imgMessage.setImageResource(R.mipmap.img_message_pressed);
|
|
|
|
|
textMessage.setTextColor(getResources().getColor(R.color.hair_grey));
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
imgMy.setImageResource(R.mipmap.img_my_pressed);
|
|
|
|
|
textMy.setTextColor(getResources().getColor(R.color.hair_grey));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
//设置当前点击的 Tab 所对应的页面
|
|
|
|
|
setCurrentFragment(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setCurrentFragment(int i) {
|
|
|
|
|
FragmentManager fm = getSupportFragmentManager();
|
|
|
|
|
FragmentTransaction trans = fm.beginTransaction();
|
|
|
|
|
trans.replace(R.id.frag_layout, fragmentList.get(i));
|
|
|
|
|
trans.commit();
|
|
|
|
|
//Log.e("fragment", "ok");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void resetImgs() {
|
|
|
|
|
imgHome.setImageResource(R.mipmap.img_home_normal);
|
|
|
|
|
imgCommunity.setImageResource(R.mipmap.img_community_normal);
|
|
|
|
|
imgMessage.setImageResource(R.mipmap.img_message_normal);
|
|
|
|
|
imgMy.setImageResource(R.mipmap.img_my_normal);
|
|
|
|
|
textHome.setTextColor(getResources().getColor(R.color.light_grey));
|
|
|
|
|
textCommunity.setTextColor(getResources().getColor(R.color.light_grey));
|
|
|
|
|
textMessage.setTextColor(getResources().getColor(R.color.light_grey));
|
|
|
|
|
textMy.setTextColor(getResources().getColor(R.color.light_grey));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initEvents() {
|
|
|
|
|
tabHome.setOnClickListener(onClickListener);
|
|
|
|
|
tabCommunity.setOnClickListener(onClickListener);
|
|
|
|
|
tabMessage.setOnClickListener(onClickListener);
|
|
|
|
|
tabMy.setOnClickListener(onClickListener);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initViews() {
|
|
|
|
|
tabHome = (LinearLayout)findViewById(R.id.tab_home);
|
|
|
|
|
tabCommunity = (LinearLayout)findViewById(R.id.tab_community);
|
|
|
|
|
tabMessage = (LinearLayout)findViewById(R.id.tab_message);
|
|
|
|
|
tabMy = (LinearLayout)findViewById(R.id.tab_my);
|
|
|
|
|
imgHome = (ImageButton)findViewById(R.id.tab_home_img);
|
|
|
|
|
imgCommunity = (ImageButton)findViewById(R.id.tab_community_img);
|
|
|
|
|
imgMessage = (ImageButton)findViewById(R.id.tab_message_img);
|
|
|
|
|
imgMy = (ImageButton)findViewById(R.id.tab_my_img);
|
|
|
|
|
textHome = (TextView)findViewById(R.id.tab_home_text);
|
|
|
|
|
textCommunity = (TextView)findViewById(R.id.tab_community_text);
|
|
|
|
|
textMessage = (TextView)findViewById(R.id.tab_message_text);
|
|
|
|
|
textMy = (TextView)findViewById(R.id.tab_my_text);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initFragments() {
|
|
|
|
|
fragmentList = new ArrayList<Fragment>();
|
|
|
|
|
fragmentList.add(new IndexFragment());
|
|
|
|
|
fragmentList.add(new CommunityFragment());
|
|
|
|
|
fragmentList.add(new MessageFragment());
|
|
|
|
|
fragmentList.add(new MyFragment());
|
|
|
|
|
}
|
|
|
|
|
}
|