无法找到代码来根据列表项位置替换详细信息片段
Unable to find code to replace detail fragment based on list item position
对于我的 master/detail 列表片段,我在尝试查找代码以根据列表项的位置替换详细信息片段时遇到严重问题。基于 需要替换的代码(顶行),我认为顶行需要更改为其他内容。因此,根据我的代码,有人知道该行可以用什么代替吗?所有相关帮助将不胜感激。我想要实现的行为就像主细节流程中的行为一样,如果仅在双窗格模式下,细节片段将被替换为所选列表项的片段。如果在单窗格模式下,选定的列表项将打开 activity.
需要替换的代码(顶行)
FragmentLineChooserList newFragment = new FragmentLineChooserList();
FragmentTransaction transaction = FragmentWCLine.this.getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
FragmentWCLine.java
public class FragmentWCLine extends android.support.v4.app.Fragment {
public final static String EXTRA_MESSAGE = "Station_key";
private class WC {
private CharSequence station;
private CharSequence zone;
private Class<? extends Activity> activityClass;
private Class<? extends android.support.v4.app.Fragment> fragmentClass;
public WC(int stationResId, int zoneResId, Class<? extends Activity> activityClass, Class<? extends android.support.v4.app.Fragment> fragmentClass) {
this.fragmentClass = fragmentClass;
this.activityClass = activityClass;
this.station = getResources().getString(stationResId);
this.zone = getResources().getString(zoneResId);
}
@Override
public String toString() { return station.toString(); }
public String getzone(){ return zone.toString(); }
}
private static WC[] mWC;
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_wc_line, container, false);
// Instantiate the list of stations.
mWC = new WC[]{
new WC(R.string.bank, R.string.zone_1, WCBankActivity.class, FragmentWCBank.class),
new WC(R.string.wat, R.string.zone_1, WCWATActivity.class, FragmentWCWAT.class)
};
final ListView listView = (ListView)v.findViewById(R.id.list_wc);
listView.setAdapter(new MyAdapter(getActivity(), mWC));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(mTwoPane){
startActivity(new Intent(getActivity(), mWC[position].fragmentClass));
FragmentLineChooserList newFragment = new FragmentLineChooserList();
FragmentTransaction transaction = FragmentWCLine.this.getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
setItemNormal();
View rowView = view;
setItemSelected(rowView);
}
else{
Intent intent = new Intent(this, mWC[position].activityClass);
String station = mWC[position].station;
intent.putExtra(EXTRA_MESSAGE, station);
startActivity(intent);
}
}
public void setItemSelected(View view){
View rowView = view;
view.setBackgroundColor(Color.parseColor("#66CCCC"));
TextView tv0 = (TextView)rowView.findViewById(R.id.list_item_station);
tv0.setTextColor(Color.parseColor("#000099"));
TextView tv1 = (TextView)rowView.findViewById(R.id.list_item_zone);
tv1.setTextColor(Color.parseColor("#000099"));
}
public void setItemNormal()
{
for (int i=0; i< listView.getChildCount(); i++) {
View v = listView.getChildAt(i);
v.setBackgroundColor(Color.TRANSPARENT);
TextView tv0 = ((TextView) v.findViewById(R.id.list_item_station));
tv0.setTextColor(Color.WHITE);
TextView tv1 = ((TextView) v.findViewById(R.id.list_item_zone));
tv1.setTextColor(Color.parseColor("#B5B5B5"));
}
}
});
return v;
}
static class MyAdapter extends BaseAdapter {
static class ViewHolder {
TextView station;
TextView zone;
}
LayoutInflater inflater;
WC[] mWC;
public MyAdapter(Context contexts, WC[] samples) {
this.mWC = samples;
inflater = LayoutInflater.from(contexts);
}
@Override
public int getCount() {
return mWC.length;
}
@Override
public Object getItem(int position) {
return mWC[position];
}
@Override
public long getItemId(int position) {
return 0;
}
/**set selected position**/
private int selectPosition = -1;
public void setSelectPosition(int position){
if(position!=selectPosition){
selectPosition = position;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_dualline, null);
viewHolder = new ViewHolder();
viewHolder.station = (TextView) convertView.findViewById(R.id.list_item_station);
viewHolder.zone = (TextView) convertView.findViewById(R.id.list_item_zone);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.station.setText(mWC[position].station);
viewHolder.zone.setText(mWC[position].getzone());
//change item color
if(position==selectPosition){
//change item background
convertView.setBackgroundColor(Color.parseColor("#000099"));
//change text color
viewHolder.station.setTextColor(Color.parseColor("#000099"));
}else {
}
return convertView;
}
}
}
WCBankActivity
public class WCBankActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "Station_key";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_wc_bank);
if (savedInstanceState == null) {
// Get the message from the intent
Intent intent = getIntent();
// Notice to specify the sender Activity for the message
String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
FragmentWCBank newFragment = new FragmentWCBank();
FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction()
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
}
}
}
FragmentWCBank
public class FragmentWCBank extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_wc_bank, container, false);
return v;
}
}
错误
现在,我看到变量 mTwoPane
永远不会改变。关于你的问题,代码 FragmentTransaction transaction = FragmentWCLine
和与之相关的代码对我来说没问题。
你的 FragmentTransaction transaction = FragmentMainList
的最高代码。这可能不正确,需要代码:
FragmentTransaction transaction = FragmentWCLine
注:
- FragmentWCLine 是正确的,不是 FragmentMainList。
建议代码,更简洁:
FragmentTransaction transaction = this.getActivity().getSupportFragmentManager().beginTransaction();
注意:在片段中时,getActivity() 应该有效。这样您就不需要指定代码所在的片段 class。
我想我现在明白多了。你真正想要的是......启动另一个 activity 并立即启动预期的片段,这对其他人来说会更清楚。要在另一个 activity 中显示正确的片段,您应该在那个 activity 中进行。有一个很好的 Google 网页@ Starting Another Activity。以下是该网页的代码示例,使用您发布的代码。
代码示例:
WCBankActivity.java:
// Define this for Intent, mainly keep the key consistent between 2 activities
public final String EXTRA_MESSAGE = "Station_key";
FragmentWCLine.java:
if (mTwoPane) {
...
}
else {
Intent intent = new Intent(this, mWC[position].activityClass);
String station = mWC[position].station;
intent.putExtra(WCBankActivity.EXTRA_MESSAGE, station);
startActivity(intent);
}
在其他activity,WCWATActivity.java,如果我理解右:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// Get the message from the intent
Intent intent = getIntent();
// Notice to specify the sender Activity for the message
String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
...
FragmentLineChooserList newFragment = new FragmentLineChooserList();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
}
}
备注:
- 注意关键字符串
EXTRA_MESSAGE
从一个 activity 到另一个。在 WCBankActivity class. 中定义这个常量 final
- 在
onCreate
()中,将站作为消息获取,并进行处理。
- 这是一种将数据从一个 Activity 传递到另一个 Intent.putExtra() 和 getStringExtra() 的技术。
使用 getActivity().getSupportFragmentManager()
代替 FragmentWCLine.getActivity()。
对于我的 master/detail 列表片段,我在尝试查找代码以根据列表项的位置替换详细信息片段时遇到严重问题。基于 需要替换的代码(顶行),我认为顶行需要更改为其他内容。因此,根据我的代码,有人知道该行可以用什么代替吗?所有相关帮助将不胜感激。我想要实现的行为就像主细节流程中的行为一样,如果仅在双窗格模式下,细节片段将被替换为所选列表项的片段。如果在单窗格模式下,选定的列表项将打开 activity.
需要替换的代码(顶行)
FragmentLineChooserList newFragment = new FragmentLineChooserList();
FragmentTransaction transaction = FragmentWCLine.this.getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
FragmentWCLine.java
public class FragmentWCLine extends android.support.v4.app.Fragment {
public final static String EXTRA_MESSAGE = "Station_key";
private class WC {
private CharSequence station;
private CharSequence zone;
private Class<? extends Activity> activityClass;
private Class<? extends android.support.v4.app.Fragment> fragmentClass;
public WC(int stationResId, int zoneResId, Class<? extends Activity> activityClass, Class<? extends android.support.v4.app.Fragment> fragmentClass) {
this.fragmentClass = fragmentClass;
this.activityClass = activityClass;
this.station = getResources().getString(stationResId);
this.zone = getResources().getString(zoneResId);
}
@Override
public String toString() { return station.toString(); }
public String getzone(){ return zone.toString(); }
}
private static WC[] mWC;
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_wc_line, container, false);
// Instantiate the list of stations.
mWC = new WC[]{
new WC(R.string.bank, R.string.zone_1, WCBankActivity.class, FragmentWCBank.class),
new WC(R.string.wat, R.string.zone_1, WCWATActivity.class, FragmentWCWAT.class)
};
final ListView listView = (ListView)v.findViewById(R.id.list_wc);
listView.setAdapter(new MyAdapter(getActivity(), mWC));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(mTwoPane){
startActivity(new Intent(getActivity(), mWC[position].fragmentClass));
FragmentLineChooserList newFragment = new FragmentLineChooserList();
FragmentTransaction transaction = FragmentWCLine.this.getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
setItemNormal();
View rowView = view;
setItemSelected(rowView);
}
else{
Intent intent = new Intent(this, mWC[position].activityClass);
String station = mWC[position].station;
intent.putExtra(EXTRA_MESSAGE, station);
startActivity(intent);
}
}
public void setItemSelected(View view){
View rowView = view;
view.setBackgroundColor(Color.parseColor("#66CCCC"));
TextView tv0 = (TextView)rowView.findViewById(R.id.list_item_station);
tv0.setTextColor(Color.parseColor("#000099"));
TextView tv1 = (TextView)rowView.findViewById(R.id.list_item_zone);
tv1.setTextColor(Color.parseColor("#000099"));
}
public void setItemNormal()
{
for (int i=0; i< listView.getChildCount(); i++) {
View v = listView.getChildAt(i);
v.setBackgroundColor(Color.TRANSPARENT);
TextView tv0 = ((TextView) v.findViewById(R.id.list_item_station));
tv0.setTextColor(Color.WHITE);
TextView tv1 = ((TextView) v.findViewById(R.id.list_item_zone));
tv1.setTextColor(Color.parseColor("#B5B5B5"));
}
}
});
return v;
}
static class MyAdapter extends BaseAdapter {
static class ViewHolder {
TextView station;
TextView zone;
}
LayoutInflater inflater;
WC[] mWC;
public MyAdapter(Context contexts, WC[] samples) {
this.mWC = samples;
inflater = LayoutInflater.from(contexts);
}
@Override
public int getCount() {
return mWC.length;
}
@Override
public Object getItem(int position) {
return mWC[position];
}
@Override
public long getItemId(int position) {
return 0;
}
/**set selected position**/
private int selectPosition = -1;
public void setSelectPosition(int position){
if(position!=selectPosition){
selectPosition = position;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_dualline, null);
viewHolder = new ViewHolder();
viewHolder.station = (TextView) convertView.findViewById(R.id.list_item_station);
viewHolder.zone = (TextView) convertView.findViewById(R.id.list_item_zone);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.station.setText(mWC[position].station);
viewHolder.zone.setText(mWC[position].getzone());
//change item color
if(position==selectPosition){
//change item background
convertView.setBackgroundColor(Color.parseColor("#000099"));
//change text color
viewHolder.station.setTextColor(Color.parseColor("#000099"));
}else {
}
return convertView;
}
}
}
WCBankActivity
public class WCBankActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "Station_key";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_wc_bank);
if (savedInstanceState == null) {
// Get the message from the intent
Intent intent = getIntent();
// Notice to specify the sender Activity for the message
String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
FragmentWCBank newFragment = new FragmentWCBank();
FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction()
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
}
}
}
FragmentWCBank
public class FragmentWCBank extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_wc_bank, container, false);
return v;
}
}
错误
现在,我看到变量 mTwoPane
永远不会改变。关于你的问题,代码 FragmentTransaction transaction = FragmentWCLine
和与之相关的代码对我来说没问题。
你的 FragmentTransaction transaction = FragmentMainList
的最高代码。这可能不正确,需要代码:
FragmentTransaction transaction = FragmentWCLine
注:
- FragmentWCLine 是正确的,不是 FragmentMainList。
建议代码,更简洁:
FragmentTransaction transaction = this.getActivity().getSupportFragmentManager().beginTransaction();
注意:在片段中时,getActivity() 应该有效。这样您就不需要指定代码所在的片段 class。
我想我现在明白多了。你真正想要的是......启动另一个 activity 并立即启动预期的片段,这对其他人来说会更清楚。要在另一个 activity 中显示正确的片段,您应该在那个 activity 中进行。有一个很好的 Google 网页@ Starting Another Activity。以下是该网页的代码示例,使用您发布的代码。
代码示例:
WCBankActivity.java:
// Define this for Intent, mainly keep the key consistent between 2 activities
public final String EXTRA_MESSAGE = "Station_key";
FragmentWCLine.java:
if (mTwoPane) {
...
}
else {
Intent intent = new Intent(this, mWC[position].activityClass);
String station = mWC[position].station;
intent.putExtra(WCBankActivity.EXTRA_MESSAGE, station);
startActivity(intent);
}
在其他activity,WCWATActivity.java,如果我理解右:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// Get the message from the intent
Intent intent = getIntent();
// Notice to specify the sender Activity for the message
String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
...
FragmentLineChooserList newFragment = new FragmentLineChooserList();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
}
}
备注:
- 注意关键字符串
EXTRA_MESSAGE
从一个 activity 到另一个。在 WCBankActivity class. 中定义这个常量 final
- 在
onCreate
()中,将站作为消息获取,并进行处理。 - 这是一种将数据从一个 Activity 传递到另一个 Intent.putExtra() 和 getStringExtra() 的技术。 使用
getActivity().getSupportFragmentManager()
代替 FragmentWCLine.getActivity()。