所以我有MainActivity.java
,我也有FragmentA.java
和FragmentB.java
。
在我的MainActivity
中,我有一个BottomNavigation
,可让我在两个片段之间进行切换。
MainActivity.java
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.nav_a:
selectedFragment = new FragmentA();
break;
case R.id.nav_b:
selectedFragment = new FragmentB();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
return true;
}
};
在FragmentA
内,我有一个RecyclerView
,当然还有一个适配器,并且在每个CardView
上都有一个按钮,当我单击该按钮时,我想将相应的对象传递给{ RecyclerView
中的{1}}。
FragmentA.java
FragmentB
FragmentB.java
adapter.setOnItemClickListener(new RewardItemAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
//When I click the item, pass theItem to FragmentB
CustomObject theItem = items.get(position);
}
});
唯一的问题是我不知道该怎么做,我尝试使用接口,但是无法正确实现,我不确定是否是因为我实例化了片段的新实例。
答案 0 :(得分:0)
通过接口执行作业是正确的,但是您必须在onAttached lifeCycle方法中将其绑定到Fragment。
定义一个接口。 OnFragmentInteractionListener.java
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(String args);
}
然后通过界面实施您的活动。
public class MainActivity extends ActionBarActivity implements OnFragmentInteractionListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
onFragmentInteraction(String args){
**// Made the Change Here**
// Send Local Broadcast Receiver
Intent intent = new Intent("UPDATE_LIST");
intent.putExtra("TYPE", 0);
// Change the args to get position or the relevant param
intent.putExtra("POSITION", selectedPosition);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
在 FragmentA.java 中,
public class FragmentA extends Fragment {
OnFragmentInteractionListener mCallback;
@Override
public void onAttach(Context context) {
super.onAttach(context);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnFragmentInteractionListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
...
adapter.setOnItemClickListener(new RewardItemAdapter.OnItemClickListener()
{
@Override
public void onItemClick(int position) {
//When I click the item, pass theItem to FragmentB
CustomObject theItem = items.get(position);
// Send the selected Item Name to Main Activity through args
mCallback.onFragmentInteraction(theItem.getName);
}
});
}
在您的 FragmentB.java 类上,添加本地广播接收器
公共类FragmentBt扩展Fragment {
public static Fragment newInstance(){
...
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.layout, container, false);
return v;
}
...
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
// Register Local Broadcast Receiver
LocalBroadcastManager.getInstance(getContext()).registerReceiver(mMessageReceiver, new IntentFilter("UPDATE_LIST"));
}
@Override
public void onDestroy() {
super.onDestroy();
// Destroy the Local Broadcast Receiver
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mMessageReceiver);
}
// Create Broadcast Receiver instance
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.hasExtra("POSITION")){
mAdapter.notifyItemChanged(intent.getIntExtra("POSITION", 0));
// Update Adapter as the requirement
}
}
};
}
希望这会有所帮助。 干杯!
答案 1 :(得分:0)
您可以通过两种方式实现这一目标:
1)通过片段与活动之间的接口契约
class FragmentA: Fragment() {
interface SomeInterfaceName {
fun setSelectedItem(item: SomeItem)
}
}
class FragmentA: Fragment() {
interface AnotherInterfaceName {
fun getSelectedItem(): SomeItem
}
}
class ContainerActivity: Activity(), AnotherInterfaceName, SomeInterfaceName {
var someItem: SomeItem? = null
fun setSelectedItem(item: SomeItem) {
someItem = item
// inflate your FragmentB
}
fun getSelectedItem(): SomeItem {
return someItem;
}
}
但是我认为最好的解决方案是在这种情况下使用共享的ViewModel。
您可以在“ {在片段之间共享数据”部分here中了解此内容。