1

如果您想知道,不,我没有错过与我的问题类似的数百个线程。但是,没有一个对我有用。我想在 FragmentActivity 中使用 findviewbyid。我基本上使用示例 android 实现和在线提出的解决方案来查找片段。

主要.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    >

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

</RelativeLayout>

Main_control.xml:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_control1"
    >
    <ProgressBar
        android:id="@+id/rollProgressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/armDisarm"
        android:layout_marginLeft="23dp"
        android:layout_toRightOf="@+id/pitchProgressbar"
        android:max="96"
        android:minHeight="10dp"
        android:minWidth="96dp" />

//There were more controls here, but they should not affect the question
    </RelativeLayout>

MainActivity.java:

package com.multiwii.multiwiiremote;

import com.multiwii.multiwiiremote.MyFragmentPagerAdapter;
//Other imports are here

public class MainActivity extends FragmentActivity implements SensorEventListener {
        ProgressBar rollProgressbar;
    MyFragment b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

/** Getting a reference to the ViewPager defined the layout file */        
        ViewPager pager = (ViewPager) findViewById(R.id.pager);

        /** Getting fragment manager */
        FragmentManager fm = getSupportFragmentManager();
//  THIS IS WHAT'S NOT WORKING
          rollProgressbar = (ProgressBar) fm.findFragmentById(R.id.main_control1).getView().findViewById(R.id.pitchProgressbar); 
//  THIS IS WHAT'S NOT WORKING

        /** Instantiating FragmentPagerAdapter */
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

        /** Setting the pagerAdapter to the pager object */
        pager.setAdapter(pagerAdapter);
}
}

MyFragment.java

package com.multiwii.multiwiiremote;

public class MyFragment extends Fragment {
    int mCurrentPage;
    View v = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /** Getting the arguments to the Bundle object */
        Bundle data = getArguments();

        /** Getting integer data of the key current_page from the bundle */
        mCurrentPage = data.getInt("current_page", 0);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        switch (mCurrentPage) {
        case 1:
            v = inflater.inflate(R.layout.main_control, container, false);
            break;
        case 2:
            v = inflater.inflate(R.layout.graph, container, false);
            break;
        case 3:
            v = inflater.inflate(R.layout.settings, container, false);
            break;
        }
        return v;
    }
//  public void setRollProgressBar(int value) {
//      if (v == null || mCurrentPage != 1) return;
//      ProgressBar bar = (ProgressBar) v.findViewById(R.id.rollProgressbar);
//      if(bar != null)
//      bar.setProgress(value);
//  }

最后的问题是:如何从 FragmentActivity(MainActivity) 获取视图/片段?

4

1 回答 1

2

从 fragmentActivity 你可以调用...

public Object instantiateItem (ViewGroup container, int position)

这是您的适配器的一种方法。你会在这个位置取回片段。一旦你有了片段,你就可以在其中调用任何公共方法。容器将是实际的 viewPager

这是此功能的文档:

http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html#instantiateItem(android.view.ViewGroup , int)

于 2013-07-23T00:04:04.087 回答