[英]Android ViewPager find Fragment for findviewbyid
如果您想知道,不,我沒有錯過與我的問題類似的數百個線程。 但是,沒有一個對我有用。 我想在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)獲取視圖/片段?
您可以從fragmentActivity調用...
public Object instantiateItem (ViewGroup container, int position)
這是您的適配器的一種方法。 您將在該位置取回片段。 一旦有了片段,就可以在其中調用任何公共方法。 該容器將是實際的viewPager
這是此功能的文檔:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.