活动内部的 ListView 内部的片段未match_parent



我在Activity内使用FragmentListView位于我的Fragment内。

我已经将所有内容设置为match_parent但它的工作方式不是我期望的。

请参阅此图像以更好地了解 https://i.stack.imgur.com/Tzkpg.jpg(声誉不够=.=无法发布图片)

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

谢谢。


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

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();
    }
    ...
}

书评片段.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"用于列表视图(假设列表视图具有6/7的空间)。

相关内容

最新更新