findFragmentById始终返回null

时间:2017-11-24 19:15:59

标签: android android-fragments android-activity

我有一个MainActivity和两个片段FGames和FGameDetail。我正在尝试在它们之间进行通信,以便在FGames中单击一个项目时,它将GameEntity传递给MainActivity并更新FGameDetail。我为手机和平板电脑创建了单独的布局。

在我的MainActivity中,我控制是否在布局中存在FGameDetail片段更新视图。但是findFragmentById总是为FGames和FGameDetails返回null。

这是我的手机和平板电脑的布局

布局\ activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/games_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.rao.igttest.MainActivity">

    <fragment
        android:id="@+id/fragment_fGames"
        android:name="com.example.rao.igttest.Games.View.FGames"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

布局大土地\ activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/fragment_fGames"
        android:name="com.example.rao.igttest.Games.View.FGames"
        android:layout_width="400dp"
        android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/fragment_fGameDetail"
        android:name="com.example.rao.igttest.Games.View.FGameDetail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity implements FGames.OnGameSelectedListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //FGames fGames = new FGames();
        //getSupportFragmentManager().beginTransaction().add(R.id.games_container, fGames).commit();
    }

    @Override
    public void onGameSelected(GameEntity gameEntity) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FGameDetail gameDetailFrag = (FGameDetail) fragmentManager
                .findFragmentById(R.id.fragment_fGameDetail);
        Fragment gameFrag = fragmentManager.findFragmentById(R.id.fragment_fGames);
        if (gameDetailFrag == null) {
          //TODO Open a new intent
        } else {
            // DisplayFragment (Fragment B) is in the layout (tablet layout),
            // so tell the fragment to update
            FGameDetail fGameDetail = new FGameDetail();
            fGameDetail.updateContent(gameEntity);
        }
    }
}

非常感谢您的帮助和建议。

修改

FGAMES

@Override
    public void initRecyclerView(List<GameEntity> gameEntities) {
        gamesAdapter = new GamesAdapter(getActivity(), gameEntities);
        rvGames.setAdapter(gamesAdapter);
        rvGames.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

GamesAdapter

//Constructor
 public GamesAdapter(Context context, List<GameEntity> gameEntities) {
        layoutInflater = LayoutInflater.from(context);
        this.data = gameEntities;
        this.context = context;
    }
 @OnClick(R.id.row_container)
        void rowClick(){
            //I think as I am creating a new instance here as I dont have referencec to GamesPresenter in adapter as I am calling it directly from FGames
            GamesPresenter gamesPresenter = new GamesPresenterImpl();
            gamesPresenter.showGameDetail(data.get(getLayoutPosition()));
            Toast.makeText(context, "itemClicked " + data.get(getLayoutPosition()), Toast.LENGTH_SHORT).show();
        }

GamesPresenterImpl

 @Override
    public void showGameDetail(GameEntity gameEntity) {
        GamesView gamesView = new FGames();
        gamesView.onListItemClick(gameEntity);

    }

FGames

 @Override
    public void onListItemClick(GameEntity gameEntity) {
        OnGameSelectedListener mListener = new MainActivity();
        mListener.onGameSelected(gameEntity);
    }
public interface OnGameSelectedListener{
    public void onGameSelected(GameEntity gameEntity);
}

MainActivity - OnGameSelected。

  @Override
    public void onGameSelected(GameEntity gameEntity) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FGameDetail gameDetailFrag = (FGameDetail) fragmentManager
                .findFragmentById(R.id.fragment_fGameDetail);
        Fragment gameFrag = fragmentManager.findFragmentById(R.id.fragment_fGames);
        if (gameDetailFrag == null) {
        } else {
            // DisplayFragment (Fragment B) is in the layout (tablet layout),
            // so tell the fragment to update
            FGameDetail fGameDetail = new FGameDetail();
            fGameDetail.updateContent(gameEntity);
        }
    }

1 个答案:

答案 0 :(得分:0)

你确定你没有做新的MainActivity()。onGameSelected(object)?

如果您使用Interactor片段模式,则必须使用该类中已有的Activity实例。

此外,此行不更新屏幕会导致您创建永远不会添加的片段的新实例:

FGameDetail fGameDetail = new FGameDetail(); 
fGameDetail.updateContent(gameEntity); 

屏幕上已有一个实例:

gameDetailFrag.updateContent(gameEntity);