Fragment里面的ListView里面Activity不就是match_parent-ing吗?

ListView inside Fragment inside Activity is not match_parent-ing?

我在 Activity 中使用 Fragment 并且 ListView 位于我的 Fragment.

我已将所有内容设置为 match_parent,但它的工作方式与我预期的不同。

请参考这张图片以便更好地理解http://i.imgur.com/2EK6qzs.jpg (信誉不够=.=无法post图片)

为了节省我们的时间,我省略了大部分不必要的代码,如果您需要我提供更多代码,请告诉我。

谢谢。


activity_book_information.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">

...

<!-- Fragments -->
<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/horizontal_line2" />
</RelativeLayout>

fragment_book_review.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
    android:id="@+id/button_reviewAddReview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/bookInfo_addReview"/>

<ListView
    android:id="@+id/list_review"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

list_review_single.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_row_curve_rectangle_box">

<!-- Name -->
<TextView
    android:id="@+id/book_reviewName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:text="Name"/>

    ...
</LinearLayout>

Java

BookInformationActivity.java

public class BookInformationActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        setContentView(R.layout.activity_book_information);

        f_manager = getFragmentManager();
        f_transaction = f_manager.beginTransaction();
        f_transaction.replace(R.id.scrollView, new BookReviewFragment(), "f_bookReview");
        f_transaction.commit();
    }
    ...
}

BookReviewFragment.java

public class BookReviewFragment extends Fragment {

    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        bundle = this.getArguments();
        return inflater.inflate(R.layout.fragment_book_review, container, false);
    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        list = (ListView) activity.findViewById(R.id.list_review);
        new GetReviewTask().execute();
    }

    public class GetReviewTask extends AsyncTask<Void, Void, Boolean> {

        protected void onPreExecute() {
            ...
        }

        protected Boolean doInBackground(Void... params) {
            ...
        }

        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);

            if(aBoolean != null) {

                ListAdapter adapter = new GetReviewAdapter(activity.getApplicationContext(), R.layout.list_review_single, reviewList);
                list.setAdapter(adapter); // TODO : Fix Layout issue
            }
        }
    }
}

为什么要在 ScrollView 中添加片段??将此滚动视图更改为 LinearLayoutRelativeLayout 即可正常工作。

ScrollViewListView 不能一起正常工作。他们都有自己的滚动 属性.

解决方法(但不是好方法)

但是如果无论如何你想这样做,那么在你的片段中这样做,不过我不建议这样做。

public void setListViewHeightBasedOnChildren()
{
    float offset = 1;

    BaseAdapter listAdapter = (BaseAdapter) gridView.getAdapter();
    int totalHeight = 0;
    final float scale = getResources().getDisplayMetrics().density;
    ViewGroup.LayoutParams params = gridView.getLayoutParams();
    for(int i = 0; i < listAdapter.getCount(); i++)
    {
        View listItem = listAdapter.getView(i, null, gridView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight() + (offset * scale);
    }
    params.height = (int) (totalHeight + (offset * scale * (listAdapter.getCount() - 1)));
    gridView.setLayoutParams(params);
}

尝试将 linearLayout weightSum:"7" 属性设置到您的 xml 中,然后将 layout_weight="1" 用于按钮,将 layout_weight="6" 用于您的 listView(假设列表视图有 space).

的 6/7