私はアンドロイドの開発にはとても慣れていません。
私の場合、1つのメインアクティビティと1つのフラグメントが作成されています。メインアクティビティは2つのボタンです。
ボタンをクリックすると、私のフラグメントがロードされるはずです.どのように私はそれを達成することができますか?
メインアクティビティ .XML <?xml version="1.0" encoding="utf-8"?>; <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"; xmlns:tools="http://schemas.android.com/tools"; android:layout_width="match_parent"; android:layout_height="match_parent"; android:paddingBottom="@dimen/activity_vertical_margin"; android:paddingLeft="@dimen/activity_horizontal_margin"; android:paddingRight="@dimen/activity_horizontal_margin"; android:paddingTop="@dimen/activity_vertical_margin"; tools:context="com.bentgeorge.fragment.MainActivity">;
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2"
android:id="@+id/fragment_button_2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"
android:id="@+id/fragment_button_1"
android:layout_above="@+id/fragment_button_2"
android:layout_alignLeft="@+id/fragment_button_2"
android:layout_alignStart="@+id/fragment_button_2"
android:layout_marginBottom="33dp" />
</RelativeLayout>
メインアクティビティ .java
package com.bentgeorge.fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button fragment_btn_1;
private Button fragment_btn_2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment_btn_1 = (Button) findViewById(R.id.fragment_button_1);
fragment_btn_2 = (Button) findViewById(R.id.fragment_button_2);
}
@Override
public void onClick(View v) {
Fragment1 frag = new Fragment1();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.mainlayout,frag,"Test Fragment");
transaction.commit();
}
}
フラグメント_フラグメント1.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bentgeorge.fragment.Fragment1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
フラグメント1.java
package com.bentgeorge.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class Fragment1 extends Fragment {
public Fragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
}
インターネット上で利用できるチュートリアルで混乱しているため、あまりコードを書いていません。
これを修正するために私を助けてください
ありがとう&amp; Regards、
更新
私はいくつかの変更を行い、私のフラグメントがロードされるようになりました。しかし、ボタンはまだ表示されています。どうすればボタンを隠すことができるのでしょうか?
[フラグメント][1]です。
更新日:。 MainActivityを次のように変更してください。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new MainFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
}
次のようにactivity_main.xmlを変更します。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
xml fragment_main.xmlを作成してください:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2"
android:id="@+id/fragment_button_2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"
android:id="@+id/fragment_button_1"
android:layout_above="@+id/fragment_button_2"
android:layout_alignLeft="@+id/fragment_button_2"
android:layout_alignStart="@+id/fragment_button_2"
android:layout_marginBottom="33dp" />
</RelativeLayout>
MainFragmentとしてフラグメントも作成します。
public class MainFragment extends Fragment implements View.OnClickListener{
private Button fragment_btn_1;
private Button fragment_btn_2;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
fragment_btn_1 = (Button) view.findViewById(R.id.fragment_button_1);
fragment_btn_2 = (Button) view.findViewById(R.id.fragment_button_2);
fragment_btn_1.setOnClickListener(this);
fragment_btn_2.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fragment_button_1:
Fragment fragment1 = new Fragment1();
moveToFragment(fragment1);
break;
case R.id.fragment_button_2:
Fragment fragment2 = new Fragment2();
moveToFragment(fragment2);
break;
}
}
private void moveToFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
}
これがあなたを助けることを願っています。 ご不明な点がございましたらお知らせください。
onClick()メソッドの中で
if(v.getId()==R.id.fragment_button_1){
getActivity().getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_frame, new Fragment1(), "createPost").addToBackStack(null).commit();
}
R.id.fragment_frame - MainActivity.javaのrootContainer Id rootViewの場合
のように、アクティビティにフラグメントを配置する場合は、Framelayoutを使用する必要があります。
以下のようにします。 アクティビティレイアウト(activity_layout.xml)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapplication.Activities.TestActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:layout_centerHorizontal="true"
android:text="Button 1" />
</LinearLayout>
</FrameLayout>
と、あなたのアクティビティ - MainActivity.java です。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class TestActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
@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_test, 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 onClick(View v) {
if (v.getId() == R.id.button1){
getSupportFragmentManager().beginTransaction().
replace(R.id.root_layout, new Fragment1()).commit();
}
}
}
あなたのフラグメントがあなたの助けになることを願っています。
IntentでFragmentを呼び出すことはできません'。FragmentはFragmentActivityの一部である。
Fragmentはコンテナではなくコンテンツなので、FragmentActivityを作成し、その中にFragment(Fragment1)を追加し、onClick()で呼び出す必要があります、
Intent intent = new Intent(MainActivity.this, SomeFragmentActivity.class);
startActivity(intent);
詳しくはこちら]1をご覧ください。
ExampleFragmentActivity.classと言うもう1つのアクティビティを作成し、フラグメントタグをFramelayout内のExampleFragmentActivityのレイアウトに含めます。
public class ExampleFragmentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_feagment_activity);
}
}
example_fragment_activity.xml(変更済み)。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bentgeorge.fragment.Fragment1">
<fragment
android:id="@+id/titles"
class="com.bentgeorge.fragment.Fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
MainAcitivity.classのOnclickメソッド。
Intent intent = new Intent(MainAcitivity.this,ExampleFragmentActivity.class);
フラグメントマネージャーを使用して、フラグメントを動的に含めることもできます。
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.add(R.id.fragment_container, fragment1, "fragment");
fragmentTransaction.commit();
FragmentTransaction fragmenttransaction = getSupportFragmentManager().beginTransaction();
HomeFragment regcomplainfragment = new HomeFragment();
fragmenttransaction.replace(R.id.content_frame, regcomplainfragment).addToBackStack("HomeFragment");
fragmenttransaction.commit();