gpt4 book ai didi

java - fragment 在两 Pane 平板电脑模式下未正确显示

转载 作者:行者123 更新时间:2023-11-29 20:43:05 25 4
gpt4 key购买 nike

我正在尝试在详细 fragment 中启动我的 fragment 并突出显示选定的列表项(代码在 FragmentWCLine.java 中)以双 Pane 模式但由于某种原因它不会做所以。似乎在单 Pane 模式下工作正常,但当涉及到双 Pane 模式时,它似乎忽略了我在 FragmentWCLine.java 的 if(mTwoPane) 部分中的代码,然后决定启动 Activity 而不是显示详细信息 fragment 中的 fragment 并突出显示所选列表项。在 FragmentWCLine.java 中,我不确定如何处理 startActivity(new Intent(getActivity(), mWC[position].fragmentClass));。此外,FragmentLineChooserList newFragment = new FragmentLineChooserList(); 需要更改为其他内容,但我不知道那会是什么。我怎样才能防止 mTwoPane 代码被忽略,以便它完成它应该做的事情以及我想要实现的上述目标?

WCLineActivity.java

    public class WCLineActivity extends ActionBarActivity {

/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#66CCCC")));
actionBar.setTitle(Html.fromHtml("<font color='#000099'>Hello World</font>"));

if (findViewById(R.id.detail_container) != null) {
mTwoPane = true;
}

FragmentWCLine newFragment = new FragmentWCLine();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.commit();
}
}

fragment WCLine.java

public class FragmentWCLine extends android.support.v4.app.Fragment {

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.
*/
public boolean mTwoPane;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_wc_line, container, false);


if (getActivity().findViewById(R.id.detail_container) != null) {
mTwoPane = true;
}else{
mTwoPane = false;
}

// Instantiate the list of stations.
mWC = new WC[]{
new WC(R.string.bank, R.string.zone_1, WCAActivity.class, FragmentWCA.class),
new WC(R.string.wat, R.string.zone_1, WCBActivity.class, FragmentWCB.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) {
setItemNormal();
View rowView = view;
setItemSelected(rowView);
Fragment newFragment;
switch (position) {
case 0:
newFragment = new FragmentWCA();
break;
case 1:
newFragment = new FragmentWCB();
break;
default:
newFragment = new FragmentWCA();
break;
}
WCLineActivity activity = (WCLineActivity) view.getContext();
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
} else {
Intent intent;
switch (position) {
case 0:
intent = new Intent(getActivity(), WCAActivity.class);
break;
case 1:
intent = new Intent(getActivity(), WCBActivity.class);
break;
default:
intent = new Intent(getActivity(), WCAActivity.class);
break;
}
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;
}


@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());


return convertView;
}
}
}

FragmentWC.java

public class FragmentWC extends android.support.v4.app.Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_wc, container, false);

return v;
}
}

fragment_wc.xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/detail_container">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Bank"
android:id="@+id/textView0"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

fragment_wc_line.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/fragmentwcline">

<ListView
android:id="@+id/list_wc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="false"
android:layout_centerHorizontal="true"/>

</LinearLayout>

activity_main.xml布局

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/master_container"
android:name="com.apptacularapps.exitsexpertlondonlite.FragmentMainList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

activity_main.xml布局(sw600dp)

<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" >

<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/master_container"/>

<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:id="@+id/detail_container"/>

</LinearLayout>

最佳答案

我猜你需要在 fragment 交易之前初始化 boolean 值:

if (findViewById(R.id.detail_container) != null) {
mTwoPane = true;
}
FragmentWCLine newFragment = new FragmentWCLine();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.commit();

更新:

同时更新点击监听器:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(mTwoPane){
setItemNormal();
View rowView = view;
setItemSelected(rowView);
Fragment newFragment;
switch (position){
case 0:
newFragment = new FragmentWCBank ();
break;
case 1:
newFragment = new FragmentWCWAT();
break;
default:
newFragment = new FragmentWCBank ();
break;
}
WCLineActivity activity = (WCLineActivity) view.getContext();
FragmentTransaction transaction = activity .getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
}
else{
Intent intent;
switch (position){
case 0:
intent = new Intent(getActivity(), WCBankActivity.class);
break;
case 1:
intent = new Intent(getActivity(), WCWATActivity.class);
break;
default:
intent = new Intent(getActivity(), WCBankActivity.class);
break;
}
startActivity(intent);
}
}

关于java - fragment 在两 Pane 平板电脑模式下未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30742092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com