android – 处理PagerTitleStrip点击兼容基于片段的ViewPager

前端之家收集整理的这篇文章主要介绍了android – 处理PagerTitleStrip点击兼容基于片段的ViewPager前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个兼容性 ViewPager与嵌入式 PagerTitleStrip元素,它工作正常,但点击PagerTitleStrip不要按预期切换到相应的视图.我已经查看了文档,但是该widget没有任何内置的方法来附加onClickListeners到PagerTitleStrip对象.这似乎有点奇怪,因为这可能是大多数人会期望的.

所以问题是如何使标题栏可点击?

我也试图找出是否有办法在选定的标题栏下显示一个粗调的下划线,而无需添加一个TabHost,如果可能的话.

这是代码

主要布局view_pager.xml:

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <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" >

        <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" />
    </android.support.v4.view.ViewPager>

</LinearLayout>

进入ViewPager的两个片段布局:

fragment_connect.xml(第二个是相同的,所以不包括)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <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" />
</LinearLayout>

主要活动类StandardViewPagerActivity.java:

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<Fragment> fragments = new Vector<Fragment>();
        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);
    }
}

适配器类MyPagerAdapter.java:

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<Fragment> fragments;

    public MyPagerAdapter(FragmentManager supportFragmentManager,List<Fragment> 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);
    }
}

最后,两个相同的Fragment实现,只包括第一个ConnectFragment.java.

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;
        }
}

解决方法

So the question is how would I make the title strip clickable?

你分叉它,并使您可以点击的自己的实现.或者,您切换到可以点击的PagerTabStrip.或者,您切换到ViewPagerIndicator指标之一,恰好是可点击的,并且看起来像您喜欢的.

报价the documentation for PagerTitleStrip

PagerTitleStrip is a non-interactive indicator of the current,next,and prevIoUs pages of a ViewPager… For an interactive indicator,see PagerTabStrip.

原文链接:https://www.f2er.com/android/313286.html

猜你在找的Android相关文章