我正在使用ViewPager
,FragmentStatePagerAdapter
和Fragment
。 如何加載每個片段的功能(第一次初始化,按鈕處理程序)?處理片段內部的查看點擊而非活動
我設法使按鈕工作,但我不得不移動主要活動中的所有句柄。我如何保持片段中的功能?
這裏是我的適配器:
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new DemoObjectFragment();
Bundle args = new Bundle();
// Our object is just an integer :-P
args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
}
return "Tab " + (position + 1);
}
}
這是該片段:
public class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
int mNum;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// The last two arguments ensure LayoutParams are inflated
// properly.
View rootView = inflater.inflate(R.layout.activity_page, container, false);
//((TextView) rootView.findViewById(android.R.id.text1)).setText(
// Integer.toString(args.getInt(ARG_OBJECT)));
return rootView;
}
/**
* When creating, retrieve this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
更新: 這裏是activity_page.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/panel_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="#fffaffff"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".GuessActivity">
<TextView
android:id="@+id/text1"
android:text="@string/lorem_ipsum"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_validate"
android:layout_width="wrap_content"
android:layout_height="@dimen/guess_height"
android:layout_alignParentRight="true"
android:onClick="validateGuess"
android:text="@string/button_validate" />
<TextView
android:id="@+id/text2"
android:text="@string/guess_success"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#008000"
android:visibility="gone"/>
</LinearLayout>
</FrameLayout>
您的代碼中沒有任何對按鈕的引用,也沒有任何活動中的按鈕處理程序。 – ataulm 2014-10-04 11:44:49
按鈕和處理程序位於activity_page.xaml中。我也會補充一點。 1秒 – alexm92 2014-10-04 11:46:30
如果您的處理程序使用'onClick'屬性進行鏈接,則您無法在片段中使用相應的方法。您需要從片段中獲取對按鈕的引用,並使用類似'myButton.setOnClickListener(new View.OnClickListener(){...})的方式爲它分配一個onClickListener' – ataulm 2014-10-04 11:51:40