gpt4 book ai didi

android - 选项卡式 Activity 下的子 fragment 不起作用

转载 作者:行者123 更新时间:2023-11-29 01:13:12 29 4
gpt4 key购买 nike

我正在处理选项卡式 Activity 。在每日提醒/特别提醒上,保存按钮不起作用。我已经提供了正确的 ID、链接和 OnClickListener。但它就是行不通。

Reminders Activity is a simple activity with List ReminderTabActivity s Tabbed Activity此处是调用 fragment 的 ReminderTabActivity 的 onCreateView。

public class ReminderTabActivity extends AppCompatActivity implements DailyReminderFragment.OnMessageSetListener {

/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;

String type = "";

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

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);

Intent intent = getIntent();
if(intent.hasExtra("key")) {
String key = intent.getStringExtra("key");
type = intent.getStringExtra("etype");
Log.d("mKey", key);
if(type.equals("special")) {
mViewPager.setCurrentItem(2);
SpecialReminderFragment srf = new SpecialReminderFragment();
Bundle bundle = new Bundle();
bundle.putString("key", key);
srf.setArguments(bundle);
//srf.updateSpecial(key);
}
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_reminder_tab, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void setMessage(String message) {
Log.d("messageSet", message);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {
}

/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(getArguments().getInt(ARG_SECTION_NUMBER)==1) {
View rootView = inflater.inflate(R.layout.fragment_daily_reminder, container, false);
return rootView;
}
else if(getArguments().getInt(ARG_SECTION_NUMBER)==2) {
View rootView = inflater.inflate(R.layout.fragment_special_reminder, container, false);
return rootView;
}
else {
View rootView = inflater.inflate(R.layout.fragment_reminder_tab, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}

@Override
public int getCount() {
// Show 2 total pages.
return 2;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "DAILY REMINDER";
case 1:
return "SPECIAL REMINDER";
}
return null;
}
}
}





@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("kedar", "ONLOAD");
rootView = inflater.inflate(R.layout.fragment_daily_reminder, container, false);
mAuth = FirebaseAuth.getInstance();
btn_save = (Button) rootView.findViewById(R.id.btn_save);
etTitle = (EditText) rootView.findViewById(R.id.etTitle);
picker = (TimePicker) rootView.findViewById(R.id.timePicker);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");

btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveReminder();
OnMessageSetListener.setMessage("test");
}
});

return rootView;
}

这是布局-`

<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etTitle"
android:layout_below="@+id/textView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="Title"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />

<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etTitle"
android:id="@+id/timePicker"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />

<Button
android:text="SAVE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_save"
android:layout_below="@+id/timePicker"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
/>

</RelativeLayout>

`

我可以看到布局,但它不在控制台中显示 Log.d 消息,onClickListener 按钮也不起作用。
你们认为我在这里做错了什么?

最佳答案

哪个 fragment 有你在帖子最后添加的 oncreateview 方法?。我认为您应该将 onclicklistener 和其他 View 创建移动到相应 View 的 placeholderfragment 的 oncreateView,以便在没有的情况下工作

关于android - 选项卡式 Activity 下的子 fragment 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41582521/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com