title: custom view Click listeners, not working for current tab
body: <p>I have done add and remove tabs, with custom tab view, which works fine. I have the same view for all tabs, added in viewpager. I have click listeners for some view in the layout, loaded in viewpager. But click listener worked on last added tab, not working on other tabs. </p>
  
  <p><strong>Activity</strong></p>
  
  <pre><code>public class Test extends BaseActivity implements View.OnClickListener {
      private TextView flatRateTextView, timeMaterialTextView,
              totalWorkOrderValueTextView;
      private LinearLayout bidTimeMaterialParentLayout, bidTaxTotalParentLayout;
      boolean isFlatRateClicked, isTimeMaterialClicked;
      private ViewPager pager = null;
      private MainPagerAdapter pagerAdapter = null;
      TabLayout tabLayout;
      int i;
  
      private void initViews() {
          i++;
          pagerAdapter = new MainPagerAdapter();
          pager = (ViewPager) findViewById(R.id.viewpager);
          pager.setAdapter(pagerAdapter);
          appendViewToViewPager(0);
          tabLayout = (TabLayout) findViewById(R.id.tabs);
          tabLayout.setSelectedTabIndicator(null);
          LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabLayout.getLayoutParams();
          params.setMargins(widthSpace, widthSpace, widthSpace, 0);
          tabLayout.setLayoutParams(params);
          // tabLayout.setC(v0);
          tabLayout.setupWithViewPager(pager);
          setupTabIcons();
          //Dynamic job Bit &amp; Material Start
          TextView totalWorkOrderTextView = findViewById(R.id.totalWorkOrderTextView);
          totalWorkOrderValueTextView = findViewById(R.id.totalWorkOrderValueTextView);
          TextView addJobTextView = findViewById(R.id.addJobTextView);
          TextView submitTextView = findViewById(R.id.submitTextView);
          submitTextView.setOnClickListener(this);
          addJobTextView.setOnClickListener(this);
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.test);
  
          //  ... do other initialization, such as create an ActionBar ...
          initViews();
  
          tabLayout.addOnTabSelectedListener(
                  new TabLayout.ViewPagerOnTabSelectedListener(pager) {
                      @Override
                      public void onTabSelected(TabLayout.Tab tab) {
                          super.onTabSelected(tab);
                          //  updateTabs();
                          int numTab = tab.getPosition();
                          Log.e("numTab &gt;", numTab + "");
                          pager.setCurrentItem(numTab);
  
                      }
                  });
  
      }
  
      private void setupTabIcons() {
  
  
          for (int j = 0; j &lt; i; j++) {
              Log.e("j val&gt;&gt;", j + "");
              View rootView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.job_dynamic_header_layout, null);
              TextView tabOne = rootView.findViewById(R.id.jobHeaderTextView);
              int tmp = j + 1;
              tabOne.setText("Job" + tmp);
              ImageView closeImageView = rootView.findViewById(R.id.closeImageView);
              closeImageView.setId(tmp);
              closeImageView.setOnClickListener(onClickListener);
              tabLayout.getTabAt(j).setCustomView(rootView);
          }
      }
  
      View.OnClickListener onClickListener = new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              switch (v.getId()) {
                  default:
                      int tabIdToRemove = v.getId();
                      tabIdToRemove = tabIdToRemove - 1;
                      if (tabIdToRemove &lt; 1) {
                          Toast.makeText(Test.this, "Couldn't remove first tab", Toast.LENGTH_SHORT).show();
                      } else {
                          i--;
                          Toast.makeText(Test.this, "Removed tab" + tabIdToRemove, Toast.LENGTH_SHORT).show();
                          pagerAdapter.removeView(pager, tabIdToRemove);
                          pagerAdapter.notifyDataSetChanged();
                          setupTabIcons();
                      }
                      break;
  
              }
          }
      };
  
  
      private void appendViewToViewPager(int position) {
          LayoutInflater inflater = Test.this.getLayoutInflater();
          NestedScrollView v = (NestedScrollView) inflater.inflate(R.layout.vendor_submit_bit_view_pager_layout, null);
          flatRateTextView = v.findViewById(R.id.flatRateTextView);
          timeMaterialTextView = v.findViewById(R.id.timeMaterialTextView);
          bidTimeMaterialParentLayout = v.findViewById(R.id.bidTimeMaterialParentLayout);
          bidTaxTotalParentLayout = v.findViewById(R.id.bidTaxTotalParentLayout);
          flatRateTextView.setTag(position);
          timeMaterialTextView.setTag(position);
  
          flatRateTextView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  isFlatRateClicked = true;
                  isTimeMaterialClicked = false;
                  bidTimeMaterialParentLayout.setVisibility(View.GONE);
                  bidTaxTotalParentLayout.setVisibility(View.VISIBLE);
                  flatRateTextView.setBackground(ContextCompat.getDrawable(Test.this, R.drawable.bottom_red_line));
                  timeMaterialTextView.setBackground(null);
              }
          });
          timeMaterialTextView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  isFlatRateClicked = false;
                  isTimeMaterialClicked = true;
                  totalWorkOrderValueTextView.setText("$0");
                  bidTimeMaterialParentLayout.setVisibility(View.VISIBLE);
                  bidTaxTotalParentLayout.setVisibility(View.GONE);
                  timeMaterialTextView.setBackground(ContextCompat.getDrawable(Test.this, R.drawable.bottom_red_line));
                  flatRateTextView.setBackground(null);
              }
          });
  
  
          pagerAdapter.addView(v, position);
          pagerAdapter.notifyDataSetChanged();
      }
  
      @Override
      public void onClick(View view) {
          switch (view.getId()) {
              case R.id.backImageView:
                  Intent newIntent = new Intent(Test.this, VendorHomeActivity.class);
                  startActivity(newIntent);
                  break;
   case R.id.addJobTextView:
                  i++;
  
                  int tmp = i - 1;
                  appendViewToViewPager(tmp);
                  setupTabIcons();
                  break;
              case R.id.submitTextView:
                  break;
          }
      }
  }
  </code></pre>
  
  <p><strong>test.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v4.widget.DrawerLayout 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="match_parent"
      android:background="@color/white_color"
      android:fitsSystemWindows="true"&gt;
  
  
      &lt;RelativeLayout
  
          android:layout_width="match_parent"
          android:layout_height="wrap_content"&gt;
  
  
          &lt;RelativeLayout
              android:id="@+id/dynamicJobLayout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_above="@+id/layoutTotalWorkOrder"
            &gt;
  
  
              &lt;android.support.design.widget.AppBarLayout
                  android:id="@+id/layoutTab"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"&gt;
  
  
                  &lt;android.support.design.widget.TabLayout
                      android:id="@+id/tabs"
                      android:layout_width="match_parent"
                      android:layout_height="@dimen/custom_tab_layout_height"
                      android:background="@android:color/transparent"
                      android:state_selected="false"
                      app:tabBackground="@drawable/job_tab_selector"
                      app:tabGravity="fill"
                      app:tabMode="scrollable" /&gt;
              &lt;/android.support.design.widget.AppBarLayout&gt;
  
              &lt;android.support.v4.view.ViewPager
                  android:id="@+id/viewpager"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="@color/white_color"
  
                  /&gt;
  
  
          &lt;/RelativeLayout&gt;
  
          &lt;LinearLayout
              android:id="@+id/layoutTotalWorkOrder"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_above="@+id/layBottom"
              android:gravity="center_vertical"
              android:orientation="horizontal"&gt;
  
              &lt;TextView
                  android:id="@+id/totalWorkOrderTextView"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:gravity="center|end"
                  android:text="@string/total_work_order_cost"
                  android:textColor="@color/dark_grey_color"
                  android:textSize="@dimen/text_size_16"
                  /&gt;
  
              &lt;TextView
                  android:id="@+id/totalWorkOrderValueTextView"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:gravity="center"
                  android:text="$0"
                  android:textColor="@color/red_color"
                  android:textSize="@dimen/text_size_16"
                  /&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:id="@+id/layBottom"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
  
              android:layout_alignParentBottom="true"
  
              android:orientation="horizontal"&gt;
  
              &lt;TextView
                  android:id="@+id/addJobTextView"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:background="@drawable/grey_cornor_stroke_bg"
                  android:gravity="center"
                  android:text="@string/add_job_label"
                  android:textColor="@color/dark_grey_color"
                  android:textSize="@dimen/text_size_14"
                 /&gt;
  
  
              &lt;TextView
                  android:id="@+id/submitTextView"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:background="@drawable/corner_5_dp_red_bg"
                  android:gravity="center"
                  android:text="@string/submit_label"
                  android:textColor="@color/white_color"
                  android:textSize="@dimen/text_size_14"
                 /&gt;
  
          &lt;/LinearLayout&gt;
      &lt;/RelativeLayout&gt;
  
  
      &lt;android.support.design.widget.NavigationView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="start"&gt;
  
          &lt;include layout="@layout/vendor_job_work_order_view" /&gt;
  
      &lt;/android.support.design.widget.NavigationView&gt;
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p><strong>job_dynamic_header_layout.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:orientation="horizontal"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/headerLay"
      android:gravity="center_vertical"
     &gt;
  
      &lt;TextView
          android:id="@+id/jobHeaderTextView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="@dimen/text_size_14"
          android:textColor="@color/white_color"
          android:text="Job"/&gt;
  
      &lt;ImageView
          android:id="@+id/closeImageView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/white_cross_icon"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>vendor_submit_bit_view_pager_layout.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;LinearLayout
      android:id="@+id/jobDescDetailLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="@dimen/custom_tab_layout_height"
      android:background="@drawable/light_blue_white_cornor_bg"
      android:orientation="vertical"&gt;
  
  
      &lt;LinearLayout
          android:id="@+id/flatRateParentLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"&gt;
  
          &lt;TextView
              android:id="@+id/flatRateTextView"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:background="@drawable/bottom_red_line"
              android:gravity="center"
              android:text="@string/flat_rate_label"
              android:textColor="@color/black_color"
              android:textSize="@dimen/text_size_small_12" /&gt;
  
          &lt;TextView
              android:id="@+id/timeMaterialTextView"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:gravity="center"
              android:singleLine="true"
              android:text="@string/time_material_label"
              android:textColor="@color/dark_grey_color"
              android:textSize="@dimen/text_size_small_12" /&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:id="@+id/bidTaxTotalParentLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@drawable/white_10_cornor_bg"
          android:orientation="vertical"&gt;
  
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:id="@+id/bidTimeMaterialParentLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@drawable/white_10_cornor_bg"
          android:orientation="vertical"&gt;
  
  
          &lt;LinearLayout
              android:id="@+id/dynamicMaterialLayout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"&gt;
  
  
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"&gt;
  
              &lt;TextView
                  android:id="@+id/materialLabelTextView"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="@string/materials_label"
                  android:textColor="@color/dark_grey_color"
                  android:textSize="@dimen/text_size_16" /&gt;
  
              &lt;TextView
                  android:id="@+id/addMaterialTextView"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:text="@string/add_material_label"
                  android:textColor="@color/red_color"
                  android:textSize="@dimen/text_size_16" /&gt;
  
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>