[英]Fragment with cardslib to fill the whole screen
我嘗試使用本教程制作一個應用程序(示例2)。 我創建了一個導航抽屜活動。
我的主要片段xml(添加的卡片):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_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"
tools:context=".MainActivity$PlaceholderFragment"
android:id="@+id/mainfragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scrollView" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/abovecards">
<it.gmariotti.cardslib.library.view.CardListView
android:id="@+id/myList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
card:list_card_layout_resourceID="@layout/list_card_thumbnail_layout"
style="@style/list_card.thumbnail"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
在我的主要活動活動中,我添加了:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
cardthingy();
}
public void cardthingy(){
int listImages[] = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher1,
R.mipmap.ic_launcher, R.mipmap.ic_launcher1, R.mipmap.ic_launcher};
ArrayList<Card> cards = new ArrayList<Card>();
for (int i = 0; i<5; i++) {
// Create a Card
Card card = new Card(this.getActivity());
// Create a CardHeader
CardHeader header = new CardHeader(this.getActivity());
// Add Header to card
header.setTitle("Test: " + i);
card.setTitle("Some text here");
card.addCardHeader(header);
CardThumbnail thumb = new CardThumbnail(this.getActivity());
thumb.setDrawableResource(listImages[i]);
card.addCardThumbnail(thumb);
cards.add(card);
}
CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(this.getActivity(), cards);
CardListView listView = (CardListView) getActivity().findViewById(R.id.myList);
if (listView != null) {
listView.setAdapter(mCardArrayAdapter);
//this I tried from googling possible solutions
FrameLayout mainy = (FrameLayout) getActivity().findViewById(R.id.container);
mainy.setLayoutParams(new DrawerLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
RelativeLayout fragy = (RelativeLayout) getActivity().findViewById(R.id.mainfragment);
fragy.setLayoutParams(new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
ScrollView feed = (ScrollView) getActivity().findViewById(R.id.scrollView);
feed.setLayoutParams(new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout rLGreen = (LinearLayout) getActivity().findViewById(R.id.abovecards);
rLGreen.setLayoutParams(new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
}
}
問題是它顯示的是一個可滾動屏幕,該屏幕僅占一張卡的空間,而不是整個屏幕的空間: 如您在我的xml和java中所看到的,我已經嘗試了一些方法來解決此問題,但是不幸的是我沒有主意。
嘗試改變
LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT
至
LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT
好。 解決方案非常簡單。 通過從xml中刪除scrollview解決了該問題
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scrollView" >
</ScrollView>
卡列表視圖在經過一定數量的項目后自動變為可滾動的。
而且以下幾行不再是必需的:
//this I tried from googling possible solutions
FrameLayout mainy = (FrameLayout) getActivity().findViewById(R.id.container);
mainy.setLayoutParams(new DrawerLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
RelativeLayout fragy = (RelativeLayout) getActivity().findViewById(R.id.mainfragment);
fragy.setLayoutParams(new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
ScrollView feed = (ScrollView) getActivity().findViewById(R.id.scrollView);
feed.setLayoutParams(new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout rLGreen = (LinearLayout) getActivity().findViewById(R.id.abovecards);
rLGreen.setLayoutParams(new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.