片段不会膨胀正确

时间:2016-01-05 00:21:10

标签: android fragment

我有一个片段在运行时获取数据,我认为导致问题的原因,但我不确定。 它显示按钮两次,我不能滚动。如果我在View v = inflater.inflate(R.layout.fragment_book_view, container, true);

中写下假的真假

它只显示按钮。

片段在运行时加载到活动的FrameLayout中,该布局假设每次都包含不同的片段。

screen shot

我的片段代码: 的java:

package javaproject.project5776;
import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import entities.Book;
import model.backend.Backend;
import model.backend.BackendFactory;
import util.ImageLoader;


/**
 * A simple {@link Fragment} subclass.
 */
public class BookView extends Fragment {

    private Bitmap bitmap;
    private String name;

    public static BookView newInstance(String book,Bitmap bitmap) {

        Bundle args = new Bundle();
        BookView fragment = new BookView();
        fragment.bitmap = bitmap;
        fragment.name = book;
        fragment.setArguments(args);
        return fragment;
    }

    public BookView() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_book_view, container, true);
        Backend backend = BackendFactory.getInstance();
        try {
            Book b = backend.readBook(name);
            ((TextView)v.findViewById(R.id.bookView_name)).setText(b.getBookName());
            ((TextView)v.findViewById(R.id.bookView_author)).setText(b.getAuthor());
            ((TextView)v.findViewById(R.id.bookView_category)).setText(b.getCategory().name());
            ((TextView)v.findViewById(R.id.bookView_description)).setText(b.getDescription());
            ((TextView)v.findViewById(R.id.bookView_pages)).setText(Integer.toString(b.getPages()));
            ((TextView)v.findViewById(R.id.bookView_publishingDate)).setText(b.getPublishingDate());
            final ImageView imageView = (ImageView)v.findViewById(R.id.bookView_image);
            if(bitmap != null){
                imageView.setImageBitmap(bitmap);
            } else{
                ImageLoader imageLoader = new ImageLoader();
                imageLoader.setListener(new ImageLoader.ImageTaskListener() {
                    @Override
                    public void onActionEnd() {}

                    @Override
                    public void onImageDownload(Bitmap bitmap) {
                        imageView.setImageBitmap(bitmap);
                    }
                });
                imageLoader.execute(b.getImage());
            }
        }
        catch (Exception e) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setMessage(e.getMessage());
            builder.create().show();
        }

        return inflater.inflate(R.layout.fragment_book_view, container, false);
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:fillViewport="true">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:layout_width="@dimen/bookview.imageWidth"
            android:layout_height="250dp"
            android:id="@+id/bookView.image"/>
        <TextView
            android:textSize="@dimen/custom_book_text"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_toEndOf="@id/bookView.image"
            android:id="@+id/bookView.name"/>
        <TextView
            android:textSize="@dimen/custom_book_text"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_toEndOf="@id/bookView.image"
            android:layout_below="@id/bookView.name"
            android:id="@+id/bookView.author"/>
        <TextView
            android:textSize="@dimen/custom_book_text"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_toEndOf="@id/bookView.image"
            android:layout_below="@id/bookView.author"
            android:id="@+id/bookView.pages"/>
        <TextView
            android:textSize="@dimen/custom_book_text"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_toEndOf="@id/bookView.image"
            android:layout_below="@id/bookView.pages"
            android:id="@+id/bookView.category"/>
        <TextView
            android:textSize="@dimen/custom_book_text"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_toEndOf="@id/bookView.image"
            android:layout_below="@id/bookView.category"
            android:id="@+id/bookView.publishingDate"/>
        <TextView
            android:id="@+id/bookView.description"
            android:layout_width="match_parent"
            android:layout_margin="10dp"
            android:layout_height="wrap_content"
            android:layout_below="@id/bookView.image"/>
        <Button
            android:background="@color/colorPrimary"
            android:textColor="@android:color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_below="@id/bookView.description"
            android:text="@string/bookView_button"/>
    </RelativeLayout>
</ScrollView>

2 个答案:

答案 0 :(得分:0)

尝试更改

return inflater.inflate(R.layout.fragment_book_view, container, false) ;

return v;

答案 1 :(得分:0)

将第一行第三个参数更改为false,并且正如Lauren.Liuling所说,将最后一行更改为return v;