ChipView上的水平滚动视图

时间:2015-10-23 10:19:11

标签: android horizontal-scrolling horizontalscrollview

我正在使用this ChipView library用于textview芯片,当点击ListView项目时,会将芯片添加到ChipView:

"Search by Name or Email Address"

我需要ChipView的水平滚动视图,但它只是“收缩”,并且当我用Horizo​​ntalScrollView标记包装它时无法像往常一样显示:

Stacked names

它在(垂直)滚动视图中工作正常:

Names in rows

如何以水平滚动方式显示ChipView?

我的XML(ll_searchBar2):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">

<LinearLayout
    android:id="@+id/ll_searchBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:padding="10dp"
            android:scaleType="centerCrop"
            android:src="@drawable/icon_search" />

        <!-- Dummy item to prevent EditText from receiving focus -->
        <LinearLayout
            android:layout_width="0px"
            android:layout_height="0px"
            android:focusable="true"
            android:focusableInTouchMode="true" />

        <EditText
            android:id="@+id/ed_searchField"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Search by Name or Email Address"
            android:imeOptions="actionSearch"
            android:inputType="text"
            android:padding="10dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_searchBar2"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.gamification.gamificationpagestudy.chipview.ChipView
                android:id="@+id/text_my_chip"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ffe0d9" />

        </HorizontalScrollView>

    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:id="@+id/llBottomAction"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_alignParentBottom="true"
    android:background="#c6b0ff"
    android:gravity="center"
    android:orientation="horizontal"
    android:visibility="visible">

    <ImageView
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:scaleType="centerCrop"
        android:src="@drawable/abc_ratingbar_full_material" />

    <TextView
        android:id="@+id/txtInvite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/transparent"
        android:paddingLeft="8dp"
        android:text="INVITE (0)"
        android:textColor="#ffffff" />

</LinearLayout>

<ListView
    android:id="@+id/invitationListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/llBottomAction"
    android:layout_below="@id/ll_searchBar"
    android:background="#ffffff"
    android:divider="@android:color/transparent"
    android:scrollbarStyle="outsideOverlay" />

我的活动:

public class FourteenthActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_twelveth);

    ChipView chipView = (ChipView) findViewById(R.id.text_my_chip);
    chipView.add(new Tag("chip1"));
    chipView.add(new Tag("chip2"));
    chipView.add(new Tag("chip3"));
    chipView.add(new Tag("chip4"));
    chipView.add(new Tag("chip5"));
    chipView.add(new Tag("chip6"));
    chipView.add(new Tag("chip7"));
    chipView.add(new Tag("chip8"));
    chipView.add(new Tag("chip9"));
    chipView.add(new Tag("chip10"));

}
public class Tag implements Chip {
    private String mName;
    private int mType = 0;

    public Tag(String name, int type) {
        this(name);
        mType = type;
    }

    public Tag(String name) {
        mName = name;
    }

    @Override
    public String getText() {
        return mName;
    }

    public int getType() {
        return mType;
    }
 }
}

1 个答案:

答案 0 :(得分:0)

将LinearLayout作为根布局。然后以编程方式添加ChipViews。

LinearLayout.LayoutParams layoutParams  = new LinearLayout.LayoutParams(100 ,100);
ChipView chipView = new ChipView(context);
layoutParams.setMargins(0, 0, 10, 0);
chipView.setLayoutParams(layoutParams);
linearLayuot.addView(chipView);
相关问题