主/详细布局未呈现

时间:2018-11-15 11:16:55

标签: android android-layout android-fragments

我正在尝试制作一个应用程序,该应用程序可从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天了,因此对您的帮助非常感谢!我尝试了许多教程,并进行了全面搜索,但是没有去!

干杯!

1 个答案:

答案 0 :(得分:0)

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

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

还要确保两个布局文件中的属性ID相同。