Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在TabLayout中使用的Demo #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
compile project(':badgeviewlib')
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
6 changes: 4 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize|stateHidden">
</activity>
<activity android:name=".ListViewActivity"></activity>
<activity android:name=".RecyclerViewActivity"></activity>
<activity android:name=".TabLayoutActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ListViewActivity"></activity>
<activity android:name=".RecyclerViewActivity"></activity>
</application>

</manifest>
23 changes: 23 additions & 0 deletions app/src/main/java/q/rorbin/badgeviewdemo/MyFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package q.rorbin.badgeviewdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* 作者:warm
* 时间:2017-12-21 13:59
* 描述:
*/

public class MyFragment extends Fragment{

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_my,container,false);
}
}
111 changes: 111 additions & 0 deletions app/src/main/java/q/rorbin/badgeviewdemo/TabLayoutActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package q.rorbin.badgeviewdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
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.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

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

import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

/**
* 作者:warm
* 时间:2017-12-21 13:39
* 描述:
*/

public class TabLayoutActivity extends AppCompatActivity implements View.OnClickListener {

private TabLayout tl;
private ViewPager vp;
private Button bt;
private Button btHide;
List<Badge> badges;
private String[] titles = {"标题一", "标题二", "标题三", "标题四", "标题五"};


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tablayout);
tl = (TabLayout) findViewById(R.id.tl);
vp = (ViewPager) findViewById(R.id.vp);
bt = (Button) findViewById(R.id.bt);
btHide= (Button) findViewById(R.id.bt_hide);
bt.setOnClickListener(this);
btHide.setOnClickListener(this);
vp.setAdapter(new VpAdapter(getSupportFragmentManager()));
tl.setupWithViewPager(vp);
badges = new ArrayList<>();

for (int i = 0; i < titles.length; i++) {

Badge badge = new QBadgeView(tl.getContext())
.bindTarget(((ViewGroup) tl.getChildAt(0)).getChildAt(i))
.setBadgeGravity(Gravity.TOP | Gravity.END)
.setBadgeTextSize(12, true)
.setBadgePadding(3, true)
.setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
@Override
public void onDragStateChanged(int dragState, Badge badge, View targetView) {

}
});
badge.setBadgeNumber(1);
badges.add(badge);
}
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt:
int i = badges.get(tl.getSelectedTabPosition()).getBadgeNumber();
badges.get(tl.getSelectedTabPosition()).setBadgeNumber(++i);
break;
case R.id.bt_hide:
for ( Badge badge :badges) {
badge.hide(true);
}
break;
}

}


class VpAdapter extends FragmentStatePagerAdapter {


public VpAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return new MyFragment();
}

@Override
public int getCount() {
return titles.length;
}

@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}


}
34 changes: 34 additions & 0 deletions app/src/main/res/layout/activity_tablayout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.TabLayout
android:id="@+id/tl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable">

</android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click" />

<Button
android:id="@+id/bt_hide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hide" />

</LinearLayout>
8 changes: 8 additions & 0 deletions app/src/main/res/layout/fragment_my.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">



</LinearLayout>