我正在尝试使用滚动标签栏项目制作自定义标签栏。
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/main_tabbar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_below="@+id/main_rlTopbar" >
<FrameLayout
android:id="@+id/main_tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@drawable/bottombar" >
<LinearLayout
android:id="@+id/main_llTabs"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
主要活动.java
...
m_scrollTabbar = new HScrollTabbar(this, R.id.main_tabcontent, R.id.main_llTabs);
m_scrollTabbar.addTab(null, 0, HomeActivity.class, true);
...
hscrolltabbar.java
public class HScrollTabbar {
private int m_nTabContentId = -1;
private int m_nScrollTabbarId = -1;
private Activity m_activityParent = null;
private ArrayList<ButtonTabbarItem> m_aryTabButton = new ArrayList<ButtonTabbarItem>();
private int m_nSelectedIndex = -1;
public void addTab(String labelId, int drawableId, Class<?> c) {
LinearLayout llTabs = (LinearLayout) m_activityParent.findViewById(m_nScrollTabbarId);
ButtonTabbarItem item = new ButtonTabbarItem(m_activityParent, c, m_aryTabButton.size());
llTabs.addView(item);
m_aryTabButton.add(item);
if (m_aryTabButton.size() == 1) {
setSelectedIndex(0);
}
item.setOnSelectTabListener(onSelectTabListener);
}
private OnSelectTabListener onSelectTabListener = new OnSelectTabListener() {
@Override
public void onSelect(int nSelectedIndex) {
setSelectedIndex(nSelectedIndex);
}
};
public void setSelectedIndex(int nIndex) {
if (m_nSelectedIndex == nIndex)
return;
ButtonTabbarItem item = null;
if (m_nSelectedIndex > -1) {
item = m_aryTabButton.get(m_nSelectedIndex);
item.setSelected(false);
}
m_nSelectedIndex = nIndex;
item = m_aryTabButton.get(m_nSelectedIndex);
item.setSelected(true);
FrameLayout frame = (FrameLayout) m_activityParent.findViewById(m_nTabContentId);
Intent intent = new Intent();
intent.setClass(frame.getContext(), item.getClassTarget());
frame.getContext().startActivity(intent);
}
public int getSelectedIndex() {
return m_nSelectedIndex;
}
}
buttontabbaritem.java
public class ButtonTabbarItem extends FrameLayout {
private static final int TAB_BTN_TAG = 100;
private Class<?> m_classTarget;
OnSelectTabListener m_listener = null;
public ButtonTabbarItem(Context context, Class<?> classTarget, int nIndex) {
super(context);
LinearLayout layout = new LinearLayout(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.hscroll_tabbar_item, layout);
addView(view);
Button btn = (Button) view.findViewById(R.id.hscroll_tabbar_item_btn);
btn.setOnClickListener(onClickTab);
btn.setTag(TAB_BTN_TAG + nIndex);
m_classTarget = classTarget;
}
public Class<?> getClassTarget() {
return m_classTarget;
}
public void setClassTarget(Class<?> classTarget) {
m_classTarget = classTarget;
}
public void setSelected(boolean bSelected) {
if (bSelected) {
} else {
}
}
private OnClickListener onClickTab = new OnClickListener() {
@Override
public void onClick(View v) {
if (m_listener != null)
m_listener.onSelect((Integer) v.getTag() - TAB_BTN_TAG);
}
};
public interface OnSelectTabListener {
public void onSelect(int nSelectedIndex);
}
public void setOnSelectTabListener(OnSelectTabListener listener) {
m_listener = listener;
}
}
FrameLayout frame = (FrameLayout) m_activityParent.findViewById(m_nTabContentId);
Intent intent = new Intent();
intent.setClass(frame.getContext(), item.getClassTarget());
frame.getContext().startActivity(intent);
我正在尝试在框架内开始活动,但它不起作用。请帮我。