按钮不会在Recyclerview项目中获得焦点

时间:2016-08-06 07:37:50

标签: android android-layout listview expandablelistview

我正在尝试点击Recyclerview项目中的按钮,但它没有收到onclick事件。但它可以双击以响应onclick监听器

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="5dp"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
<ImageView
android:layout_width="62dp"
android:layout_height="62dp"
android:layout_marginRight="5dp"
android:focusableInTouchMode="false"
android:scaleType="fitXY"
android:layout_marginLeft="5dp"
android:id="@+id/calllog_contact_img"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/calllog_contact_name"
        android:layout_marginBottom="2dp"
        android:text="Pounkumar Purushothaman"
        android:textSize="18dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginRight="20dp"
            android:focusableInTouchMode="false"
            android:src="@android:drawable/sym_call_incoming"
            android:layout_marginBottom="10dp"
            android:id="@+id/calllog_call_type"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/calllog_no"
                android:text="+919043974134"/>
           <LinearLayout
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:orientation="horizontal">
               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="10:20"
                   android:layout_marginRight="20dp"
                   android:id="@+id/calllog_call_duration"/>
               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="10:20"
                   android:id="@+id/calllog_call_timing"/>
           </LinearLayout>
        </LinearLayout>
     </LinearLayout>
  </LinearLayout>
  </LinearLayout>
    <LinearLayout
        android:id="@+id/llExpandArea"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="6dp"
            android:id="@+id/call_txt"
            android:text="Call" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="6dp"
            android:paddingLeft="15dp"
            android:id="@+id/block_txt"
            android:text="Msg" />
</LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

我正在尝试点击Recyclerview项目中的按钮,但它没有收到onclick事件。但它可以双击以响应onclick监听器

适配器:

public class calllog_customadapter extends RecyclerView.Adapter<calllog_customadapter.RecordHolder> {
private List<calllog_item> calllog_items;
Context c;
RecyclerView list;
private int expandedPosition = -1;
@Override
public RecordHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.calllog_item, parent, false);
    return new RecordHolder(itemView);
}
calllog_customadapter(Context context, RecyclerView list, final List<calllog_item> calllog_item){
    this.c=context;
    this.list=list;
    this.calllog_items=calllog_item;
    list.addOnItemTouchListener(new RecyclerItemClickListener(c, list ,new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Button call= (Button) view.findViewById(R.id.call_txt);
            Button block= (Button) view.findViewById(R.id.block_txt);
            call.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(c, "call", Toast.LENGTH_SHORT).show();
                }
            });
            block.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(c, "blockbbb222", Toast.LENGTH_SHORT).show();
                }
            });
            if (expandedPosition >= 0) {
                int prev = expandedPosition;
                notifyItemChanged(prev);
            }
            expandedPosition = position;
            notifyItemChanged(expandedPosition);
        }

        @Override
        public void onLongItemClick(View view, int position) {
            // do whatever
        }
    }));
}

@Override
public void onBindViewHolder(RecordHolder holder, int position) {
    calllog_item list=calllog_items.get(position);
    holder.contact_name.setText(list.getContact_name());
    holder.contact_img.setImageBitmap(list.getContact_img());
    holder.call_type.setImageBitmap(list.getCall_type());
    holder.call_duration.setText(list.getCall_duration());
    holder.call_timing.setText(list.call_timing);
    holder.contact_no.setText(list.getCall_no());
    if (position == expandedPosition) {
        holder.llExpandArea.setVisibility(View.VISIBLE);
    } else {
        holder.llExpandArea.setVisibility(View.GONE);
    }
}

@Override
public int getItemCount() {
    return calllog_items.size();
}



public class RecordHolder extends RecyclerView.ViewHolder  {
    TextView contact_name,call_timing,call_duration,contact_no;
    ImageView contact_img,call_type;
    LinearLayout llExpandArea;
    public RecordHolder(View itemView) {
        super(itemView);
        contact_img= (ImageView) itemView.findViewById(R.id.calllog_contact_img);
        call_type= (ImageView) itemView.findViewById(R.id.calllog_call_type);
        call_duration= (TextView) itemView.findViewById(R.id.calllog_call_duration);
        call_timing= (TextView) itemView.findViewById(R.id.calllog_call_timing);
        contact_name= (TextView) itemView.findViewById(R.id.calllog_contact_name);
        contact_no= (TextView) itemView.findViewById(R.id.calllog_no);
        llExpandArea=(LinearLayout)itemView.findViewById(R.id.llExpandArea);
    }
  }
}

1 个答案:

答案 0 :(得分:1)

您是否尝试将其设置为可引用且可聚焦?

XML

android:focusable="true"
android:clickable="true"

爪哇

button.setClickable("true");
button.setFocusable("true");

如果没有任何变化,请尝试在onClick方法中进行日志调用(可能代码中存在不良内容),或者在OnTouch中。