包含所有Android活动的底部布局,onclick事件不起作用

时间:2018-01-25 06:30:15

标签: android inheritance scrollview onclicklistener

我是Android开发的新手。我想在所有android活动中包含相同的水平Scroll视图。我已经在一个单独的活动中定义了布局和Onclick事件,并将该类与其他活动一起扩展。但是onclick事件不起作用

这是我的基本活动

public class Footer extends AppCompatActivity implements View.OnClickListener {
    private ImageView img_school, img_group, img_news, img_schemes, img_jobs, img_gallery, img_goddess, img_services, img_census, img_address, img_abt_us;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imgs_school:
                Intent i = new Intent(Footer.this, SchoolActivity.class);
                startActivity(i);
                break;
            case R.id.imgs_galary:
                Intent gi = new Intent(Footer.this, GalleryActivity.class);
                startActivity(gi);
                break;
            case R.id.imgs_events:
                Intent ni = new Intent(Footer.this, EventsActivity.class);
                startActivity(ni);
                break;
            case R.id.imgs_abtus:
                break;
            case R.id.imgs_jobs:
                Intent ji = new Intent(Footer.this, JobsActivity.class);
                startActivity(ji);
                break;

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_footer);
        initAll();
        img_gallery.setOnClickListener(this);
        img_school.setOnClickListener(this);
        img_news.setOnClickListener(this);
        img_jobs.setOnClickListener(this);
        img_abt_us.setOnClickListener(this);
    }

    public void initAll() {

        img_school = findViewById(R.id.imgs_school);

        img_news = findViewById(R.id.imgs_events);
        img_jobs = findViewById(R.id.imgs_jobs);
        img_gallery = findViewById(R.id.imgs_galary);
        img_abt_us = findViewById(R.id.imgs_abtus);
    }
}

和XML

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:background="#fff"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <HorizontalScrollView
        android:scrollbars="none"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <LinearLayout
                android:orientation="vertical"
                android:paddingLeft="@dimen/_20sdp"
                android:paddingRight="@dimen/_50sdp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/imgs_school"
                    android:layout_gravity="center"
                    android:clickable="true"
                    android:src="@drawable/school"
                    android:layout_width="@dimen/_35sdp"
                    android:layout_height="@dimen/_35sdp" />
                <TextView
                    android:text="School"
                    android:layout_gravity="center"
                    android:textSize="@dimen/_13sdp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

            <LinearLayout
                android:orientation="vertical"
                android:paddingRight="@dimen/_50sdp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/imgs_events"
                    android:clickable="true"
                    android:src="@drawable/news"
                    android:layout_gravity="center"
                    android:layout_width="@dimen/_35sdp"
                    android:layout_height="@dimen/_35sdp" />
                <TextView
                    android:text="Events"
                    android:layout_gravity="center"
                    android:textSize="@dimen/_13sdp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <LinearLayout
                android:orientation="vertical"
                android:paddingRight="@dimen/_50sdp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/imgs_jobs"
                    android:layout_gravity="center"
                    android:clickable="true"
                    android:src="@drawable/jobs"
                    android:layout_width="@dimen/_35sdp"
                    android:layout_height="@dimen/_35sdp" />
                <TextView
                    android:text="Jobs"
                    android:layout_gravity="center"
                    android:textSize="@dimen/_13sdp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <LinearLayout
                android:paddingRight="@dimen/_50sdp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/imgs_galary"
                    android:clickable="true"
                    android:src="@drawable/gallery"
                    android:layout_gravity="center"
                    android:layout_width="@dimen/_35sdp"
                    android:layout_height="@dimen/_35sdp" />
                <TextView
                    android:text="Gallery"
                    android:layout_gravity="center"
                    android:textSize="@dimen/_13sdp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:paddingRight="@dimen/_50sdp"
                android:layout_height="wrap_content">
                <ImageView
                    android:layout_gravity="center"
                    android:id="@+id/imgs_abtus"
                    android:clickable="true"
                    android:src="@drawable/abt_us"
                    android:layout_width="@dimen/_35sdp"
                    android:layout_height="@dimen/_35sdp" />
                <TextView
                    android:text="About Us"
                    android:layout_gravity="center"
                    android:textSize="@dimen/_13sdp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

我正在将此活动扩展到其他课程

,例如

public class SchoolActivity extends Footer {

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

及其activity_school xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.evoqis.manovaru.SchoolActivity">

<include layout="@layout/activity_footer"
    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</RelativeLayout>

但onclick事件没有发生。 谢谢你

1 个答案:

答案 0 :(得分:0)

问题在于:

在子类中,您调用:

super.onCreate(savedInstanceState);   

哪个电话: -

setContentView(R.layout.activity_footer); //which creates a new views    
initAll(); //which finds views by id    
img_gallery.setOnClickListener(this); //setting listeners.

然后在子类中调用:
setContentView(R.layout.activity_school); 创建新视图,并覆盖您在超类中设置的所有侦听器。

如何修复

移动

    img_gallery.setOnClickListener(this);
    img_school.setOnClickListener(this);
    img_news.setOnClickListener(this);
    img_jobs.setOnClickListener(this);
    img_abt_us.setOnClickListener(this);

public void initAll()内,在子类中执行此操作:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school);
initAll();

您也可以从超类中删除onCreate()方法。