当我运行我的应用程序时,一切正常,直到我需要将片段替换为另一个片段。当用户单击列表视图中的某个项目时,它应该打开新布局(新片段)。但就目前而言,当我点击该项目
时没有任何反应这是我的onCreate方法
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
JsonArrayRequest tournamentReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Tournament tournament = new Tournament();
tournament.setTitle(obj.getString("title"));
tournament.setThumbnailUrl(obj.getString("image"));
tournament.setDate(((Number) obj.get("tdate"))
.doubleValue());
// Genre is json array
JSONArray categoryArry = obj.getJSONArray("category");
ArrayList<String> category = new ArrayList<String>();
for (int j = 0; j < categoryArry.length(); j++) {
category.add((String) categoryArry.get(j));
}
tournament.setCategory(category);
// adding tournament to tournament array
TournamentList.add(tournament);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(tournamentReq);
}
这是我在
中的onCreateView和OnSelectionChanged方法TournamentFragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tournament, container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
if (rootView.findViewById(R.id.frag_tour) != null){
if (savedInstanceState != null) {
// Create an Instance of Fragment
TournamentFragment tFragment = new TournamentFragment();
tFragment.setArguments(getActivity().getIntent().getExtras());
getFragmentManager().beginTransaction()
.add(R.id.frag_tour, tFragment)
.commit();
}}return rootView;
}
public void OnSelectionChanged(int versionNameIndex) {
DescriptionFragment descriptionFragment = new DescriptionFragment();
descriptionFragment.setArguments(getActivity().getIntent().getExtras());
if (descriptionFragment != null){
descriptionFragment.setDescription(versionNameIndex);
} else {
TournamentFragment tFragment = new TournamentFragment();
fragmentTransaction.replace(R.id.fragment_container, tFragment);
fragmentTransaction.commit();
}
}
fragment_description.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="This is where each individual description will appear"
android:id="@+id/version_description"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:gravity="center"/>
</RelativeLayout>
到目前为止,我曾尝试过在stackoverflow中回答的不同类型的解决方案,但仍然无法解决我的问题。 有人能帮助我理解这个吗?
答案 0 :(得分:0)
使用以下代码
getSupportFragmentManager()
.beginTransaction()
.detach(contentFragment) // detach the current fragment
.attach(contentFragment) // attach fragment that you want.
.commit();
答案 1 :(得分:0)
尝试通过
进行通话getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, tFragment)
.fragmentTransaction
.commit();