Reputation: 13
In case you were wondering, no I didn't miss the hundreds of threads similar to my question. However, none worked for me. I want to use findviewbyid in the FragmentActivity. I am basically using the sample android implementation and a solution proposed online to find the fragment.
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);
// }
Finally the question: How do I get the view/fragment from the FragmentActivity(MainActivity)?
Upvotes: 1
Views: 5583
Reputation: 5666
From the fragmentActivity you can call...
public Object instantiateItem (ViewGroup container, int position)
It is a method of your adapter. And you will get back the fragment in this position. Once you have the fragment then you can call any public method in it. The container would be the actual viewPager
Here is the documentation for this function:
Upvotes: 2