我在Fragment
内使用Activity
而ListView
位于Fragment
内。
我已将所有内容设置为match_parent
,但它的工作方式并非我所期待的。
请参阅此图片以更好地理解http://i.imgur.com/2EK6qzs.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>
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
}
}
}
}
答案 0 :(得分:0)
为什么要在ScrollView
内添加片段?将此滚动视图更改为LinearLayout
或RelativeLayout
,它可以正常工作。
ScrollView
和ListView
无法正常协作。他们都有自己的滚动属性。
解决方法(但不是一种好方法)
但是无论如何你想要这样做,然后在你的片段中这样做,我不会建议它。
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);
}
答案 1 :(得分:0)
尝试设置linearLayout weightSum:&#34; 7&#34;属性到你的xml然后layout_weight =&#34; 1&#34; for button和layout_weight =&#34; 6&#34;对于你的listView(假设列表视图有6/7的空间)。