2015-01-03 52 views
0

我是一名Android新手,我遇到了一些似乎對我來說很基本的問題。充氣視圖不適用屬性

我在活動(MainActivty)中有一個片段(MainFragment)。在MainFragment的onCreateView中,我給MainFragment XML佈局充氣,其中包括一個CardStackView(一個自定義AdapterView)並將其適配器設置爲一個CardStackAdapter實例。

填充CardStackView時會出現問題,因爲高度屬性會被忽略。我的目標是包含垂直和水平居中的所有視圖CardStackView。我想要有一個類似卡堆棧的效果(例如類似於Tinder UI)。

Java文件

MainFragment.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

    CardStackView stackView = (CardStackView) rootView.findViewById(R.id.cardStackView); 

    CardStackAdapter adapter = new CardStackAdapter(this.getActivity(), stackView); 
    adapter.add(createCard("name1", "age", 14, 21)); 
    adapter.add(createCard("name2", "age", 15, 21)); 
    adapter.add(createCard("name3", "age", 16, 21)); 

    stackView.setAdapter(adapter); 

    return rootView; 
} 

CardStackView.java

public class CardStackView extends AdapterView<ListAdapter> { 
    private ListAdapter adapter = null; 

    public CardStackView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CardStackView(Context context) { 
     super(context); 
    } 

    @Override 
    public ListAdapter getAdapter() { 
     return adapter; 
    } 

    @Override 
    public void setAdapter(ListAdapter listAdapter) { 
     this.adapter = listAdapter; 
     removeAllViewsInLayout(); 
     requestLayout(); 
    } 

    @Override 
    public View getSelectedView() { 
     throw new UnsupportedOperationException(); 
    } 

    @Override 
    public void setSelection(int i) { 
     throw new UnsupportedOperationException(); 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 

     if (adapter != null) { 
      // Add children if we haven't added any yet 
      if (getChildCount() == 0) { 
       for (int index=0; index < adapter.getCount(); index++) { 
        View newChildView = adapter.getView(index, null, this); 
        addAndMeasureChild(newChildView, index); 
       } 
      } 

      positionItems(); 
     } 
    } 

    private void positionItems() { 
     int numOfElements = getChildCount(); 
     for (int index=0; index < numOfElements; index++) { 
      View child = getChildAt(index); 

      int width = child.getMeasuredWidth(); 
      int height = child.getMeasuredHeight(); 

      child.layout(0, 10*(numOfElements-index), width, height); 
     } 
    } 

    private void addAndMeasureChild(View view, int index) { 
     LayoutParams params = view.getLayoutParams(); 
     if (params == null) { 
      params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     } 

     // Index 0 inserts new child at the beginning of the list (thus rendering it at the back) 
     addViewInLayout(view, 0, params, true); 

     int itemWidth = getWidth(); 
     int itemHeight = getHeight(); 
     view.measure(MeasureSpec.EXACTLY | itemWidth, MeasureSpec.EXACTLY | itemHeight);  
    } 
} 

CardStackAdapter.java

public class CardStackAdapter extends BaseAdapter { 
    private ArrayList<Card> cardsArray; 
    private Context context; 
    private ViewGroup containerView; 
    private LayoutInflater inflater; 

    public CardStackAdapter(Context c, ViewGroup containerView) { 
     super(); 
     this.context = c; 
     this.inflater = LayoutInflater.from(c); 
     this.containerView = containerView; 
     this.cardsArray = new ArrayList<Card>(); 
    } 

    @Override 
    public View getView(int i, View view, ViewGroup parent) { 
     Card card = this.getItem(i); 

     if (view == null) { 
      view = inflater.inflate(R.layout.card_view, parent, false); 
      view.setOnTouchListener(card); 
     } 

     view = inflater.inflate(R.layout.card_view, parent, false); 
     ImageView mPicture = (ImageView) view.findViewById(R.id.picture); 
     TextView mName = (TextView) view.findViewById(R.id.name); 
     TextView mLikes = (TextView) view.findViewById(R.id.likes); 
     TextView mDislikes = (TextView) view.findViewById(R.id.dislikes); 

     mName.setText(card.getName()); 
     mLikes.setText(card.getLikes().toString()); 
     mDislikes.setText(card.getDislikes().toString()); 
     mPicture.setImageDrawable(card.getPicture()); 

     return view; 
    } 

    public void add(Card card) { 
     cardsArray.add(card); 
     notifyDataSetChanged(); 
    } 

    @Override 
    public Card getItem(int i) { 
     return cardsArray.get(i); 
    } 

    @Override 
    public long getItemId(int i) { 
     return i; 
    } 

    @Override 
    public int getCount() { 
     return cardsArray.size(); 
    } 
} 

**佈局文件**

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    tools:context=".MainActivity$PlaceholderFragment" 
    android:focusable="false" 
    android:id="@+id/linearLayout"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:text="Top text" /> 

    <jorgerdg.likeornot.CardStackView 
     android:layout_width="match_parent" 
     android:layout_height="350dp" 
     android:id="@+id/cardStackView" 
     android:layout_margin="10dp" 
     android:background="#fffffec7"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText2" 
     android:text="Top text" /> 
</LinearLayout> 

card_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="215dp" 
    android:layout_height="250dp" 
    android:orientation="vertical" 
    android:gravity="center_vertical|center_horizontal" 
    android:layout_gravity="center" 
    android:padding="5dp" 
    android:background="@drawable/card_border" 
    > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/picture" 
     android:background="#ffd1ff7e" 
     android:layout_weight="0.95" 
     android:layout_gravity="center_horizontal" 
     /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.05" 
     > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:id="@+id/name" 
      android:text="Yorch, 22" 
      android:layout_weight="0.8" 
      android:textSize="20dp" 
      android:singleLine="true" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="21" 
      android:id="@+id/likes" 
      android:layout_weight="0.1" 
      android:textSize="20dp" 
      android:textColor="#ff26ff00" 
      android:textAlignment="center" 
      android:gravity="center_horizontal" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="13" 
      android:id="@+id/dislikes" 
      android:layout_weight="0.1" 
      android:textSize="20dp" 
      android:textColor="#ffff0060" 
      android:textAlignment="center" 
      android:gravity="center_horizontal" /> 
    </LinearLayout> 
</LinearLayout> 

card_border.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
    <stroke 
     android:width="1dp" 
     android:color="@android:color/darker_gray" /> 
    <solid android:color="@android:color/white" /> 
</shape> 

** **截圖

這就是我得到:

problem screenshot

這是我想要得到什麼:

enter image description here

回答

1

你可以試試

View rootView = inflater.inflate(R.layout.fragment_main, container, true); 
view = inflater.inflate(R.layout.card_view, parent, true); 

這樣做,您將該視圖附加到擴展該父項屬性的父項。