Android 充气框架布局上的按钮未触发单击事件

Android 充气框架布局上的按钮未触发单击事件,android,android-layout,layout-inflater,android-framelayout,Android,Android Layout,Layout Inflater,Android Framelayout,我正在使用以下coverflow库: 每个封面都有自己的框架布局。我添加到framelayout中的按钮不执行onClick事件。具体地说,我在Apply按钮中添加了onClick事件,但是onClick事件是在单击评级标签时执行的,而不是在单击Apply按钮时执行的。 我假设布局在某种意义上是混乱的。我尝试了前面问题的多种解决方案,但都没有成功 膨胀布局时,我会传递父视图,并将attachToRoot参数设置为false: LayoutInflater inflater = (LayoutIn

我正在使用以下coverflow库:

每个封面都有自己的框架布局。我添加到framelayout中的按钮不执行onClick事件。具体地说,我在Apply按钮中添加了onClick事件,但是onClick事件是在单击评级标签时执行的,而不是在单击Apply按钮时执行的。 我假设布局在某种意义上是混乱的。我尝试了前面问题的多种解决方案,但都没有成功

膨胀布局时,我会传递父视图,并将attachToRoot参数设置为false:

LayoutInflater inflater = (LayoutInflater) mContext.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.item_coverflow, parent, false);
请参阅下面的详细代码。为了清楚起见,用于说明目的的按钮在项目_coverflow.xml中具有id buttonApply,其onClick侦听器在CoverFlowAdapter.java中分配。谢谢你的帮助

我已将自定义coverflow元素添加到RelativeLayout:

    <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_register"
        android:layout_gravity="center"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:coverflow="http://schemas.android.com/apk/res-auto"
        tools:context="za.co.xxx.xxx.VacancyActivity">

    <it.moondroid.coverflow.components.ui.containers.FeatureCoverFlow
        android:id="@+id/coverFlow"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        coverflow:coverHeight="@dimen/cover_height"
        coverflow:coverWidth="@dimen/cover_width"
        coverflow:maxScaleFactor="1.5"
        coverflow:reflectionGap="0px"
        coverflow:rotationThreshold="0.5"
        coverflow:scalingThreshold="0.5"
        coverflow:spacing="0.6" />
    </RelativeLayout>

coverflow为coverflow中的每个视图使用框架布局,我在其中添加了相关按钮,特别是id为buttonApply的button就是一个例子:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="@dimen/cover_width"
    android:layout_height="@dimen/cover_height"
    android:background="@color/black_transparent">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:stretchColumns="0,1">
        <TableRow>
            <TextView
                android:id="@+id/textViewPracticeNameLbl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/practice"
                android:textColor="@color/white"
                android:layout_column="0"
                android:maxLines="4"/>

            <TextView
                android:id="@+id/textViewPracticeName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/not_available"
                android:textColor="@color/white"
                android:layout_column="1"
                android:maxLines="4"/>
        </TableRow>

        <TableRow>
            <TextView
                android:id="@+id/textViewEmploymentTypeLbl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/practice"
                android:textColor="@color/white"
                android:layout_column="0"
                android:maxLines="4"/>

            <TextView
                android:id="@+id/textViewEmploymentType"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/not_available"
                android:textColor="@color/white"
                android:layout_column="1"
                android:maxLines="4"/>
        </TableRow>

        <TableRow>
            <TextView
                android:id="@+id/textViewVacancyTypeLbl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/practice"
                android:textColor="@color/white"
                android:layout_column="0"
                android:maxLines="4"/>

            <TextView
                android:id="@+id/textViewVacancyType"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/not_available"
                android:textColor="@color/white"
                android:layout_column="1"
                android:maxLines="4"/>
        </TableRow>

        <TableRow>
            <TextView
                android:id="@+id/textViewAddressLbl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/practice"
                android:textColor="@color/white"
                android:layout_column="0"
                android:maxLines="4"/>

            <TextView
                android:id="@+id/textViewAddress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/not_available"
                android:textColor="@color/white"
                android:layout_column="1"
                android:maxLines="4"/>
        </TableRow>

        <TableRow>
            <TextView
                android:id="@+id/textViewRatePerHourLbl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/practice"
                android:textColor="@color/white"
                android:layout_column="0"
                android:maxLines="4"/>

            <TextView
                android:id="@+id/textViewRatePerHour"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/not_available"
                android:textColor="@color/white"
                android:layout_column="1"
                android:maxLines="4"/>
        </TableRow>

        <TableRow>
            <TextView
                android:id="@+id/textViewStartDateLbl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/practice"
                android:textColor="@color/white"
                android:layout_column="0"
                android:maxLines="4"/>

            <TextView
                android:id="@+id/textViewStartDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/not_available"
                android:textColor="@color/white"
                android:layout_column="1"
                android:maxLines="4"/>
        </TableRow>

        <TableRow>
            <TextView
                android:id="@+id/textViewEndDateLbl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/practice"
                android:textColor="@color/white"
                android:layout_column="0"
                android:maxLines="4"/>

            <TextView
                android:id="@+id/textViewEndDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/not_available"
                android:textColor="@color/white"
                android:layout_column="1"
                android:maxLines="4"/>
        </TableRow>

        <TableRow>
            <TextView
                android:id="@+id/textViewDistanceTravelLbl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/practice"
                android:textColor="@color/white"
                android:layout_column="0"
                android:maxLines="4"/>

            <TextView
                android:id="@+id/textViewDistanceTravel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/not_available"
                android:textColor="@color/white"
                android:layout_column="1"
                android:maxLines="4"/>
        </TableRow>

        <TableRow>
            <TextView
                android:id="@+id/textViewTimeToTravelLbl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/practice"
                android:textColor="@color/white"
                android:layout_column="0"
                android:maxLines="4"/>

            <TextView
                android:id="@+id/textViewTimeToTravel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/not_available"
                android:textColor="@color/white"
                android:layout_column="1"
                android:maxLines="4"/>
        </TableRow>

        <TableRow>
            <TextView
                android:id="@+id/textViewRatingLbl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/practice"
                android:textColor="@color/white"
                android:layout_column="0"
                android:maxLines="4"/>

            <TextView
                android:id="@+id/textViewRating"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/not_available"
                android:textColor="@color/white"
                android:layout_column="1"
                android:maxLines="4"/>
        </TableRow>

        <TableRow>
            <Button
                android:id="@+id/buttonViewProfileVideo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/practice"
                android:textColor="@color/colorPrimary"
                android:layout_span="2"
                android:background="@color/transparent" />
        </TableRow>

        <TableRow>
            <Button
                android:id="@+id/buttonViewMoreDetails"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/practice"
                android:textColor="@color/colorPrimary"
                android:layout_span="2"
                android:background="@color/transparent" />
        </TableRow>

        <TableRow>
            <Button
                android:id="@+id/buttonApply"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textSize="@dimen/small_text_size"
                tools:text="@string/practice"
                android:textColor="@color/bright_green"
                android:layout_span="2"
                android:background="@color/transparent"
                android:clickable="true"/>
        </TableRow>

    </TableLayout>

</FrameLayout>

coverflow在此处初始化:

public class VacancyActivity extends AppCompatActivity {

    private FeatureCoverFlow coverFlow;
    private CoverFlowAdapter coverFlowAdapter;
    private ArrayList<VacancyEntity> mData = new ArrayList<>(0);

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

    private void setup() {

        mData.add(new VacancyEntity(
                R.string.practice, "ABC Inc",
                R.string.employment_type, "asldkjhf",
                R.string.vacancy_type, "sdkfaslkdjf",
                R.string.address, "asdfasdf",
                R.string.rate_per_hour, "asdfasdf",
                R.string.start_date, "asdkfdf",
                R.string.end_date, "asdfasdf",
                R.string.distance_to_travel, "sdkfjslkdjf",
                R.string.time_to_travel, "asdfasdf",
                R.string.rating, "asdlkfj",
                R.string.view_practice_profile_video,
                R.string.view_more_details,
                R.string.apply));

        coverFlowAdapter = new CoverFlowAdapter(this);
        coverFlowAdapter.setData(mData);
        coverFlow = (FeatureCoverFlow) findViewById(R.id.coverFlow);
        coverFlow.setAdapter(coverFlowAdapter);
    }
}
公共类VacancyActivity扩展了AppCompative活动{
私人功能coverFlow coverFlow;
专用CoverFlowAdapter CoverFlowAdapter;
私有ArrayList mData=新ArrayList(0);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_);
设置();
}
私有无效设置(){
mData.add(新真空度)(
R.string.practice,“ABC公司”,
R.string.employment_类型,“asldkjhf”,
R.string.u类型“sdkfaslkdjf”,
R.string.address,“asdfasdf”,
R.string.rate_/小时,“asdfasdf”,
R.string.start_日期,“asdkfdf”,
R.string.end_日期,“asdfasdf”,
R.string.distance_to_travel,“sdkfjslkdjf”,
R.string.time_to_travel,“asdfasdf”,
R.string.rating,“asdlkfj”,
R.string.view\u practice\u profile\u video,
R.string.view\u更多详细信息,
R.string.apply);
coverFlowAdapter=新的coverFlowAdapter(此);
coverFlowAdapter.setData(mData);
coverFlow=(功能coverFlow)findViewById(R.id.coverFlow);
coverFlow.setAdapter(coverFlowAdapter);
}
}
充气布局的自定义适配器:

public class CoverFlowAdapter extends BaseAdapter {
    private ArrayList<VacancyEntity> mData = new ArrayList<>(0);
    private Context mContext;

    public CoverFlowAdapter(Context context) {
        mContext = context;
    }

         public void setData(ArrayList<VacancyEntity> data) {
        mData = data;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int pos) {
        return mData.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return pos;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView = convertView;

        if (rowView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.item_coverflow, parent, false);

            ViewHolder viewHolder = new ViewHolder();
            viewHolder.textViewPracticeNameLbl = (TextView)
                    rowView.findViewById(R.id.textViewPracticeNameLbl);
            viewHolder.textViewPracticeName =
                    (TextView) rowView.findViewById(R.id.textViewPracticeName);
            viewHolder.textViewEmploymentTypeLbl =
                    (TextView) rowView.findViewById(R.id.textViewEmploymentTypeLbl);
            viewHolder.textViewEmploymentType =
                    (TextView) rowView.findViewById(R.id.textViewEmploymentType);
            viewHolder.textViewVacancyTypeLbl =
                    (TextView) rowView.findViewById(R.id.textViewVacancyTypeLbl);
            viewHolder.textViewVacancyType =
                    (TextView) rowView.findViewById(R.id.textViewVacancyType);
            viewHolder.textViewAddressLbl =
                    (TextView) rowView.findViewById(R.id.textViewAddressLbl);
            viewHolder.textViewAddress =
                    (TextView) rowView.findViewById(R.id.textViewAddress);
            viewHolder.textViewRatePerHourLbl = (TextView) rowView.findViewById(R.id.textViewRatePerHourLbl);
            viewHolder.textViewRatePerHour =
                    (TextView) rowView.findViewById(R.id.textViewRatePerHour);
            viewHolder.textViewStartDateLbl =
                    (TextView) rowView.findViewById(R.id.textViewStartDateLbl);
            viewHolder.textViewStartDate =
                    (TextView) rowView.findViewById(R.id.textViewStartDate);
            viewHolder.textViewEndDateLbl =
                    (TextView) rowView.findViewById(R.id.textViewEndDateLbl);
            viewHolder.textViewEndDate =
                    (TextView) rowView.findViewById(R.id.textViewEndDate);
            viewHolder.textViewDistanceToTravelLbl =
                    (TextView) rowView.findViewById(R.id.textViewDistanceTravelLbl);
            viewHolder.textViewDistanceToTravel =
                    (TextView) rowView.findViewById(R.id.textViewDistanceTravel);
            viewHolder.textViewTimeToTravelLbl =
                    (TextView) rowView.findViewById(R.id.textViewTimeToTravelLbl);
            viewHolder.textViewTimeToTravel =
                    (TextView) rowView.findViewById(R.id.textViewTimeToTravel);
            viewHolder.textViewRatingLbl =
                    (TextView) rowView.findViewById(R.id.textViewRatingLbl);
            viewHolder.textViewRating =
                    (TextView) rowView.findViewById(R.id.textViewRating);
            viewHolder.buttonViewProfileVideo =
                    (Button) rowView.findViewById(R.id.buttonViewProfileVideo);
            viewHolder.buttonViewMoreDetails =
                    (Button) rowView.findViewById(R.id.buttonViewMoreDetails);
            viewHolder.buttonApply =
                    (Button) rowView.findViewById(R.id.buttonApply);
            viewHolder.buttonApply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(view.getContext(), "hello", Toast.LENGTH_SHORT).show();
                }
            });
            rowView.setTag(viewHolder);
        }

        ViewHolder holder = (ViewHolder) rowView.getTag();

        holder.textViewPracticeNameLbl.setText(mData.get(position).practiceLblId);
        holder.textViewPracticeName.setText(mData.get(position).practiceData);
        holder.textViewEmploymentTypeLbl.setText(mData.get(position).employmentTypeLblId);
        holder.textViewEmploymentType.setText(mData.get(position).employmentTypeData);
        holder.textViewVacancyTypeLbl.setText(mData.get(position).vacancyTypeLblId);
        holder.textViewVacancyType.setText(mData.get(position).vacancyTypeData);
        holder.textViewAddressLbl.setText(mData.get(position).addressLblId);
        holder.textViewAddress.setText(mData.get(position).addressData);
        holder.textViewRatePerHourLbl.setText(mData.get(position).ratePerHourLblId);
        holder.textViewRatePerHour.setText(mData.get(position).ratePerHourData);
        holder.textViewStartDateLbl.setText(mData.get(position).startDateLblId);
        holder.textViewStartDate.setText(mData.get(position).startDateData);
        holder.textViewEndDateLbl.setText(mData.get(position).endDateLblId);
        holder.textViewEndDate.setText(mData.get(position).endDateData);
        holder.textViewDistanceToTravelLbl.setText(mData.get(position).distanceToTravelLblId);
        holder.textViewDistanceToTravel.setText(mData.get(position).distanceToTravelData);
        holder.textViewTimeToTravelLbl.setText(mData.get(position).timeToTravelLblId);
        holder.textViewTimeToTravel.setText(mData.get(position).timeToTravelData);
        holder.textViewRatingLbl.setText(mData.get(position).ratingLblId);
        holder.textViewRating.setText(mData.get(position).ratingData);
        holder.buttonViewProfileVideo.setText(mData.get(position).viewPracticeProfileVideoLblId);
        holder.buttonViewMoreDetails.setText(mData.get(position).viewMoreDetailsLblId);
        holder.buttonApply.setText(mData.get(position).applyLblId);

        return rowView;
    }


    private static class ViewHolder {
        private TextView textViewPracticeNameLbl;
        private TextView textViewPracticeName;
        private TextView textViewEmploymentTypeLbl;
        private TextView textViewEmploymentType;
        private TextView textViewVacancyTypeLbl;
        private TextView textViewVacancyType;
        private TextView textViewAddressLbl;
        private TextView textViewAddress;
        private TextView textViewRatePerHourLbl;
        private TextView textViewRatePerHour;
        private TextView textViewStartDateLbl;
        private TextView textViewStartDate;
        private TextView textViewEndDateLbl;
        private TextView textViewEndDate;
        private TextView textViewDistanceToTravelLbl;
        private TextView textViewDistanceToTravel;
        private TextView textViewTimeToTravelLbl;
        private TextView textViewTimeToTravel;
        private TextView textViewRatingLbl;
        private TextView textViewRating;
        private Button buttonViewProfileVideo;
        private Button buttonViewMoreDetails;
        private Button buttonApply;
    }
}
公共类CoverFlowAdapter扩展了BaseAdapter{
私有ArrayList mData=新ArrayList(0);
私有上下文;
公共CoverFlowAdapter(上下文){
mContext=上下文;
}
公共void setData(ArrayList数据){
mData=数据;
}
@凌驾
public int getCount(){
返回mData.size();
}
@凌驾
公共对象getItem(int-pos){
返回mData.get(pos);
}
@凌驾
公共长getItemId(int-pos){
返回pos;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行视图=转换视图;
if(rowView==null){
LayoutFlater充气器=(LayoutFlater)mContext。
getSystemService(上下文布局\充气机\服务);
rowView=充气机。充气(R.layout.item_coverflow,父项,false);
ViewHolder ViewHolder=新ViewHolder();
viewHolder.textViewPracticeNameLbl=(TextView)
rowView.findviewbyd(R.id.textViewPracticeNameLbl);
viewHolder.textViewPracticeName=
(TextView)rowView.findViewById(R.id.textViewPracticeName);
viewHolder.textViewEmploymentTypeLbl=
(TextView)rowView.findViewById(R.id.textViewEmploymentTypeLbl);
viewHolder.textViewEmploymentType=
(TextView)rowView.findViewById(R.id.textViewEmploymentType);
viewHolder.textViewVacancyTypeLbl=
(TextView)rowView.findViewById(R.id.textViewVacancyTypeLbl);
viewHolder.textViewVacancyType=
(TextView)rowView.findViewById(R.id.textViewVacancyType);
viewHolder.textViewAddressLbl=
(TextView)rowView.findViewById(R.id.textViewAddressLbl);
viewHolder.textViewAddress=
(TextView)rowView.findViewById(R.id.textViewAddress);
viewHolder.textViewRatePerHourbl=(TextView)rowView.findViewById(R.id.textViewRatePerHourbl);
viewHolder.textViewRate每小时=
(TextView)rowView.findViewById(R.id.textViewRate per Hour);
viewHolder.textViewStartDatelebl=
(TextView)rowView.findViewById(R.id.textViewStartDatelebl);
viewHolder.textViewStartDate=
(TextView)rowView.findViewById(R.id.textViewStartDate);
viewHolder.textViewEndDateLbl=
(TextView)rowView.findViewById(R.id.textViewEndDateLbl);
viewHolder.textViewEndDate=
(TextView)rowView.findViewById(R.id.textViewEndDate);
viewHolder.textViewDistanceToTravelBL=
(TextView)rowView.findViewById(R.id.textViewDistanceTravelLbl);
不及物动词