title: Handling PagerTitleStrip click in compatibiliy Fragment-based ViewPager
body: <p>I have a compatibility <a href="http://developer.android.com/reference/android/support/v4/view/ViewPager.html" rel="noreferrer">ViewPager</a> with an embedded <a href="http://developer.android.com/reference/android/support/v4/view/PagerTitleStrip.html" rel="noreferrer">PagerTitleStrip</a> element and it works fine but clicks on the PagerTitleStrip do not switch to the corresponding view as expected. I have looked through the docs but the widget does not have any built-in methods for attaching onClickListeners to the PagerTitleStrip object. This seems a bit weird since that is probably what most people would expect.</p>
  
  <p>So the question is how would I make the title strip clickable? </p>
  
  <p>I am also trying to figure out if there is a way to show a thick underline under the selected title strip without adding a TabHost if at all possible.</p>
  
  <p>Here is the code:</p>
  
  <p>Main layout view_pager.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
  
      &lt;android.support.v4.view.ViewPager
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/my_view_pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent" &gt;
  
          &lt;android.support.v4.view.PagerTitleStrip
              android:id="@+id/pager_title_strip"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="top"
              android:background="#ccc"
              android:textColor="#fff" /&gt;
      &lt;/android.support.v4.view.ViewPager&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The two fragment layouts that go into the ViewPager:</p>
  
  <p>fragment_connect.xml (the second is identical so not included)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:id="@+id/text_view_connect"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="connect"
          android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The main activity class StandardViewPagerActivity.java:</p>
  
  <pre><code>package com.example.standardviewpager;
  
  import java.util.List;
  import java.util.Vector;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.view.PagerTitleStrip;
  import android.support.v4.view.ViewPager;
  import android.view.Menu;
  
  public class StandardViewPagerActivity extends FragmentActivity {
  
      private MyPagerAdapter pagerAdapter;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.view_pager);
          ViewPager myPager = (ViewPager) findViewById(R.id.my_view_pager);
          PagerTitleStrip pagerStrip = (PagerTitleStrip) findViewById(R.id.pager_title_strip);
          // TODO: add underline for selected title strip
          myPager.setAdapter(pagerAdapter);
          myPager.setCurrentItem(0);
          initialisePaging();
      }
  
      private void initialisePaging() {
  
          List&lt;Fragment&gt; fragments = new Vector&lt;Fragment&gt;();
          fragments.add(Fragment.instantiate(this, ConnectFragment.class.getName()));
          fragments.add(Fragment.instantiate(this, ProfilesFragment.class.getName()));
          this.pagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
          ViewPager pager = (ViewPager) super.findViewById(R.id.my_view_pager);
          pager.setAdapter(this.pagerAdapter);
      }
  }
  </code></pre>
  
  <p>The adapter class MyPagerAdapter.java:</p>
  
  <pre><code>package com.example.standardviewpager;
  
  import java.util.List;
  
  import android.os.Parcelable;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.view.LayoutInflater;
  import android.view.View;
  
  public class MyPagerAdapter extends FragmentPagerAdapter {
  
      private List&lt;Fragment&gt; fragments;
  
      public MyPagerAdapter(FragmentManager supportFragmentManager, List&lt;Fragment&gt; fragments) {
          super(supportFragmentManager);
          this.fragments = fragments;
      }
  
      @Override
      public int getCount() {
          return this.fragments.size();
      }
  
      @Override
      public void destroyItem(View arg0, int arg1, Object arg2) {
          ((ViewPager) arg0).removeView((View) arg2);
      }
  
      @Override
      public Parcelable saveState() {
          return null;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return String.format("Title %d", position);
      }
  
      @Override
      public Fragment getItem(int position) {
              return this.fragments.get(position);
      }
  }
  </code></pre>
  
  <p>And, finally, the two identical Fragment implementations, including only the first - ConnectFragment.java. </p>
  
  <pre><code>package com.example.standardviewpager;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  
  public class ConnectFragment extends Fragment {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.fragment_connect, container, false);
          TextView textView = (TextView) layout.findViewById(R.id.text_view_connect);
          textView.setText("onCreateView() Connect");
          return (View)layout;
          }
  }
  </code></pre>