Recyclerview观看者项点击渲染问题

时间:2019-08-23 13:10:08

标签: android-recyclerview

我遇到了有关recyclerview项目点击的问题,因为viewholder(item)内部有一个textview,并且正在实现对该textview的点击。 我面临的问题是,当我在该holder.textview.setOnclicklistener代码块中放置断点时,我发现我的控件将自动进入该代码块,而无需物理/手动单击该项目。

在该代码中的任何地方我都没有调用textview.performClick。

任何人都可以解释吗?

onPindViewhOlder的

代码块

 @Override
public void onBindViewHolder(ViewHolder holder, final int position) {


    holder.txt_passeger.setText(passengerArrayList.get(position).getPaxFullName());
    holder.txt_passeger.setTag("Unselected");
    holder.img_tick_mark.setVisibility(View.GONE);
    holder.txt_passeger.setTextColor(Color.BLACK);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        holder.lyt_background.setBackground(context.getResources().getDrawable(R.color.color_white));
    }
    holder.txt_seat_number.setVisibility(View.GONE);


    //set seat number from map
    //HashMap<String, HashMap<Integer, PassengerArray>> mapRoutePaxSeat (static data)
    HashMap<Integer, PassengerArray> mapPaxSeat = FlightSeatViewFragment.mapIfsLegPaxSeat.get(ifsIndex + "_" + legIndex);

    if (mapPaxSeat != null) {
        //here the mapIfsLegPaxSeat is filled with ifs+legIndex that why mapPaxSeat is not null

        Set<Integer> intSetPaxKey = mapPaxSeat.keySet();

        Iterator<Integer> intIteratorPaxKey = intSetPaxKey.iterator();

        while (intIteratorPaxKey.hasNext()) {
            Integer intPaxId = intIteratorPaxKey.next();
            PassengerArray passengerArrayObj = mapPaxSeat.get(intPaxId);

            if (passengerArrayObj.getPaxId() == passengerArrayList.get(position).getPaxId() && passengerArrayObj.getSeatSelectedNumber() != null) {
                holder.txt_seat_number.setText(passengerArrayObj.getSeatSelectedNumber());
                holder.txt_seat_number.setVisibility(View.VISIBLE);
                break;
            } else {
                holder.txt_seat_number.setVisibility(View.GONE);
            }
        }
    }
    holder.txt_passeger.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            View view = (View) v.getParent();   // Linear layout
            LinearLayout txtPaxParentLayout = (LinearLayout) view.getParent();  //Linear lay row

            try {

                // unselect if any previous view is selected already

                RecyclerView recycler = (RecyclerView) txtPaxParentLayout.getParent();
                for (int i = 0; i < recycler.getChildCount(); i++) {
                    ViewHolder viewHolder = (ViewHolder) recycler.findViewHolderForAdapterPosition(i);

                    if (viewHolder.txt_passeger.getTag().toString().equals("Selected")) {

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            viewHolder.lyt_background.setBackground(context.getResources().getDrawable(R.color.color_white));
                        }

                        viewHolder.txt_passeger.setTag("Unselected");
                        Utils.DEBUG(TAG, "Recycler block Selection status: " + viewHolder.txt_passeger.getText() + ":" + viewHolder.txt_passeger.getTag());
                        viewHolder.txt_passeger.setTextColor(Color.BLACK);
                    }
                }
            } catch (Exception e) {
            }


            /**below code is to set only first passenger item selected*/

            OTTextView txtv = v.findViewById(R.id.txt_passeger);
            LinearLayout lyt_background = (LinearLayout) v.getParent();

            if (txtv.getTag().toString().equals("Unselected")) {
                // if passenger is selected

                txtv.setTag("Selected");
                txtv.setTextColor(Color.WHITE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    lyt_background.setBackground(context.getResources().getDrawable(R.color.color_blue));
                }


                passengerArrayList.get(position).setAllowedToSelectSeat(true);                  //updating passengerArrayList by updating passenger status to select seat when the name is clicked

                Utils.DEBUG(TAG, "Selection status: " + txtv.getText().toString() + ":" + txtv.getTag());
                Utils.DEBUG(TAG, "Position: " + position + "; Current Passenger Object: " + new Gson().toJson(passengerArrayList.get(position)));
            } else {
                // if passenger unselected

                txtv.setTag("Unselected");                                                      //why make it unselected if already selected
                txtv.setTextColor(Color.BLACK);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    lyt_background.setBackground(context.getResources().getDrawable(R.color.color_white));
                }
                passengerArrayList.get(position).setAllowedToSelectSeat(false);                 //why make it false if user clicks it to again select seat

                Utils.DEBUG(TAG, "Selection status: " + txtv.getText().toString() + ":" + txtv.getTag());
                Utils.DEBUG(TAG, "Position: " + position + "; Current Passenger Object: " + new Gson().toJson(passengerArrayList.get(position)));

            }

            l.onClickRecyclerItem(v, passengerArrayList.get(position));
        }
    });
}   

我的商品xml代码如下:

    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/lyt_background"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <com.optiontown.app.view.customview.OTTextView
        android:id="@+id/txt_passeger"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:paddingLeft="@dimen/dp_10"
        android:paddingRight="@dimen/dp_10"
        android:paddingTop="@dimen/dp_5"
        android:paddingBottom="@dimen/dp_5"
        android:textSize="@dimen/size_font_11"
        android:textColor="@color/caldroid_black"
        android:text="BLR>GOI" />
        <ImageView
        android:id="@+id/img_tick_mark"
        android:layout_width="0dp"
        android:layout_height="25dp"
        android:layout_weight="1"
        android:layout_gravity="right"
        android:src="@drawable/tick_green_icon"/>
        <com.optiontown.app.view.customview.OTTextView
        android:id="@+id/txt_seat_number"
        android:layout_width="0dp"
        android:layout_height="@dimen/dp_27"
        android:layout_weight="1"
        android:text="07A"
        android:gravity="center"
        android:textColor="@color/color_font_green"
        android:textStyle="bold"/>
    </LinearLayout>
</LinearLayout>

谢谢。

0 个答案:

没有答案