【问题标题】:Get ViewPager views from MainActivity从 MainActivity 获取 ViewPager 视图
【发布时间】:2013-12-30 13:52:51
【问题描述】:

我正在开发一个应用程序,它使用 ViewPager 和两个滑动视图(first_page.xml 和 second_page.xml)并有一个活动(activity_main.xml)。

当我在主活动类中尝试使用findViewById 访问TextView(位于first_page.xml 中)时,找不到它。

这是我的 activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

这是我的 first_page.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/someText"
        android:text="example"
        android:textSize="42sp" >
    </TextView>
</LinearLayout>

这是我的 customPagerAdapter

public class CustomPagerAdapter extends PagerAdapter {

    public Object instantiateItem(View collection, int position) {
    LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    int resId = 0;
    switch (position) {
    case 0: {
        resId = R.layout.first_page;
        break;
    }
    case 1: {
        resId = R.layout.second_page;
        break;
    }
    }
    View view = inflater.inflate(resId, null);
    ((ViewPager) collection).addView(view, 0);
    return view;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
    return null;
}

@Override
public int getCount() {
    return 2;
}
}

这是我的MainActivity,我想在其中获取 first_page 布局,找到名为 someText 的 TextView,并设置新的自定义字体。

public class MainActivity extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Create and set adapter
    CustomPagerAdapter adapter = new CustomPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.pager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);

    /*
     *  Here i want to set a new custom font but it seems the someText can't
     *  be found because the Activity searches for it in the 
     *  layout.activity_main and not in the layout.first_page
    */
    TextView textView = (TextView) findViewById(R.id.someText);
    Typeface font = Typeface.createFromAsset(getAssets(), "roboto_thin.ttf");  
    textView.setTypeface(font);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

我为这个问题苦苦挣扎了两天。

有什么建议吗?

【问题讨论】:

    标签: android views android-viewpager swipeview main-activity


    【解决方案1】:

    textview被封装在fragment的容器中。如果您获得对正确片段的引用并获得对容器的引用,您可以调用 findViewById(R.id.someid)。


    示例代码:

    // TODO cast checks
    int item = mViewPager.getCurrentItem();
    Fragment frag = (Fragment) mPagerAdapter.getItem(item);
    View container = frag.getView();
    TextView searchTextView = (TextView) container.findViewById(R.id.someid);
    

    或者是你的情况:

    // TODO add null + cast checks
    TextView textView = (TextView) ((Fragment)adapter.getItem(0)).getView().findViewById(R.id.someText);
    

    【讨论】:

    • 感谢您的回复!但是我应该在 customPagerAdapter 类的 getItem() 方法中添加什么?
    • 没问题,如果答案帮助您解决了问题:您应该接受答案并可能对帖子进行投票;)
    • 我对您的解决方案有疑问。我不明白如何在 customPagerAdapter 类中实现 getItem() 方法。你能帮我吗?
    • 我已经添加了替换的 onCreate 方法。如果这不起作用,请告诉我。您还可以使用另一种方法,在创建时将参数传递给片段,并让片段本身处理字体。 (这将是一个更好的设计方案)
    • 我在 onCreate 方法中复制了您的代码,但 Eclipse 发现两个错误并提出两件事:将 (Object) cast 添加到 'adapter.getItem(0)' 并更改 getView() cast跨度>
    猜你喜欢
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多