我是一名使用Android的新手,我遇到了一些对我来说很基本的问题。
我在活动(MainFragment
)中有一个片段( MainActivty
)。在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>
**截图**
这就是我得到的:
这就是我想要的:
答案 0 :(得分:1)
你可以试试
View rootView = inflater.inflate(R.layout.fragment_main, container, true);
view = inflater.inflate(R.layout.card_view, parent, true);
这样做,您将该视图附加到扩展该父级属性的父级。