【发布时间】:2018-11-15 11:16:54
【问题描述】:
我正在尝试制作一个从 API 获取图书列表并将其显示在主列表中的应用程序,并在单击时显示详细信息。它适用于手机,但我无法让它适用于平板电脑,而且由于没有错误,我无法找出哪里出错了。
在平板电脑上呈现就像在手机上一样,而不是呈现两个窗格视图。
我正在使用带有片段的单个活动。
“主要”活动:
public class ItemListActivity extends AppCompatActivity {
public static FragmentManager fragmentManager;
private boolean isTwoPane = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
determinePaneLayout();
fragmentManager = getSupportFragmentManager();
if(isTwoPane){
//fragmentManager.beginTransaction().add(R.id.master_dual, new ItemsListFragment()).commit();
fragmentManager.beginTransaction().add(R.id.flDetailContainer, new ItemDetailFragment()).commit();
fragmentManager.beginTransaction().add(R.id.fragmentContainer, new ItemsListFragment()).commit();
} else {
fragmentManager.beginTransaction().add(R.id.fragmentContainer, new ItemsListFragment()).commit();
}
}
private void determinePaneLayout() {
FrameLayout fragmentItemDetail = (FrameLayout) findViewById(R.id.flDetailContainer);
// If there is a second pane for details
if (fragmentItemDetail != null) {
isTwoPane = true;
}
}
项目列表片段:
public class ItemsListFragment extends Fragment {
private ArrayList<Book> bookList;
private ArrayList<String> bookNames;
private ArrayAdapter<Book> bookArrayAdapter;
private ArrayAdapter<String> bookNamesAdapter;
private ApiInterface apiInterface;
private ListView lvItems;
private static final String bookKey = "newBook";
private static Book nBook;
public ItemsListFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_items_list,
container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lvItems = view.findViewById(R.id.lvItems);
bookNames = new ArrayList<>();
//bookNames.add(0, "Gabriel");
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<ArrayList<Book>> call = apiInterface.getBooks();
call.enqueue(new Callback<ArrayList<Book>>() {
@Override
public void onResponse(Call<ArrayList<Book>> call, Response<ArrayList<Book>> response) {
bookList = new ArrayList<>();
bookList = response.body();
bookArrayAdapter = new ArrayAdapter<>(getContext(),
android.R.layout.simple_list_item_activated_1, bookList);
lvItems.setAdapter(bookArrayAdapter);
}
@Override
public void onFailure(Call<ArrayList<Book>> call, Throwable t) {
}
});
lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
nBook = bookList.get(i);
if(view.findViewById(R.id.flDetailContainer) != null){
ItemListActivity.fragmentManager.beginTransaction().replace(R.id.flDetailContainer, ItemDetailFragment.newInstance(nBook)).addToBackStack(null).commit();
} else {
ItemListActivity.fragmentManager.beginTransaction().replace(R.id.fragmentContainer, ItemDetailFragment.newInstance(nBook)).addToBackStack(null).commit();
}
}
});
}`
物品细节片段:
public class ItemDetailFragment extends Fragment {
private static final String bookKey = "newBook";
private static Book nBook;
private TextView title;
private TextView isbn;
private TextView currency;
private TextView price;
private TextView author;
public static ItemDetailFragment newInstance(Book book) {
ItemDetailFragment fragment = new ItemDetailFragment();
Bundle args = new Bundle();
args.putSerializable(bookKey, book);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
nBook = (Book)getArguments().getSerializable(bookKey);
Log.v("BundleOK", "BundleOK");
} else {
Log.v("Bundle Nulo", "Bundle nulo");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate view
View view = inflater.inflate(R.layout.fragment_item_detail,
container, false);
// Return view
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
title = view.findViewById(R.id.tvTitle);
isbn = view.findViewById(R.id.tvIsbn);
currency = view.findViewById(R.id.tvCurrency);
price = view.findViewById(R.id.tvPrice);
author = view.findViewById(R.id.tvAuthor);
title.setText(nBook.getTitle());
isbn.setText("ISBN: " + nBook.getIsbn());
currency.setText("Currency: "+ nBook.getCurrency());
price.setText("Price: "+ String.valueOf((long)nBook.getPrice()/100));
author.setText("Autor: "+nBook.getAuthor());
}
双窗格的 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:showDividers="middle"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/fragmentItemsList"
android:name="gabrielgomes.studio.com.itemretrieve.ItemsListFragment"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
tools:layout="@layout/fragment_items_list" />
<View android:background="#000000"
android:layout_width="1dp"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="@+id/flDetailContainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
主要活动的 XML(项目列表活动)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
项目详细信息片段的 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" >
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="110dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:fontFamily="@font/baskervillebt"
android:gravity="center"
android:text="Item Title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvIsbn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:fontFamily="@font/baskervilleitalicbt"
android:text="Item Body"
android:textSize="16sp" />
<TextView
android:id="@+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvIsbn"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:fontFamily="@font/baskervilleitalicbt"
android:text="Price"
android:textSize="16sp" />
<TextView
android:id="@+id/tvCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvPrice"
android:layout_centerHorizontal="true"
android:fontFamily="@font/baskervilleitalicbt"
android:layout_marginTop="44dp"
android:text="Currency"
android:textSize="16sp" />
<TextView
android:id="@+id/tvAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvCurrency"
android:fontFamily="@font/baskervilleitalicbt"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:text="Author"
android:textSize="16sp" />
</RelativeLayout>
我已经 5 天了,所以非常感谢任何帮助!我尝试了许多教程并彻底搜索但没有成功!
干杯!
【问题讨论】:
-
发布单页布局,包括其全名。例如:
activity_main.xml -
什么不起作用??它的崩溃或其他什么?请说明
标签: android android-layout android-fragments