如何使用childs使父类成为一个类 - android

时间:2012-07-23 07:40:48

标签: android

我有一个Android应用程序,其中包含一个超级类,其中包含一个应该对每个活动都是静态的布局,下面的插图以更好的方式显示这个而不是我的描述

enter image description here

此“标题”包含tabBar,其中包含ImageButton。我希望这个标题对我应用中的所有活动都是静态的。我试图做的是extend来自这个超类的其他类。超类的代码在

之下
public class MySuperClass extends Activity {
 MyHorizontalScrollView scrollView;
 View menu;
 View app;
 ImageButton btnSlide;
 boolean menuOut = false;
 Handler handler = new Handler();
 int btnWidth;

 Button testClass; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LayoutInflater inflater = LayoutInflater.from(this);
    scrollView = (MyHorizontalScrollView) inflater.inflate(R.layout.horz_scroll_with_list_menu, null);
    setContentView(scrollView);

    menu = inflater.inflate(R.layout.horz_scroll_menu, null);

    app = inflater.inflate(R.layout.horz_scroll_app, null);
    ViewGroup tabBar = (ViewGroup) app.findViewById(R.id.tabBar);

    ListView listView = (ListView) app.findViewById(R.id.list);

    listView = (ListView) menu.findViewById(R.id.list);

    ArrayList<MenuItem> menuItems = getMenuItems();
    listView.setAdapter(new MenuCustomAdapter(this, menuItems)); 

    btnSlide = (ImageButton) tabBar.findViewById(R.id.BtnSlide);
    btnSlide.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch(event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                btnSlide.setImageResource(R.drawable.lincolor);
                break;
            case MotionEvent.ACTION_UP:
                btnSlide.setImageResource(R.drawable.lin);
                break;
            }

            return false;
        }


    });
    btnSlide.setOnClickListener(new ClickListenerForScrolling(scrollView, menu));

    testClass = (Button) app.findViewById(R.id.button1);

    testClass.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(MySuperClass.this, TestClass.class);
            startActivity(intent); 
        }


    });

    final View[] children = new View[] { menu, app };

    // Scroll to app (view[1]) when layout finished.
    int scrollToViewIdx = 1;
    scrollView.initViews(children, scrollToViewIdx, new SizeCallbackForMenu(btnSlide));
}

public ArrayList<MenuItem> getMenuItems() {
    ArrayList<MenuItem> items = new ArrayList<MenuItem>();


    MenuItem m1 = new MenuItem(R.drawable.scroll, "Show history"); 
    items.add(m1);
    MenuItem m2 = new MenuItem(R.drawable.right, "Right"); 
    items.add(m2);

    return items;
}


/**
 * Helper for examples with a HSV that should be scrolled by a menu View's width.
 */
static class ClickListenerForScrolling implements OnClickListener {
    HorizontalScrollView scrollView;
    View menu;
    ImageButton button;
    int pressed;
    int timeout;
    /**
     * Menu must NOT be out/shown to start with.
     */
    boolean menuOut = false;

    public ClickListenerForScrolling(HorizontalScrollView scrollView, View menu) {
        super();
        this.scrollView = scrollView;
        this.menu = menu; 
    }



    @Override
    public void onClick(View v) {
        Context context = menu.getContext();

        int menuWidth = menu.getMeasuredWidth();

        // Ensure menu is visible
        menu.setVisibility(View.VISIBLE);

        if (!menuOut) {
            // Scroll to 0 to reveal menu
            int left = 0;
            scrollView.smoothScrollTo(left, 0);
        } else {
            // Scroll to menuWidth so menu isn't on screen.
            int left = menuWidth;
            scrollView.smoothScrollTo(left, 0);
        }
        menuOut = !menuOut;
    }


}

/**
 * Helper that remembers the width of the 'slide' button, so that the 'slide' button remains in view, even when the menu is
 * showing.
 */
static class SizeCallbackForMenu implements SizeCallback {
    int btnWidth;
    View btnSlide;

    public SizeCallbackForMenu(View btnSlide) {
        super();
        this.btnSlide = btnSlide;
    }

    @Override
    public void onGlobalLayout() {
        btnWidth = btnSlide.getMeasuredWidth();
        System.out.println("btnWidth=" + btnWidth);
    }

    @Override
    public void getViewSize(int idx, int w, int h, int[] dims) {
        dims[0] = w;
        dims[1] = h;
        final int menuIdx = 0;
        if (idx == menuIdx) {
            dims[0] = w - btnWidth;
        }
    }
}

}

一个测试类,它扩展了这个超类。

public class TestClass extends MySuperClass {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

}

}

我怎样才能为每项活动制作tabBar静态?

3 个答案:

答案 0 :(得分:1)

你可以使用TabHost吧。通过单击每个选项卡,您可以启动单独的活动

http://mfarhan133.wordpress.com/2010/11/17/tablayouttabhost-tutorial-for-android-reusing-layout/

答案 1 :(得分:1)

我不知道这是否可行。我将发布一个替代解决方案,它需要一些更多的代码但是有效,并且是最佳实践,所以如果什么都没有出现,这是最好的选择,可能是唯一的。

当您尝试反复使用某个布局时,就像您提到的标签栏一样,布局中有<merge><include>功能。基本思想是您创建一个要包含在其他布局中的layout.xml文件。

这篇文章给出了一个如何使用它的很好的例子。 Simple example of <merge> and <include> usage in Android XML-layouts

答案 2 :(得分:1)

无法在活动范围内实现此目的。布局独立于您的Acitvity(好吧,直到你绑定它们)。

您的问题的解决方案可能是使用公共标题布局,保存在布局文件夹下的xml文件中,如下所示:

header.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/red"
    ... />

使用include标记将它们包含在布局中:

my_activity_layout.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    ....
    .... >

    <include layout="@layout/header.xml" android:id="@+id/header" />

    <!-- Countinue your layout .... -->

</RelativeLayout>