2016-04-12 13 views
1

私は小さなGoogleマップアプリケーションを使用して、近くに場所を見つけることができます。ユーザーがお気に入りのリストに場所を追加できるようにする機能を追加したいと思います。機能性Open Fragmentアクティビティ

import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.ImageButton; 

public class MainActivity extends AppCompatActivity { 

    ImageButton btnNearBy; 
    ImageButton btnFavourites; 


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

     btnNearBy = (ImageButton) findViewById(R.id.btnNearby); 
     btnNearBy.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent mapIntent = new Intent(getBaseContext(), MapsActivity.class); 
       startActivity(mapIntent); 
      } 
     }); 

     btnFavourites = (ImageButton) findViewById(R.id.btnFavourites); 
     btnFavourites.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       FragmentManager fragmentManager = MainActivity.this.getFragmentManager(); 
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       FavouriteListFragment fragment = new FavouriteListFragment(); 
       fragmentTransaction.add(R.id.fragment_container, fragment); //ERROR ON THIS LINE 
       fragmentTransaction.commit(); 
      } 
     }); 

    } 
} 

アイブ氏は、私のフラグメントはこのように宣言され、お気に入りのリストを保持しているフラグメントを開く必要があるボタンを作成:

私の主な活動は、他の活動、以下のコードを開き、自分のホームページです。 public class FavouriteListFragment extends Fragment { ... }

ボタンをクリックすると、MainActivityからフラグメントを開く方法が少し不明です。 アイデア ありがとう!

+0

まず、XMLファイルにフラグメントコンテナが必要です。残りの部分は@Rohit answer – Pooya

+0

それは私のMainActivity xmlファイルにありますか? – T91

+0

OK私はあなたのために答えを入れます – Pooya

答えて

1

断片を表示する2つの方法があります。

1 - まず、あなたは、次のようにあなたのコード内のフラグメントのコンテナを定義する必要があります。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="do something" 
     android:onClick="openFragment" /> 
    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_height="wrap_content" 
     android:layout_width="match_content" /> 
</LinearLayout> 

、その後、アクティビティにopenFragmentという名前の関数を作成し、openFragmentに次のコードを使用する必要があります。

getSupportFragmentManager().beginTransaction().add(R.id_fragment_container,new FavouriteListFragment()).commit(); 

2 - あなたは同様にあなたの活動のxmlファイルにフラグメントを定義することができます。

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/my_fragment" 
    android:name="com.example.android.something.FavouriteListFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.android.something.FavouriteListFragment" 
    tools:layout="@android:layout/fragment_layout" /> 

最初のものは、動的フラグメントの作成と呼ばれる2つ目は、静的と呼ばれています。最初のもので自由度が増しますが、アクティビティ全体でフラグメントが変化しない場合は、2番目のものを使用する方が簡単です

+0

ありがとうございました。私がリストを開くときにそれを見たくないので、フラグメントは主なアクティビティを置き換えますか? – T91

+0

@ T91フラグメントは、あなたのアクティビティを置き換えることはありませんが、アクティビティ内のビューに参加します。フラグメントが占有するレイアウトが親ビューであれば、それはあなたの全体像を占めます。しかし、あなたのレイアウトが例えば4つのビューを持っているならば、断片のためにビューの1つを使うことができ、残りはあなたの活動のメインレイアウトのために残ります – Pooya

+0

@ T91あなたがその動作をよりよく理解したいならば、それがどのように動作するか見るには – Pooya

3

android.support.v4.app.Fragmentを使用しているため、正しいバージョンをインポートする際に多くの混乱が生じています。このように試してみてください:

fragment_containeractivity_main内部 FrameLayoutある
android.support.v4.app.FragmentManager fragmentManager = MainActivity.this.getSupportFragmentManager(); 
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

FavouriteListFragment fragment = new FavouriteListFragment(); 

fragmentTransaction.add(R.id.fragment_container, fragment); 
fragmentTransaction.commit(); 

<FrameLayout 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</FrameLayout> 

Reference

+0

私はフラグメントコンテナのIDで何も持っていません。私はどこでそれを作りますか? – T91

+0

は 'activity_main'に' FrameLayout'でなければなりません。 –

+0

@ T91、私の更新された回答を確認してください –