onClickListener не работает для просмотра карты - PullRequest
0 голосов
/ 18 января 2019

Вот мой файл деятельности_основной

   <?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

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);

    }
}

когда я пытаюсь добавить onClickListener к просмотру карты, он не работает. Я также пытался применить android:clickable="true" ко всем картам, но безрезультатно. Он не показывает никаких ошибок и работает нормально, но прослушиватель onclick не работает. Может кто-нибудь сказать мне, если я сделал какую-либо ошибку. Это правильный путь или любой другой путь.

1 Ответ

0 голосов
/ 18 января 2019

Попробуйте поместить слушателя щелчка на дочерний вид карты, как

<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>

и в своей деятельности определите linearLayout и попробуйте установить onClicklistener с

...