如何在没有 onCreateView 的情况下使用 Layout inflater
How to use Layout inflater without onCreateView
所以我的代码应该同时支持 phone 和平板电脑(带有片段的平板电脑)。我知道如何膨胀片段并在 MessageFragment class 中设置必要的值,但我应该在 MessageDetails class 中执行此操作。我不知道该怎么做。
ChatWindow class 有我的代码,它根据我是在 phone 还是平板电脑上启动:
myDisplay.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(isTablet){
MessageFragment mFragment = new MessageFragment();
Bundle bundle = new Bundle();
String idString = String.valueOf(id);
bundle.putString("message_id", idString);
String message = cursor.getString(cursor.getColumnIndex(ChatDatabaseHelper.KEY_MESSAGE));
bundle.putString("message_value", message);
mFragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.frme, mFragment).addToBackStack(null) .commit();
} else { Intent intent = new Intent(ChatWindow.this, MessageDetails.class);
String idString = String.valueOf(id);
String message = cursor.getString(cursor.getColumnIndex(ChatDatabaseHelper.KEY_MESSAGE));
intent.putExtra("message_id", idString);
intent.putExtra("message_value", message);
startActivity(intent); } //for phone
}
});
这是我的 MessageFragment onCreateView 代码 Class:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup parent, @Nullable Bundle savedInstanceState) {
// Inflate the xml file for the fragment
View rootView = inflater.inflate(R.layout.activity_message_details, parent, false);
return rootView;
}
最后,这是我的 MessageDetails class,我应该在其中为平板电脑填充片段并分配值(您可以看到 phone 布局的代码已经存在)。
public class MessageDetails extends Activity {
String id;
String message;
MessageFragment mfragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_details);
Bundle bundle;
TextView delMsg;
TextView delId;
Button delBtn;
bundle = getIntent().getExtras();
setId(bundle.getString("message_id"));
setMessage(bundle.getString("message_value"));
//Missing Fragment Inflater code
delMsg = (TextView) findViewById(R.id.delMsg);
delId = (TextView) findViewById(R.id.delId);
delBtn = (Button) findViewById(R.id.delBtn);
delMsg.setText(message);
delId.setText(id);
delBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MessageDetails.this, ChatWindow.class);
intent.putExtra ("delete_id", id);
setResult(RESULT_OK);
startActivityForResult(intent, 33);
finish();
}
}
);
}
public void setId(String delId) {
delId = id;
}
public void setMessage(String delMessage) {
delMessage = message;
}
public String getId() {
return id;
}
public String getMessage() {
return message;
}
}
因为 MessageDetails 是 Activity。你可以尝试这样的事情:
getLayoutInflater().inflate(your_xml_layout)
如果你想嵌入基于一些业务逻辑的片段,那么你可以尝试
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
或者,如果 fragment
已经存在于您的 activity 布局 xml 文件中,则只要 activity's
布局被膨胀,片段就会被实例化fragment
的系统和 onCreateView
方法将被调用。
根据官方文档,
When the system creates this activity layout, it instantiates each fragment specified in the layout and calls the onCreateView() method for each one, to retrieve each fragment's layout. The system inserts the View returned by the fragment directly in place of the element.
为了更好地理解 Fragment
生命周期,请遵循以下 link 的官方文档。
https://developer.android.com/guide/components/fragments.html#Creating
所以我的代码应该同时支持 phone 和平板电脑(带有片段的平板电脑)。我知道如何膨胀片段并在 MessageFragment class 中设置必要的值,但我应该在 MessageDetails class 中执行此操作。我不知道该怎么做。
ChatWindow class 有我的代码,它根据我是在 phone 还是平板电脑上启动:
myDisplay.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(isTablet){
MessageFragment mFragment = new MessageFragment();
Bundle bundle = new Bundle();
String idString = String.valueOf(id);
bundle.putString("message_id", idString);
String message = cursor.getString(cursor.getColumnIndex(ChatDatabaseHelper.KEY_MESSAGE));
bundle.putString("message_value", message);
mFragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.frme, mFragment).addToBackStack(null) .commit();
} else { Intent intent = new Intent(ChatWindow.this, MessageDetails.class);
String idString = String.valueOf(id);
String message = cursor.getString(cursor.getColumnIndex(ChatDatabaseHelper.KEY_MESSAGE));
intent.putExtra("message_id", idString);
intent.putExtra("message_value", message);
startActivity(intent); } //for phone
}
});
这是我的 MessageFragment onCreateView 代码 Class:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup parent, @Nullable Bundle savedInstanceState) {
// Inflate the xml file for the fragment
View rootView = inflater.inflate(R.layout.activity_message_details, parent, false);
return rootView;
}
最后,这是我的 MessageDetails class,我应该在其中为平板电脑填充片段并分配值(您可以看到 phone 布局的代码已经存在)。
public class MessageDetails extends Activity {
String id;
String message;
MessageFragment mfragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_details);
Bundle bundle;
TextView delMsg;
TextView delId;
Button delBtn;
bundle = getIntent().getExtras();
setId(bundle.getString("message_id"));
setMessage(bundle.getString("message_value"));
//Missing Fragment Inflater code
delMsg = (TextView) findViewById(R.id.delMsg);
delId = (TextView) findViewById(R.id.delId);
delBtn = (Button) findViewById(R.id.delBtn);
delMsg.setText(message);
delId.setText(id);
delBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MessageDetails.this, ChatWindow.class);
intent.putExtra ("delete_id", id);
setResult(RESULT_OK);
startActivityForResult(intent, 33);
finish();
}
}
);
}
public void setId(String delId) {
delId = id;
}
public void setMessage(String delMessage) {
delMessage = message;
}
public String getId() {
return id;
}
public String getMessage() {
return message;
}
}
因为 MessageDetails 是 Activity。你可以尝试这样的事情:
getLayoutInflater().inflate(your_xml_layout)
如果你想嵌入基于一些业务逻辑的片段,那么你可以尝试
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
或者,如果 fragment
已经存在于您的 activity 布局 xml 文件中,则只要 activity's
布局被膨胀,片段就会被实例化fragment
的系统和 onCreateView
方法将被调用。
根据官方文档,
When the system creates this activity layout, it instantiates each fragment specified in the layout and calls the onCreateView() method for each one, to retrieve each fragment's layout. The system inserts the View returned by the fragment directly in place of the element.
为了更好地理解 Fragment
生命周期,请遵循以下 link 的官方文档。
https://developer.android.com/guide/components/fragments.html#Creating