ご参考までに、私の質問に似た何百ものスレッドを見逃さなかったわけではありません。しかし、どれも私にとってはうまくいきませんでした。FragmentActivity で findviewbyid を使用したいと考えています。私は基本的に、サンプルの Android 実装と、フラグメントを見つけるためにオンラインで提案されたソリューションを使用しています。
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"
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) からビュー/フラグメントを取得するにはどうすればよいですか?