java - CalendarView 在片段中不起作用?安卓
问题描述
因此,如果我创建一个新项目并将 calanderView 放入主要活动的 xml 文件中。然后在主要活动中写一些代码
public class MainActivity extends AppCompatActivity {
private CalendarView mCalendarView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCalendarView = findViewById(R.id.calendarView);
mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
String date = year + "/" + month + "/" + dayOfMonth;
Log.wtf("wtf","onSelectedDayChange" + date);
}
});
}
}
setOnDateChangeListener 将按预期工作,并且 Log.wtf 将执行。
如果我尝试将 calanderView 放入片段的 xml 文件中。日历将出现在片段中,我可以单击日期。但由于某种原因, setOnDateChangeListener 不起作用。这是我的片段代码。抱歉,它是默认代码,所以有很多。我是使用片段的新手,所以我不确定最重要的是什么。我在 onCreateView 方法中实现了 setOnDateChangeListener。
public class ThirdFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public ThirdFragment() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static ThirdFragment newInstance(String param1, String param2) {
ThirdFragment fragment = new ThirdFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
private CalendarView mCalendarView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_third, container, false);
mCalendarView = (CalendarView) view.findViewById(R.id.calendarView);
mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
String date = year + "/" + month + "/" + dayOfMonth;
Log.wtf("wtf","onSelectedDayChange" + date);
}
});
return inflater.inflate(R.layout.fragment_third, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
老实说,我不知道为什么这不起作用,因为我的项目中有其他片段,其中我有按钮单击事件起作用的按钮,我实现了这样的:
private OnFragmentInteractionListener mListener;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_login, container, false);
button2 = view.findViewById(R.id.btn_register);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// IMPORTANT this interface can send stuff back to the main activity
mListener.register();
}
});
return view;
}
如果有人能给我一些见解,将不胜感激!
解决方案
在 Fragments 中,如果您不使用绑定,那么在onCreateView方法中您应该只inflate and return the view
.. 并在onViewCreated中执行您的代码内容it makes sure that your get the inflated view and the view is not null
。
您还应该寻找Fragment Lifecycle Methods。这样,您将了解 Fragments 如何与 Activity 一起工作。
推荐阅读
- java - 使用键发送数据对象
- java - Spring Batch:块属性的预期行为是什么?
- python - python - 如何使用sqlite3在Python中合并具有相同模式的大量列的多个表?
- swift - SwiftUI:ScrollView 无法与 ForEach 一起正常工作
- python - 如何循环创建文件夹?
- javascript - Webpack - 防止对等依赖项中的字体文件(.woff、.eot、.ttf)成为构建的一部分
- xcode - xCode Firebase '无效路径验证
- graphql - GraphQL 解析数据,用于响应
- powerapps - Sybase 和 PowerApps
- linux - 如何在 Unix 中提取一行中的某些字符和单词