我正在開發一個android項目,我正在努力研究如何使用片段爲我的應用製作平板電腦友好的用戶界面。但我不確定如何根據片段A中發生的情況更新片段B.我知道我需要某種接口,但我無法解決如何實現它。更新片段中的內容
基本上,我所擁有的是一項名爲MainActivity的活動,它設置了片段的佈局。
在橫向模式下,XML文件是。
<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:name="com.BoardiesITSolutions.FragmentTest.FragmentA"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent">
</fragment>
<FrameLayout android:id="@+id/viewer"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent"
android:background="?android:attr/detailsElementBackground">
</FrameLayout>
</LinearLayout>
在縱向模式下其
目前在MainActivity只是設置的內容視圖的XML文件在上面的方法onCreate
內使用SetContentView
。下面是它的外觀。
在它延伸ListFragment,包含項目的ListView和我希望能夠做的是基於什麼是碎片A.
選擇更新片段B內的TextView的FragmentA類文件下面是片段A.代碼
@Override
public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
{
return inflator.inflate(R.layout.fragment_a, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
myListView = getListView();
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("Item1");
arrayList.add("Item2");
arrayList.add("Item3");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_activated_1, arrayList);
setListAdapter(arrayAdapter);
View fragmentB = getActivity().findViewById(R.id.viewer);
mDualPane = fragmentB != null && fragmentB.getVisibility() == View.VISIBLE;
if (savedInstanceState != null)
{
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
if (mDualPane)
{
myListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
showDetails(mCurCheckPosition);
}
}
@Override
public void onListItemClick(ListView l, View view, int position, long id)
{
showDetails(position);
}
private void showDetails(int index)
{
mCurCheckPosition = index;
if (mDualPane)
{
myListView.setItemChecked(index, true);
FragmentB details = (FragmentB)getFragmentManager().findFragmentById(R.id.viewer);
if (details == null || details.getShownIndex() != index)
{
details = FragmentB.newInstance(index);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.viewer, details);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
}
}
else
{
Intent intent = new Intent(getActivity(), FragmentBActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
FragmentB包含下面的代碼,這類擴展片段
public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
{
if (container == null)
{
return null;
}
View view = inflator.inflate(R.layout.fragment_b, container, false);
return view;
}
public static FragmentB newInstance(int index)
{
FragmentB fragmentB = new FragmentB();
Bundle args = new Bundle();
args.putInt("index", index);
//rgs.putString("content", content);
fragmentB.setArguments(args);
return fragmentB;
}
public int getShownIndex()
{
return getArguments().getInt("index", 0);
}
而且在FragmentB活動文件時,它包含以下
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
finish();
return;
}
if (savedInstanceState == null)
{
FragmentB fragmentB = new FragmentB();
fragmentB.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, fragmentB).commit();
}
}
正如你可以從上面的截圖中看到,我已在片段的基礎工作,當我點擊每個項目,它顯示目前選擇的項目是什麼,但我不知道如何告訴它根據用戶從片段a中單擊的內容更新片段b中的textview,以及如何在縱向和橫向模式下處理這些內容。
感謝您提供的任何幫助。
我將如何從第一個片段獲取值,即如果用戶在fragmentA中單擊Item1,則它將該值傳遞給FragmentB,以便它可以顯示在第二個片段上 – Boardy
獲取列表項的值,並將其傳遞給'FragmentB.newInstance()'。正如你所看到的,'showDetails()'已經做了類似的事情,它接受item的索引,並通過'newInstance(index)'方法將它傳遞給FragmentB。 –