2016-08-24 6 views
1

ListViewを持つフラグメントがrow個あります。 rowはで構成されてちょうど私がこのListViewを埋めるためにResourceCursorAdapterを持ってImageViewAndroid。リストビューのアイテムをonClickすると、新しい子フラグメントに内部ボタンが表示されます。

fragment_1.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:id="@+id/fragment_1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".Fragment1"> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:divider="#b5b5b5" 
     android:dividerHeight="1dp" 
     android:listSelector="@drawable/list_selector"/> 

</RelativeLayout> 

row.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/row" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/list_selector" 
    android:orientation="horizontal" 
    android:padding="5dip"> 

    <!-- Rightend Play Button. --> 
    <ImageView 
     android:id="@+id/play_button" 
     android:layout_width="50dip" 
     android:layout_height="50dip" 
     android:src="@drawable/ic_play" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true"/> 

</RelativeLayout> 

ています。私は各ImageViewにクリックリスナーを割り当てます。提供されたパスに保存されたレコードを再生します。

private abstract class MyAdapter extends ResourceCursorAadpter { 

    @Override 
    public void bindView(View view, Context context, final Cursor cursor) { 
     // Play button. 
     ImageView playButton = (ImageView) view.findViewById(R.id.play_button); 
     playButton.setOnClickListener(new View.OnClickListener() { 
      String record = cursor.getString(cursor.getColumnIndex(DbAdapter.RECORD)); 
      public void onClick(View v) { 
       // Play record in path [record]. Not the problem. 
      } 
     }); 
    } 
} 

は、今私はrowクリックで新しいフラグメントFragment_2を開きたいです。このフラグメントは同じplay_buttonを持ち、同じレコードを再生する必要があります。

fragment_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:id="@+id/fragment_audio" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".Fragment2"> 

    <!-- Play Button. --> 
    <ImageView 
     android:id="@+id/play_button" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:src="@drawable/ic_play"/> 

</RelativeLayout> 

は、どのように私は、このボタンはFragment_1に比べて同じレコードを果たしていることを管理することができますか?私はレコードのパスで隠れたTextViewを持つことができますが、よりスマートなソリューションを持っているかどうかは確かです。

私のonCreateViewFragment_1です。

mList = (ListView) root.findViewById(R.id.list); 
// Click event for single row. 
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) { 
     // TODO: Go to fragment_2 and has the same view [view.findViewById(R.id.play_button)] 
    } 
}); 
+0

あなたの問題を編集して問題を明確に述べることをお勧めします。また、画像の代わりにポストコードを書いてください。 –

+1

@ AjayP.Prajapati申し訳ありませんが、コードをアップロードする際に問題がありましたが、今はすべて正しいです。問題について不明な点はありますか? –

答えて

2

フラグメント間の通信は、関連アクティビティを介して行う必要があります。あなたはこれのためのインターフェイスを使用することができます。契約と橋渡しの役割を果たします。

のは、次のコンポーネント持ってみましょう:データ

FragmentB第2のフラグメントを送信するフラグメントの通信

FragmentA最初のフラグメントを

活動ホストフラグメントをし、許可されますフラグメントAからデータを受信

FragmentAの実装は次のとおりです。

public class FragmentA extends Fragment { 
    DataPassListener mCallback; 
    public interface DataPassListener{ 
    public void passData(String data); 
    } 
    @Override 
    public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    // Make sure that container activity implement the callback interface 
    try { 
     mCallback = (DataPassListener)activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString() 
      + " must implement DataPassListener"); 
    } 
    } 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
    // Suppose that when a button clicked second FragmentB will be inflated 
    // some data on FragmentA will pass FragmentB 
    // Button passDataButton = (Button)......... 
    passDataButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     if (view.getId() == R.id.passDataButton) { 
      mCallback.passData("Text to pass FragmentB"); 
     } 
     } 
    }); 
    } 
} 

MainActivityの実装は次のとおりです。

public class MainActivity extends ActionBarActivity implements DataPassListener{ 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    if (findViewById(R.id.container) != null) { 
     if (savedInstanceState != null) { 
      return; 
     } 
     getFragmentManager().beginTransaction() 
       .add(R.id.container, new FragmentA()).commit(); 
    } 
} 
@Override 
public void passData(String data) { 
    FragmentB fragmentB = new FragmentB(); 
    Bundle args = new Bundle(); 
    args.putString(FragmentB.DATA_RECEIVE, data); 
    fragmentB .setArguments(args); 
    getFragmentManager().beginTransaction() 
     .replace(R.id.container, fragmentB) 
     .commit(); 
} 
} 

FragmentBの実装は次のとおりです。この文書は、で与えられた

public class FragmentB extends Fragment{ 
final static String DATA_RECEIVE = "data_receive"; 
TextView showReceivedData; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_B, container, false); 
    showReceivedData = (TextView) view.findViewById(R.id.showReceivedData); 
} 
@Override 
public void onStart() { 
    super.onStart(); 
    Bundle args = getArguments(); 
    if (args != null) { 
     showReceivedData.setText(args.getString(DATA_RECEIVE)); 
    } 
} 

https://developer.android.com/training/basics/fragments/communicating.html

詳細に形成すると、あなたはそれを通過することができます。

このようにして、2つのフラグメント間で通信することができます。

+0

私の場合、「FragmentBを渡すテキスト」をどのように抽出すればよいですか?私はそれが私のレコードがあるパスを渡したいが、私はこのレコードを再生するonClickを持っている。どのように私は行のリストビューごとにパス文字列を保存することができます。私の質問では、この文字列は 'レコード'となります。 –

+0

この例でわかるように、bundleを使用して値を渡し、必要な場所で抽出することができます。 –

+0

この例では、FragmentAからFragmentBにTextを渡しました。 –

関連する問題