Skip to content

Commit

Permalink
修改demo演示,添加动态添加和移除条目;
Browse files Browse the repository at this point in the history
  • Loading branch information
ccw committed Dec 13, 2018
1 parent bc65ace commit fbe5a5c
Show file tree
Hide file tree
Showing 19 changed files with 648 additions and 181 deletions.
8 changes: 3 additions & 5 deletions demo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
</intent-filter>
</activity>

<activity android:name=".ViewPagerActivity">
</activity>

<activity android:name=".FragmentManagerActivity">
</activity>
<activity android:name=".ViewPagerActivity"/>
<activity android:name=".FragmentManagerActivity"/>
<activity android:name=".DynamicAddItemActivity"/>
</application>

</manifest>
16 changes: 16 additions & 0 deletions demo/src/main/java/com/chaychan/bottombarlayout/DemoBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.chaychan.bottombarlayout;

public class DemoBean {
public String name;
public Class<?> clazz;

public DemoBean(String name, Class<?> clazz) {
this.name = name;
this.clazz = clazz;
}

@Override
public String toString() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package com.chaychan.bottombarlayout;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
* @author ChayChan
* @description: 动态添加条目
* @date 2018/12/13 14:45
*/
public class DynamicAddItemActivity extends AppCompatActivity {

private List<TabFragment> mFragmentList = new ArrayList<>();
private FrameLayout mFlContent;
private BottomBarLayout mBottomBarLayout;

private int[] mNormalIconIds = new int[]{
R.mipmap.tab_home_normal, R.mipmap.tab_video_normal,
R.mipmap.tab_micro_normal, R.mipmap.tab_me_normal
};

private int[] mSelectedIconIds = new int[]{
R.mipmap.tab_home_selected, R.mipmap.tab_video_selected,
R.mipmap.tab_micro_selected, R.mipmap.tab_me_selected
};

private int[] mTitleIds = new int[]{
R.string.tab_home,
R.string.tab_video,
R.string.tab_micro,
R.string.tab_me
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic_add_item);

initView();
initData();
initListener();
}

private void initView() {
mFlContent = (FrameLayout) findViewById(R.id.fl_content);
mBottomBarLayout = (BottomBarLayout) findViewById(R.id.bbl);
}

private void initData() {
for (int i = 0; i < mTitleIds.length; i++) {
//创建item
BottomBarItem item = createBottomBarItem(i);
mBottomBarLayout.addItem(item);

TabFragment homeFragment = createFragment(mTitleIds[i]);
mFragmentList.add(homeFragment);
}

changeFragment(0); //默认显示第一页
}

@NonNull
private TabFragment createFragment(int titleId) {
TabFragment homeFragment = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString(TabFragment.CONTENT, getString(titleId));
homeFragment.setArguments(bundle);
return homeFragment;
}

private BottomBarItem createBottomBarItem(int i) {
BottomBarItem item = new BottomBarItem.Builder(this)
.titleTextSize(8)
.titleNormalColor(R.color.tab_normal_color)
.titleSelectedColor(R.color.tab_selected_color)
// .openTouchBg(false)
// .marginTop(5)
// .itemPadding(5)
// .unreadNumThreshold(99)
// .unreadTextColor(R.color.white)

//还有很多属性,详情请查看Builder里面的方法
//There are still many properties, please see the methods in the Builder for details.
.create(mNormalIconIds[i], mSelectedIconIds[i], getString(mTitleIds[i]));
return item;
}

private void initListener() {
mBottomBarLayout.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
@Override
public void onItemSelected(final BottomBarItem bottomBarItem, int previousPosition, final int currentPosition) {
Log.i("MainActivity", "position: " + currentPosition);

changeFragment(currentPosition);
}
});
}

private void changeFragment(int currentPosition) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fl_content, mFragmentList.get(currentPosition));
transaction.commit();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_add_item:
int random = new Random().nextInt(3);
//addFragment
mFragmentList.add(createFragment(mTitleIds[random]));
//addItem
BottomBarItem bottomBarItem = createBottomBarItem(random);
mBottomBarLayout.addItem(bottomBarItem);

mBottomBarLayout.setCurrentItem(mFragmentList.size() - 1);
break;
case R.id.action_remove_item:
//移除条目
mBottomBarLayout.removeItem(0);

if (mFragmentList.size() != 0){
mFragmentList.remove(0);

if (mFragmentList.size() != 0){
mBottomBarLayout.setCurrentItem(0);
}
}
break;
}
return super.onOptionsItemSelected(item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void onItemSelected(final BottomBarItem bottomBarItem, int previousPositi
//如果是第一个,即首页
if (previousPosition == currentPosition) {
//如果是在原来位置上点击,更换首页图标并播放旋转动画
bottomBarItem.setIconSelectedResourceId(R.mipmap.tab_loading);//更换成加载图标
bottomBarItem.setSelectedIconResourceId(R.mipmap.tab_loading);//更换成加载图标
bottomBarItem.setStatus(true);

//播放旋转动画
Expand All @@ -100,7 +100,7 @@ public void onItemSelected(final BottomBarItem bottomBarItem, int previousPositi
@Override
public void run() {
boolean tabNotChanged = mBottomBarLayout.getCurrentItem() == currentPosition; //是否还停留在当前页签
bottomBarItem.setIconSelectedResourceId(R.mipmap.tab_home_selected);//更换成首页原来选中图标
bottomBarItem.setSelectedIconResourceId(R.mipmap.tab_home_selected);//更换成首页原来选中图标
bottomBarItem.setStatus(tabNotChanged);//刷新图标
cancelTabLoading(bottomBarItem);
}
Expand All @@ -111,7 +111,7 @@ public void run() {

//如果点击了其他条目
BottomBarItem bottomItem = mBottomBarLayout.getBottomItem(0);
bottomItem.setIconSelectedResourceId(R.mipmap.tab_home_selected);//更换为原来的图标
bottomItem.setSelectedIconResourceId(R.mipmap.tab_home_selected);//更换为原来的图标
cancelTabLoading(bottomItem);//停止旋转动画
}
});
Expand Down
27 changes: 19 additions & 8 deletions demo/src/main/java/com/chaychan/bottombarlayout/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
package com.chaychan.bottombarlayout;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends ListActivity {

private DemoBean[] mDatas= {
new DemoBean("UseWithViewPager",ViewPagerActivity.class),
new DemoBean("UseWithoutViewPager",FragmentManagerActivity.class),
new DemoBean("DynamicAddItem",DynamicAddItemActivity.class),

};

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

public void useVp(View view){
startActivity(new Intent(this,ViewPagerActivity.class));
ArrayAdapter<DemoBean> adapter = new ArrayAdapter<DemoBean>(this, android.R.layout.simple_list_item_1, mDatas);
setListAdapter(adapter);
}

public void noUseVp(View view){
startActivity(new Intent(this,FragmentManagerActivity.class));
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//为条目设置点击事件
DemoBean dataBean = mDatas[position];
startActivity(new Intent(this,dataBean.clazz));//跳转到对应的activity
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
Expand All @@ -18,7 +18,7 @@
import java.util.ArrayList;
import java.util.List;

public class ViewPagerActivity extends FragmentActivity {
public class ViewPagerActivity extends AppCompatActivity {

private ViewPager mVpContent;
private BottomBarLayout mBottomBarLayout;
Expand Down Expand Up @@ -80,7 +80,7 @@ public void onItemSelected(final BottomBarItem bottomBarItem, int previousPositi
//如果是第一个,即首页
if (previousPosition == currentPosition){
//如果是在原来位置上点击,更换首页图标并播放旋转动画
bottomBarItem.setIconSelectedResourceId(R.mipmap.tab_loading);//更换成加载图标
bottomBarItem.setSelectedIconResourceId(R.mipmap.tab_loading);//更换成加载图标
bottomBarItem.setStatus(true);

//播放旋转动画
Expand All @@ -100,7 +100,7 @@ public void onItemSelected(final BottomBarItem bottomBarItem, int previousPositi
@Override
public void run() {
boolean tabNotChanged = mBottomBarLayout.getCurrentItem() == currentPosition; //是否还停留在当前页签
bottomBarItem.setIconSelectedResourceId(R.mipmap.tab_home_selected);//更换成首页原来选中图标
bottomBarItem.setSelectedIconResourceId(R.mipmap.tab_home_selected);//更换成首页原来选中图标
bottomBarItem.setStatus(tabNotChanged);//刷新图标
cancelTabLoading(bottomBarItem);
}
Expand All @@ -111,7 +111,7 @@ public void run() {

//如果点击了其他条目
BottomBarItem bottomItem = mBottomBarLayout.getBottomItem(0);
bottomItem.setIconSelectedResourceId(R.mipmap.tab_home_selected);//更换为原来的图标
bottomItem.setSelectedIconResourceId(R.mipmap.tab_home_selected);//更换为原来的图标

cancelTabLoading(bottomItem);//停止旋转动画
}
Expand Down
25 changes: 25 additions & 0 deletions demo/src/main/res/layout/activity_dynamic_add_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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">

<FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>

<com.chaychan.library.BottomBarLayout
android:id="@+id/bbl"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center"
android:background="@color/tab_gb"
/>

</LinearLayout>
8 changes: 4 additions & 4 deletions demo/src/main/res/layout/activity_fragment_manager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_home_normal"
app:iconSelected="@mipmap/tab_home_selected"
app:itemText="首页"
app:itemText="@string/tab_home"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
Expand All @@ -45,7 +45,7 @@
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_video_normal"
app:iconSelected="@mipmap/tab_video_selected"
app:itemText="视频"
app:itemText="@string/tab_video"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
Expand All @@ -62,7 +62,7 @@
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_micro_normal"
app:iconSelected="@mipmap/tab_micro_selected"
app:itemText="微头条"
app:itemText="@string/tab_micro"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
Expand All @@ -77,7 +77,7 @@
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_me_normal"
app:iconSelected="@mipmap/tab_me_selected"
app:itemText="我的"
app:itemText="@string/tab_me"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
Expand Down
30 changes: 0 additions & 30 deletions demo/src/main/res/layout/activity_main.xml

This file was deleted.

Loading

0 comments on commit fbe5a5c

Please sign in to comment.