2016-08-24 37 views
1

我有一個ListView的row項目的片段。一個row在於只是我ImageViewAndroid。獲取ListView項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> 

我有一個ResourceCursorAdapter填補這一ListView。我只是爲每個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

片段之間的通信應該通過關聯的Activity完成。你可以使用這個接口。它將成爲它們之間的契約和橋樑。

讓我們以下組件:

一個活動主機片段和允許片段通信

FragmentA第一片段,其將發送數據

FragmentB秒片段,其將從接收數據片段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 本文件中給出詳細的形成,你可以通過它。

在你的情況下,你可以用這種方式在兩個片段之間進行通信。

+0

在我的情況下,我如何提取'文本傳遞FragmentB'。我想將它傳遞到我的記錄的路徑,但我只需使用onClick播放此記錄。如何保存每行列表視圖的路徑字符串。在我的問題中,這個字符串將是「記錄」。你可以在例子中看到 –

+0

,你可以使用bundle傳遞值,並在需要的地方提取它。 –

+0

在這個例子中,我將文本從FragmentA傳遞給FragmentB。 –