简体   繁体   English

onClickListener不适用于名片视图

[英]onClickListener not working for card view

Here it's is my activity_main file 这是我的activity_main文件

   <?xml version="1.0" encoding="utf-8"?> <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"
        android:background="#eeeeee"
        android:id="@+id/container"
        tools:context=".MainActivity">


        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="15dp"
            android:layout_marginStart="15dp"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp">


            <android.support.v7.widget.CardView


                android:id="@+id/cardTop"
                android:layout_width="match_parent"
                android:layout_height="205dp"
                android:layout_marginEnd="4dp"
                app:cardCornerRadius="10dp"
                android:elevation="4dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" >

                <android.support.constraint.ConstraintLayout
                    android:id="@+id/lab"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">


                    <ImageView
                        android:id="@+id/imageView"
                        android:layout_width="wrap_content"
                        android:layout_height="145dp"
                        app:layout_constraintEnd_toEndOf="parent"
                        android:scaleType="centerCrop"
                        app:layout_constraintHorizontal_bias="0.0"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:srcCompat="@drawable/cse1" />

                </android.support.constraint.ConstraintLayout>

        </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:id="@+id/cardRight"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:cardCornerRadius="10dp"
                android:elevation="4dp"
                android:layout_marginEnd="4dp"
                android:layout_marginStart="4dp"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="@+id/guideline"
                app:layout_constraintTop_toBottomOf="@+id/cardTop">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:weightSum="6"
                    android:orientation="vertical"
                    android:gravity="center">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="3"
                        android:src="@drawable/illustration"
                        android:scaleType="centerCrop"/>




                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.constraint.Guideline
                android:id="@+id/guideline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_begin="172dp" />

            <android.support.v7.widget.CardView
                android:id="@+id/cardleft1"
                android:layout_width="0dp"
                app:cardCornerRadius="10dp"
                android:elevation="4dp"
                android:layout_height="0dp"
                android:layout_marginBottom="4dp"
                android:layout_marginEnd="4dp"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toTopOf="@+id/cardleft2"
                app:layout_constraintEnd_toStartOf="@+id/guideline"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardTop" >


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:gravity="center">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="84dp"
                        android:src="@drawable/support"/>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_marginTop="5dp"
                        android:layout_marginBottom="5dp"
                        android:layout_marginStart="10dp"
                        android:layout_marginEnd="10dp"
                        android:background="#c0c0c0"/>

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:id="@+id/cardleft2"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginEnd="4dp"
                android:layout_marginTop="4dp"
                app:cardCornerRadius="10dp"
                android:elevation="4dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/guideline"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardleft1"
                app:layout_constraintVertical_bias="0.0" >


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:gravity="center">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="84dp"
                        android:src="@drawable/ground"
                        android:layout_marginBottom="2dp"
                        android:scaleType="centerCrop"/>


                </LinearLayout>

            </android.support.v7.widget.CardView>
        </android.support.constraint.ConstraintLayout>
        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <FrameLayout
                android:id="@+id/framelLayoutOne"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >
            </FrameLayout>
            <android.support.design.widget.NavigationView
                android:id="@+id/navigation_view"
                app:headerLayout="@layout/header"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@color/white"
                app:itemTextColor="@color/darkgrey"
                app:itemIconTint="@color/darkgrey"
                app:menu="@menu/drawermenu"
                android:layout_gravity="start">
            </android.support.design.widget.NavigationView>
        </android.support.v4.widget.DrawerLayout> </RelativeLayout>

MainActivity.java MainActivity.java

public class MainActivity extends AppCompatActivity {

    private DatabaseReference mUserRef;
    private ImageButton mLogout;
    private FirebaseAuth mAuth;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    CardView cardTop, cardLeft1, cardLeft2, cardRight;
    Animation upToDown, leftToRight, downToUp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.open, R.string.close);
        cardTop = (CardView)findViewById(R.id.cardTop);
        cardLeft1 = (CardView)findViewById(R.id.cardleft1);
        cardLeft2 = (CardView)findViewById(R.id.cardleft2);
        cardRight = (CardView)findViewById(R.id.cardRight);

        cardTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent labIntent = new Intent(MainActivity.this,LabInfastructure.class);
                startActivity(labIntent);
            }
        });
        cardRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent dashBoardIntent = new Intent(MainActivity.this, Dashboard.class);
                startActivity(dashBoardIntent);
            }
        });
        cardLeft1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent faculty = new Intent(MainActivity.this,Departments.class);
                startActivity(faculty);
            }
        });
        cardLeft2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent games = new Intent(MainActivity.this,GamesActivity.class);
                startActivity(games);
            }
        });

        upToDown = AnimationUtils.loadAnimation(this,R.anim.uptodown);
        cardTop.setAnimation(upToDown);
        leftToRight = AnimationUtils.loadAnimation(this,R.anim.lefttoright);
        cardLeft1.setAnimation(leftToRight);
        cardLeft2.setAnimation(leftToRight);
        downToUp = AnimationUtils.loadAnimation(this,R.anim.downtotop);
        cardRight.setAnimation(downToUp);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view);
        setupDrawerContent(navigationView);
        mAuth=FirebaseAuth.getInstance();
        if(mAuth.getCurrentUser() != null ){
            mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());
        }
}
        public void selectItemDrawer (MenuItem menuItem){
            android.support.v4.app.Fragment myFragement = null;
         //   Fragment myFragement = null;
            Class fragementClass;
            switch (menuItem.getItemId()){

                case R.id.db :
                   fragementClass = Dashboard.class;
                   break;
                case R.id.gallery :
                   fragementClass = Gallery.class;
                   break;
                case R.id.contactUs :
                   fragementClass = Contactus.class;
                   break;
                case R.id.aboutCollege :
                   fragementClass = AboutCollege.class;
                   break;
                case R.id.appInfo :
                   fragementClass = AppInfo.class;
                   break;
                case R.id.inviteFriends :
                   fragementClass = InviteFriends.class;
                   break;
                case R.id.info :
                    fragementClass = Info.class;
                    break;
                default:
                    fragementClass = Dashboard.class;
            }
            try{

                myFragement = (android.support.v4.app.Fragment) fragementClass.newInstance();
            }
            catch (Exception e){
                e.printStackTrace();
            }
            FragmentManager fragementManager = getSupportFragmentManager();
            fragementManager.beginTransaction().replace(R.id.framelLayoutOne,myFragement).commit();
            menuItem.setChecked(true);
            setTitle(menuItem.getTitle());
            mDrawerLayout.closeDrawers();
        }
        private void setupDrawerContent (NavigationView navigationView){
            navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        selectItemDrawer(item);
                    return false;
                }
            });
        }
    @Override
    protected void onStart() {
        super.onStart();
        FirebaseUser currentUser = mAuth.getCurrentUser();
        if(currentUser == null){

            sendToStart();
        }
    }
    private void sendToStart() {

        Intent startIntent = new Intent(MainActivity.this,StartActivity.class);
        startActivity(startIntent);
        finish();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return  true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (mToggle.onOptionsItemSelected(item)){
            return true;
        }

        switch (item.getItemId()) {
            case R.id.logout:
                logout();
                return true;

            case R.id.notifications :

                NotificationFragment notificationFragment = new NotificationFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.container,notificationFragment).commit();
                break;

            case R.id.newpost :
                    Intent post = new Intent(MainActivity.this,NewPostActivity.class);
                    startActivity(post);
                break;

            case R.id.home :
                Intent home = new Intent(MainActivity.this,MainActivity.class);
                startActivity(home);
                break;

            default:
                return super.onOptionsItemSelected(item);
        }

        return super.onOptionsItemSelected(item);
    }
    private void logout() {

        FirebaseAuth.getInstance().signOut();

        Intent logoutIntent = new Intent(MainActivity.this, StartActivity.class);
        //  Toast.makeText(MainActivity.this,"Logout Btn ... ", Toast.LENGTH_LONG);
        startActivity(logoutIntent);

    }
}

when I am trying to add onClickListener to card view it is not working. 当我尝试将onClickListener添加到名片视图时,它不起作用。 am also tried with applying android:clickable="true" to all the cardview but no use.It doesn't showing any error and its working fine but onclick listener is not working. 还尝试将android:clickable="true"应用于所有cardview,但没有用。它没有显示任何错误,并且工作正常,但onclick侦听器不起作用。 can anyone tell me if I have made any mistake done. 谁能告诉我我做错了什么吗? Is it correct way or any other way is there. 它是正确的方法还是其他任何方法?

Try to put the click listener on the child view of the card like 尝试将点击侦听器放在卡片的子视图上,例如

<android.support.v7.widget.CardView
            android:id="@+id/cardRight"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:cardCornerRadius="10dp"
            android:elevation="4dp"
            android:layout_marginEnd="4dp"
            android:layout_marginStart="4dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/guideline"
            app:layout_constraintTop_toBottomOf="@+id/cardTop">

            <LinearLayout
                android:id="@+id/layRight"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:weightSum="6"
                android:orientation="vertical"
                android:gravity="center">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="3"
                    android:src="@drawable/illustration"
                    android:scaleType="centerCrop"/>

            </LinearLayout>
        </android.support.v7.widget.CardView>

and on your activity define the linearLayout and try to set onClicklistener with 并在您的活动中定义linearLayout并尝试使用以下方法设置onClicklistener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM