首页 > 解决方案 > 主/详细布局未呈现

问题描述

我正在尝试制作一个从 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 天了,所以非常感谢任何帮助!我尝试了许多教程并进行了彻底的搜索,但没有成功!

干杯!

标签: androidandroid-layoutandroid-fragments

解决方案


我相信你有两份 activity_item_list.xml 布局文件,一份是 res/layout/ 文件夹,另一份是 res/layout-sw600dp/ 文件夹。

sw600dp --> 屏幕宽度高于 600dp 使用此布局,否则如果小于 600dp 使用默认布局文件。

此外,您还要确保在两个布局文件中保持相同的属性 ID 相同。


推荐阅读