线性布局单击不起作用,我在做什么错?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线性布局单击不起作用,我在做什么错?相关的知识,希望对你有一定的参考价值。

我有一个视图,我希望将其设置为可单击以打开新活动。该代码运行没有任何错误,但是当我单击主要活动中的列表时,什么也没有发生。列表显示正常,但是当单击特定数据时,什么也没有发生。这是适配器的代码

package com.example.helpresponse;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Locale;

public class TargetDataAdapter extends RecyclerView.Adapter<TargetDataAdapter.TargetViewHolder>

    ArrayList<AndroidTargets> targetsArrayList;
    private Context context;
    private HandleClick mHandleClick;


    public TargetDataAdapter(ArrayList<AndroidTargets> mTargetData) 
        targetsArrayList = mTargetData;
    

    public void setmHandleClick(HandleClick handleClick) 
        this.mHandleClick = handleClick;
    
    @NonNull
    @Override
    public TargetViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) 
        context = viewGroup.getContext();
        View v= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.target_row,viewGroup,false);
        return new TargetViewHolder(v);
    

    @Override
    public void onBindViewHolder(@NonNull TargetViewHolder viewHolder, int i) 
        viewHolder.androidTargetName.setText(targetsArrayList.get(i).FIELD1 );
        viewHolder.androidTargetNumber.setText(String.format(Locale.getDefault(), "API Level: %d", targetsArrayList.get(i).FIELD2));
        viewHolder.androidTargetShortName.setText(targetsArrayList.get(i).FIELD3);
        viewHolder.myClickableView.setClickable(true);


/*
        viewHolder.myClickableView.setOnClickListener((v) -> 
            Intent intent = new Intent(context, MapsActivity.class);
            //int myData = 1;
            //intent.putExtra("myDataKey", myData);
            //more intent.putExtra(s) as needed
            context.startActivity(intent);
        );

*/


        viewHolder.myClickableView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                // Do your stuff

                Intent intent = new Intent(context, MapsActivity.class);
                int myData = 1;
                intent.putExtra("myDataKey", myData);
                //more intent.putExtra(s) as needed
                context.startActivity(intent);
            
        );

    
    @Override
    public int getItemCount() 
        if(targetsArrayList == null)
            return 0;
        return targetsArrayList.size();
    

    public class TargetViewHolder extends RecyclerView.ViewHolder 
        protected TextView androidTargetName;
        protected TextView androidTargetNumber;
        protected TextView androidTargetShortName;
        protected LinearLayout myClickableView;


        public TargetViewHolder(@NonNull View itemView) 

            super(itemView);
            myClickableView =  itemView.findViewById(R.id.linearLayout);
            androidTargetShortName = itemView.findViewById(R.id.textView2);
            androidTargetName = itemView.findViewById(R.id.textView3);
            androidTargetNumber = itemView.findViewById(R.id.textView4);

        
    

    public interface HandleClick 

        void onItemClick(int index);

    




此代码是主要活动

package com.example.helpresponse;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class ShowStudentDetailsActivity extends AppCompatActivity 


    DatabaseReference databaseReference;

    ProgressDialog progressDialog;

    List<StudentDetails> list = new ArrayList<>();

    RecyclerView recyclerView;

    RecyclerView.Adapter adapter ;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_student_details);



        recyclerView = findViewById(R.id.recyclerView);

        recyclerView.setHasFixedSize(true);

        recyclerView.setLayoutManager(new LinearLayoutManager(ShowStudentDetailsActivity.this));

        progressDialog = new ProgressDialog(ShowStudentDetailsActivity.this);

        progressDialog.setMessage("Loading Data from Firebase Database");

        progressDialog.show();

        databaseReference = FirebaseDatabase.getInstance().getReference("Police").child("Chats");

        databaseReference.addValueEventListener(new ValueEventListener() 
            @Override
            public void onDataChange(DataSnapshot snapshot) 

                for (DataSnapshot dataSnapshot : snapshot.getChildren()) 

                    StudentDetails studentDetails = dataSnapshot.getValue(StudentDetails.class);

                    list.add(studentDetails);
                

                adapter = new RecyclerViewAdapter(ShowStudentDetailsActivity.this, list);


                recyclerView.setAdapter(adapter);

                progressDialog.dismiss();
            

            @Override
            public void onCancelled(DatabaseError databaseError) 

                progressDialog.dismiss();

            
        );


    

此代码是layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    >

    <TextView
        android:id="@+id/textView2"
        android:textColor="@color/colorPrimary"
        style="@style/Base.TextAppearance.AppCompat.Small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:gravity="center"
        android:text="textview"
        android:minWidth="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:clickable="true"/>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="parent"
        android:clickable="true"
        >

        <TextView
            android:id="@+id/textView3"
            android:gravity="center_vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_marginBottom="8dp"
            android:text="textview"
            android:clickable="true"/>



        <TextView
            android:layout_marginTop="8dp"
            android:id="@+id/textView4"
            android:gravity="center_vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="textview"
            android:clickable="true"/>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

请帮助,谢谢

答案

您没有使用您的实现,这应该可以解决您的问题。

public class ShowStudentDetailsActivity extends AppCompatActivity 
     DatabaseReference databaseReference;
     ProgressDialog progressDialog;
     List<StudentDetails> list = new ArrayList<>();
     RecyclerView recyclerView;
     TargetDataAdapter adapter;

...

adapter = new TargetDataAdapter(list);
另一答案

菲利普,祝你圣诞快乐。首先,您创建了一个自定义适配器,并且没有使用它。其次,您正在尝试通过抽象适配器的某些功能(例如onClickListeners)来减轻适配器的工作量,但是您仍然在适配器内部执行onClick,这几乎破坏了抽象的目的。最后,接口基本上将要实现,我很好奇您是如何错过的。

下面是代码的注释正确片段,只有当您阅读注释时,您才能更好地理解它。

首先是您的适配器:

public class TargetDataAdapter extends RecyclerView.Adapter<TargetDataAdapter.TargetViewHolder>

    ArrayList<AndroidTargets> targetsArrayList;
    private Context context;
    private HandleClick mHandleClick;


    public TargetDataAdapter(ArrayList<AndroidTargets> mTargetData) 
        targetsArrayList = mTargetData;
    

    public void setmHandleClick(HandleClick handleClick) 
        this.mHandleClick = handleClick;
    

    @NonNull
    @Override
    public TargetViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) 
        context = viewGroup.getContext();
        View v= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.target_row,viewGroup,false);
        return new TargetViewHolder(v);
    

    @Override
    public void onBindViewHolder(@NonNull TargetViewHolder viewHolder, int i) 
        viewHolder.androidTargetName.setText(targetsArrayList.get(i).FIELD1 );
        viewHolder.androidTargetNumber.setText(String.format(Locale.getDefault(), "API Level: %d", targetsArrayList.get(i).FIELD2));
        viewHolder.androidTargetShortName.setText(targetsArrayList.get(i).FIELD3);
        viewHolder.myClickableView.setClickable(true);

        viewHolder.myClickableView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                // Do your stuff
                mHandleClick.onItemClick(i);//here is where you call the onItemClick method in the interface
            
        );

    

    public interface HandleClick 

        void onItemClick(int index);

    

您的ShowStudentDetails活动:

    //Take note of the implements keyword
        public class ShowStudentDetailsActivity extends AppCompatActivity, implements HandleClick 


            DatabaseReference databaseReference;

            ProgressDialog progressDialog;

            ArrayList<StudentDetails> list = new ArrayList<>();

            RecyclerView recyclerView;

            TargetDataAdapter adapter;

            @Override
            protected void onCreate(Bundle savedInstanceState) 
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_show_student_details);

                recyclerView = findViewById(R.id.recyclerView);

                recyclerView.setHasFixedSize(true);

                recyclerView.setLayoutManager(new LinearLayoutManager(ShowStudentDetailsActivity.this));

                progressDialog = new ProgressDialog(ShowStudentDetailsActivity.this);

                progressDialog.setMessage("Loading Data from Firebase Database");

                progressDialog.show();

                databaseReference = FirebaseDatabase.getInstance().getReference("Police").child("Chats");

                databaseReference.addValueEventListener(new ValueEventListener() 
                    @Override
                    public void onDataChange(DataSnapshot snapshot) 

                        for (DataSnapshot dataSnapshot : snapshot.getChildren()) 

                            StudentDetails studentDetails = dataSnapshot.getValue(StudentDetails.class);

                            list.add(studentDetails);
                        

                        adapter = new TargetDataAdapter(list);

        /*The this keyword below parses whatever setmHandleClick() method needs that's present in that same class, which is the onItemClick() method in the interface that we implemented. So, the this keyword simply is a reference to ShowStudentDetailsActivity as an object, including all it resources like context */
                        adapter.setmHandleClick(this)

                        recyclerView.setAdapter(adapter);

                        progressDialog.dismiss();
                    

                    @Override
                    public void onCancelled(DatabaseError databaseError) 

                        progressDialog.dismiss();

                    
                );
            

    /* This is the result of the implemented interface above using implements keyword
       you will note that without the below method, our class will show error and it's
basically telling us to do like below. So, when you implement an interface, you will be forced to have all its method implemented like below also.
I will suggest you read more on interface in java.*/
        @Override
        void onItemClick(int index)
        //note that we have moved the code that was in the adapter back to the activity it self.
           Intent intent = new Intent(this, MapsActivity.class);
                 int myData = index;
                 intent.putExtra("myDataKey", myData);
                 startActivity(intent);
             

就是这样。

奖金,下面是更好的方法和可读的版本。

首先,我将接口放在一个单独的文件中。

public interface HandleClick 
    void onItemClick(int index);

第二个我的适配器:

    public class TargetDataAdapter extends RecyclerView.Adapter<TargetDataAdapter.TargetViewHolder>

        private ArrayList<AndroidTargets> targetsArrayList;
        private HandleClick mHandleClick;

/*Note that the constructor now take two params which helps us get ride of setmHandleClick() method.*/
        public TargetDataAdapter(ArrayList<AndroidTargets> mTargetData, HandleClick handleClick) 
            targetsArrayList = mTargetData;
            mHandleClick = handleClick;
        

        @NonNull
        @Override
        public TargetViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) 
            View v= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.target_row,viewGroup,false);
            return new TargetViewHolder(v);
        

/* There is an interesting way to make below code neater by abstracting it back to the Activity incharge, let me know if you want that...priority now is to focus on fixing your issue. */
        @Override
        public void onBindViewHolder(@NonNull TargetViewHolder viewHolder, int i) 
            viewHolder.androidTargetName.setText(targetsArrayList.get(i).FIELD1 );
            viewHolder.androidTargetNumber.setText(String.format(Locale.getDefault(), "API Level: %d", targetsArrayList.get(i).FIELD2));
            viewHolder.androidTargetShortName.setText(targetsArrayList.get(i).FIELD3);
            viewHolder.myClickableView.setClickable(true);

            viewHolder.myClickableView.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View view) 
                    mHandleClick.onItemClick(i);
                
            );
        
    

我的ShowStudentDetailsActivity:

//Note that you can implement more than one interfaces, that's one of it's beauty.
public class ShowStudentDetailsActivity extends AppCompatActivity, implements HandleClick 
            private DatabaseReference databaseReference;

            ProgressDialog progressDialog;

            private Arr

以上是关于线性布局单击不起作用,我在做什么错?的主要内容,如果未能解决你的问题,请参考以下文章

当我调用可见性时,Android 动画不起作用

将相对布局转换为线性布局

如何通过单击片段内的线性布局从片段类开始新活动?下面是我的代码,但这不起作用

动画在 Android 中无法正常工作(仅执行一次)

Android 底部工作表布局边距

如何在 ScrollView 底部对齐线性广告布局?