title: Android TabLayout and ViewPager get position click item
body: <p>Good evening my friend, </p>
  
  <p>I did this tutorial <a href="http://www.journaldev.com/12958/android-tablayout-viewpager#comment-37790" rel="nofollow noreferrer">http://www.journaldev.com/12958/android-tablayout-viewpager#comment-37790</a> and everythink was fine. 
  Now i want add the function on click to listView and get the position when i click on items. Please help me.</p>
  
  <p>This is the factivity_main.xml</p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:context="com.journaldev.tablayoutviewpager.MainActivity"&gt;
  
      &lt;android.support.design.widget.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="@style/AppTheme.AppBarOverlay"&gt;
  
          &lt;android.support.v7.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="?attr/colorPrimary"
              app:layout_scrollFlags="scroll|enterAlways"
              app:popupTheme="@style/AppTheme.PopupOverlay" /&gt;
  
          &lt;android.support.design.widget.TabLayout
              android:id="@+id/tabs"
              style="@style/MyStyle"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:tabGravity="fill"
              app:tabMode="fixed" /&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"
          app:layout_behavior="@string/appbar_scrolling_view_behavior" /&gt;
  
  
      &lt;android.support.design.widget.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="bottom|end"
          android:layout_margin="@dimen/fab_margin"
          android:src="@android:drawable/ic_dialog_email" /&gt;
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p>this is the  ViewPager in the MainActivity</p>
  
  <pre><code>public class ViewPagerAdapter extends FragmentPagerAdapter {
  
      public ViewPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
          Fragment fragment = null;
          if (position == 0)
          {
              fragment = new FragmentA();
          }
          else if (position == 1)
          {
              fragment = new FragmentB();
          }
          else if (position == 2)
          {
              fragment = new FragmentC();
          }
          return fragment;
      }
  
      @Override
      public int getCount() {
          return 3;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          String title = null;
          if (position == 0)
          {
              title = "Tab-1";
          }
          else if (position == 1)
          {
              title = "Tab-2";
          }
          else if (position == 2)
          {
              title = "Tab-3";
          }
          return title;
      }
  }
  </code></pre>
  
  <p>this is fragment_list.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;ListView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/list"/&gt;
  </code></pre>
  
  <p>this is FragmentA</p>
  
  <pre><code>public class FragmentA extends Fragment {
  
  
      ListView list;
  
  
         public FragmentA() {
          }
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              View view = inflater.inflate(R.layout.fragment, container, false);
  
              list = (ListView) view.findViewById(R.id.list);
              ArrayList stringList= new ArrayList();
  
              stringList.add("Item 1A");
              stringList.add("Item 1B");
              stringList.add("Item 1C");
              stringList.add("Item 1D");
              stringList.add("Item 1E");
              stringList.add("Item 1F");
  
              CustomAdapter adapter = new CustomAdapter(stringList,getActivity());
              list.setAdapter(adapter);
  
              return view;
          }
      }
  </code></pre>
  
  <p>This is CustomAdapter.java </p>
  
  <pre><code>public class CustomAdapter extends ArrayAdapter {
  
      private ArrayList dataSet;
      Context mContext;
  
      // View lookup cache
      private static class ViewHolder {
          TextView txtName;
  
      }
  
      public CustomAdapter(ArrayList data, Context context) {
          super(context, R.layout.row_item, data);
          this.dataSet = data;
          this.mContext = context;
  
      }
  
      @Nullable
      @Override
      public String getItem(int position) {
          return dataSet.get(position);
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          ViewHolder viewHolder; // view lookup cache stored in tag
  
          if (convertView == null) {
  
              viewHolder = new ViewHolder();
              LayoutInflater inflater = LayoutInflater.from(getContext());
              convertView = inflater.inflate(R.layout.row_item, parent, false);
              viewHolder.txtName = (TextView) convertView.findViewById(R.id.name);
              convertView.setTag(viewHolder);
          } else {
              viewHolder = (ViewHolder) convertView.getTag();
          }
  
          viewHolder.txtName.setText(getItem(position));
          // Return the completed view to render on screen
          return convertView;
      }
  }
  </code></pre>
  
  <p>The MainActivity.java class </p>
  
  <pre><code>public class CustomAdapter extends ArrayAdapter {
  
      private ArrayList dataSet;
      Context mContext;
  
      // View lookup cache
      private static class ViewHolder {
          TextView txtName;
  
      }
  
      public CustomAdapter(ArrayList data, Context context) {
          super(context, R.layout.row_item, data);
          this.dataSet = data;
          this.mContext = context;
  
      }
  
      @Nullable
      @Override
      public String getItem(int position) {
          return dataSet.get(position);
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          ViewHolder viewHolder; // view lookup cache stored in tag
  
          if (convertView == null) {
  
              viewHolder = new ViewHolder();
              LayoutInflater inflater = LayoutInflater.from(getContext());
              convertView = inflater.inflate(R.layout.row_item, parent, false);
              viewHolder.txtName = (TextView) convertView.findViewById(R.id.name);
              convertView.setTag(viewHolder);
          } else {
              viewHolder = (ViewHolder) convertView.getTag();
          }
  
          viewHolder.txtName.setText(getItem(position));
          // Return the completed view to render on screen
          return convertView;
      }
  }
  </code></pre>