如何在没有onCreateView的情况下使用Layout inflater

时间:2018-04-15 18:43:33

标签: android layout-inflater

所以我的代码应该支持手机和平板电脑(带片段的平板电脑)。我知道如何膨胀片段并在MessageFragment类中设置必要的值,但我应该在MessageDetails类中执行此操作。我不知道该怎么做。

如果我在手机或平板电脑上,ChatWindow类的代码会启动:

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代码:

@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类,我应该为平板电脑扩充片段并分配值(你可以看到电话布局的代码已经存在)。

 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;
}

}

2 个答案:

答案 0 :(得分:0)

由于MessageDetails是一个Activity。你可以尝试这样的事情:

getLayoutInflater().inflate(your_xml_layout)

答案 1 :(得分:0)

如果您想基于某些业务逻辑嵌入片段,那么您可以尝试

<?php
    $email = $_GET['email'];

    $servername = "localhost";
    $usernamedb = "root";
    $passworddb = "smartlock";
    $dbname = "login";
    $conn = mysqli_connect($servername,$usernamedb,$passworddb,$dbname);
    $sql = "DELETE   FROM  `users` WHERE (`email`='".$email."')";

    if ($conn->query($sql) === TRUE)  {
        $response = array('message'=>'success');
        echo json_encode($response);
    } 
    else {
        $response = array('message'=>'wrong');
        echo json_encode($response);
    }

    mysqli_close($conn);
?>

或者,如果活动的布局xml文件中已存在FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit(); ,则只要fragment布局被系统和{{1}夸大,就会实例化该片段将调用activity's的方法。

根据官方文件,

  

当系统创建此活动布局时,它会实例化布局中指定的每个片段,并为每个片段调用onCreateView()方法,以检索每个片段的布局。系统直接将片段返回的视图插入到元素的位置。

请按照以下链接中的官方文档,以便更好地了解onCreateView生命周期。

https://developer.android.com/guide/components/fragments.html#Creating