用另一个列表替换 master activity 中的列表并从视图中删除旧列表
Replace list in master activity with another list and remove old list from view
启动我的应用程序时,主片段中会显示一个列表 (list_main)。当我从我的主列表片段中单击第一个列表项时,还有一个应该打开的第二个列表片段,但是在单击第一个列表项之后,主列表片段不会消失但第二个列表不会出现因此主片段保持空白。此外,当我单击后退按钮时,主列表不会重新出现。有谁知道为什么会出现这些问题以及如何解决这些问题?
主要ActivityXML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".MainActivity" >
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/master_container"
android:name="com.apptacularapps.exitsexpertlondonlite.FragmentMainList"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:id="@+id/detail_container"/>
</LinearLayout>
主列表片段XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentmainlist">
<ListView
android:id="@+id/list_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="false"
android:layout_centerHorizontal="true"/>
</LinearLayout>
主列表片段class
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentMainList extends android.support.v4.app.Fragment {
ListView list_main;
String[] listContent = {
"Item 1",
"Item 2",
"Item 3"
};
private boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_main_list, container, false);
list_main = (ListView)v.findViewById(R.id.list_main);
list_main = (ListView)findViewById(R.id.list_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listContent);
list_main.setAdapter(adapter);
list_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (mTwoPane) {
for (int j = 0; j < adapterView.getChildCount(); j++)
adapterView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
// change the background color of the selected list view item
view.setBackgroundColor(Color.parseColor("#00BCD4"));
}
if (position == 0) {
if (mTwoPane) {
FragmentItem1 newFragment = new FragmentItem1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
list_main.setVisibility(View.GONE);
transaction.replace(R.id.master_container, newFragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.addToBackStack(null);
transaction.commit();
} else {
}
}
if (position == 1) {
}
if (position == 2) {
}
}
});
return v;
}
}
主要Activityclass
public class MainActivity extends ActionBarActivity {
ListView list_main;
String[] listContent = {
"Station Chooser",
"Item 2",
"Item 3"
};
private boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_main = (ListView)findViewById(R.id.list_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listContent);
list_main.setAdapter(adapter);
list_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (position == 0) {
if (mTwoPane) {
FragmentStationChooser newFragment = new FragmentStationChooser();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
getSupportFragmentManager().executePendingTransactions();
} else {
}
}
if (position == 1) {
}
if (position == 2) {
}
}
});
if (findViewById(R.id.detail_container) != null) {
mTwoPane = true;
}
}
}
选台片段
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_linechooser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="false"
android:layout_centerHorizontal="true"/>
</LinearLayout>
选台器片段class
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentStationChooser extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_station_chooser, container, false);
}
}
电台选择器 Activity class
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class StationChooserActivity extends ActionBarActivity {
ListView list_linechooser;
String[] listContent = {
"Line 1",
"Line 2",
"Line 3"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_station_chooser);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
list_linechooser = (ListView)findViewById(R.id.list_linechooser);
list_linechooser.setAdapter(adapter);
list_linechooser.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
}
});
}
@Override
public void onBackPressed() {
if (mTwoPane) {
FragmentMainList newFragment = new FragmentMainList();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
list_main.setVisibility(View.VISIBLE);
transaction.replace(R.id.master_container, newFragment).commit();
transaction.addToBackStack(null);
}
}
}
好像StationChooserActivity
是你的MainActivity,没有其他的activity。你没有提到那个。
您的应用程序唯一要做的就是根据数组 listContent 将 "Line 1"、"Line 2" 和第 3 行显示到列表视图中。这是由于代码 MyColoringAdapter adapter = new MyColoringAdapter(this,listContent)
。
另外,当用户点击 Back 键时,如果 mTwoPane 为真,则可能会显示一个新的 Fragment FragmentMainList,但不确定。之后我认为您的应用程序将退出。
注意:Main Activity 的作用不大。我不认为这是意图。您需要确定如何处理它。这是我最好的建议。
根据您过去的评论和我建议将代码从 StationChooserActivity.java 移动到 FragmentStationChooser
FragmentStationChooser的代码class:
String[] listContent = {
"Line 1",
"Line 2",
"Line 3"
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_station_chooser, container, false);
ListView list_linechooser = (ListView) v.findViewById(R.id.list_linechooser);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.fragment_station_chooser, listContent);
list_linechooser.setAdapter(adapter);
return v;
}
这是一个开始。我尽力了解您的 post 和问题。祝你好运。
为什么片段在弹出后退栈时没有显示的主要问题是你试图在另一个片段 id 中膨胀该片段,这是代码的这一部分:
transaction.replace(R.id.master_container, newFragment);
您不需要全新的 activity 来托管您在 FragmentStationChooser
中创建的另一个片段。
现在你需要做的是在你的 activity main 中有一个容器来托管你的片段,而不是用一个容器来代替片段标签,比如相对布局:
替换为:
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/master_container"
android:name="com.apptacularapps.exitsexpertlondonlite.FragmentMainList"/>
到
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/master_container"/>
在你这样做之后,在你的主 activity 中删除所有列表视图代码并将其传输到你的 FragmentMainList
你在膨胀它时使用视图的地方:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_main_list, container, false);
list_main = (ListView)v.findViewById(R.id.list_main);
.//the logic from your activity main transfered here in the FragmentMainList
.
.
return v;
}
最后在你的 FragmentStationChooser
中做同样的事情并且不要使用 activity StationChooserActivity
只需将它从你的项目中删除
编辑:
删除 list_main.setVisibility(View.GONE);
因为它在膨胀时会被碎片替换
编辑 2:
在您的主程序 activity 的 onCreate 中添加:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentMainList newFragment = new FragmentMainList();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.commit();
}
启动我的应用程序时,主片段中会显示一个列表 (list_main)。当我从我的主列表片段中单击第一个列表项时,还有一个应该打开的第二个列表片段,但是在单击第一个列表项之后,主列表片段不会消失但第二个列表不会出现因此主片段保持空白。此外,当我单击后退按钮时,主列表不会重新出现。有谁知道为什么会出现这些问题以及如何解决这些问题?
主要ActivityXML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".MainActivity" >
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/master_container"
android:name="com.apptacularapps.exitsexpertlondonlite.FragmentMainList"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:id="@+id/detail_container"/>
</LinearLayout>
主列表片段XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentmainlist">
<ListView
android:id="@+id/list_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="false"
android:layout_centerHorizontal="true"/>
</LinearLayout>
主列表片段class
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentMainList extends android.support.v4.app.Fragment {
ListView list_main;
String[] listContent = {
"Item 1",
"Item 2",
"Item 3"
};
private boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_main_list, container, false);
list_main = (ListView)v.findViewById(R.id.list_main);
list_main = (ListView)findViewById(R.id.list_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listContent);
list_main.setAdapter(adapter);
list_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (mTwoPane) {
for (int j = 0; j < adapterView.getChildCount(); j++)
adapterView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
// change the background color of the selected list view item
view.setBackgroundColor(Color.parseColor("#00BCD4"));
}
if (position == 0) {
if (mTwoPane) {
FragmentItem1 newFragment = new FragmentItem1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
list_main.setVisibility(View.GONE);
transaction.replace(R.id.master_container, newFragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.addToBackStack(null);
transaction.commit();
} else {
}
}
if (position == 1) {
}
if (position == 2) {
}
}
});
return v;
}
}
主要Activityclass
public class MainActivity extends ActionBarActivity {
ListView list_main;
String[] listContent = {
"Station Chooser",
"Item 2",
"Item 3"
};
private boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_main = (ListView)findViewById(R.id.list_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listContent);
list_main.setAdapter(adapter);
list_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (position == 0) {
if (mTwoPane) {
FragmentStationChooser newFragment = new FragmentStationChooser();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
getSupportFragmentManager().executePendingTransactions();
} else {
}
}
if (position == 1) {
}
if (position == 2) {
}
}
});
if (findViewById(R.id.detail_container) != null) {
mTwoPane = true;
}
}
}
选台片段
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_linechooser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="false"
android:layout_centerHorizontal="true"/>
</LinearLayout>
选台器片段class
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentStationChooser extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_station_chooser, container, false);
}
}
电台选择器 Activity class
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class StationChooserActivity extends ActionBarActivity {
ListView list_linechooser;
String[] listContent = {
"Line 1",
"Line 2",
"Line 3"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_station_chooser);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
list_linechooser = (ListView)findViewById(R.id.list_linechooser);
list_linechooser.setAdapter(adapter);
list_linechooser.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
}
});
}
@Override
public void onBackPressed() {
if (mTwoPane) {
FragmentMainList newFragment = new FragmentMainList();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
list_main.setVisibility(View.VISIBLE);
transaction.replace(R.id.master_container, newFragment).commit();
transaction.addToBackStack(null);
}
}
}
好像StationChooserActivity
是你的MainActivity,没有其他的activity。你没有提到那个。
您的应用程序唯一要做的就是根据数组 listContent 将 "Line 1"、"Line 2" 和第 3 行显示到列表视图中。这是由于代码 MyColoringAdapter adapter = new MyColoringAdapter(this,listContent)
。
另外,当用户点击 Back 键时,如果 mTwoPane 为真,则可能会显示一个新的 Fragment FragmentMainList,但不确定。之后我认为您的应用程序将退出。
注意:Main Activity 的作用不大。我不认为这是意图。您需要确定如何处理它。这是我最好的建议。
根据您过去的评论和我建议将代码从 StationChooserActivity.java 移动到 FragmentStationChooser
FragmentStationChooser的代码class:
String[] listContent = {
"Line 1",
"Line 2",
"Line 3"
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_station_chooser, container, false);
ListView list_linechooser = (ListView) v.findViewById(R.id.list_linechooser);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.fragment_station_chooser, listContent);
list_linechooser.setAdapter(adapter);
return v;
}
这是一个开始。我尽力了解您的 post 和问题。祝你好运。
为什么片段在弹出后退栈时没有显示的主要问题是你试图在另一个片段 id 中膨胀该片段,这是代码的这一部分:
transaction.replace(R.id.master_container, newFragment);
您不需要全新的 activity 来托管您在 FragmentStationChooser
中创建的另一个片段。
现在你需要做的是在你的 activity main 中有一个容器来托管你的片段,而不是用一个容器来代替片段标签,比如相对布局:
替换为:
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/master_container"
android:name="com.apptacularapps.exitsexpertlondonlite.FragmentMainList"/>
到
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/master_container"/>
在你这样做之后,在你的主 activity 中删除所有列表视图代码并将其传输到你的 FragmentMainList
你在膨胀它时使用视图的地方:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_main_list, container, false);
list_main = (ListView)v.findViewById(R.id.list_main);
.//the logic from your activity main transfered here in the FragmentMainList
.
.
return v;
}
最后在你的 FragmentStationChooser
中做同样的事情并且不要使用 activity StationChooserActivity
只需将它从你的项目中删除
编辑:
删除 list_main.setVisibility(View.GONE);
因为它在膨胀时会被碎片替换
编辑 2:
在您的主程序 activity 的 onCreate 中添加:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentMainList newFragment = new FragmentMainList();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.commit();
}