动态将OnClickListener分配给自定义布局

时间:2018-11-23 09:25:55

标签: android onclicklistener

在Android中,我创建了一个包含2个主要列的界面。两列都有必须充当按钮的自定义对象。这些自定义对象基于具有2个textviews的简单自定义类(ButtonItem)。我也有一个布局,其中包含带有两个TextViews的LinearLayout(垂直)。在主视图中,我使用自定义类和占位符文本填充,适配器和示例内容这两个列,结果是这些列具有带有2条独立文本行的“按钮”。问题是向这些对象添加OnClickListeners。我的想法是为每个ButtonItem添加一些属性,以在单击时分配一个动作,但是直到现在我还没有成功。

这是填充(更新)列的方式

public class TicketSaleActivity extends Activity {

    private ListView lv;
    private LinearLayout ll;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ticket_sale_layout);

        lv = (ListView) findViewById(R.id.buttonList);
        ll = (LinearLayout)findViewById(R.id.linearLayout);

        ArrayList<ButtonItem> buttonItemList = new ArrayList<>();

        ButtonItem buttonItem1 = new ButtonItem();
        buttonItem1.setUpperText("1€");
        buttonItem1.setBottomText("Billete a Narnia");
        ButtonItem buttonItem2 = new ButtonItem();
        buttonItem2.setUpperText("1.5€");
        buttonItem2.setBottomText("BILLETE A CANGAS DE ONIS");
        ButtonItem buttonItem3 = new ButtonItem();
        buttonItem3.setUpperText("1.25€");
        buttonItem3.setBottomText("Billete a Narnia");
        ButtonItem buttonItem4 = new ButtonItem();
        buttonItem4.setUpperText("2€");
        buttonItem4.setBottomText("BILLETE A CALASPARRA");

        buttonItemList.add(buttonItem1);
        buttonItemList.add(buttonItem2);
        buttonItemList.add(buttonItem3);
        buttonItemList.add(buttonItem4);
        buttonItemList.add(buttonItem1);

        ButtonItemAdapter buttonItemAdapter = new ButtonItemAdapter(this, buttonItemList);
        lv.setAdapter(buttonItemAdapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.i(TicketSaleActivity.class.getSimpleName(),"Clicked");
        }
    });


    for (int i=0;i<buttonItemList.size();i++)
    {
        View item = buttonItemAdapter.getView(i, null, null);
        ll.addView(item);

    }
    }
}

自定义类别:

public class ButtonItem{

    private String upperText;
    private String bottomText;


    public String getUpperText() {
        return upperText;
    }

    public void setUpperText(String upperText) {
        this.upperText = upperText;
    }

    public String getBottomText() {
        return bottomText;
    }

    public void setBottomText(String bottomText) {
        this.bottomText = bottomText;
    }

}

适配器

public class ButtonItemAdapter extends BaseAdapter {

    ArrayList<ButtonItem> buttonItemList;
    Context context;

    public ButtonItemAdapter(Context context, ArrayList<ButtonItem> buttonList)
    {
        this.context=context;
        this.buttonItemList=buttonList;
    }

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

    @Override
    public Object getItem(int position) {
        return buttonItemList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        if(convertView==null)
        {
            convertView = LayoutInflater.from(context).inflate(R.layout.button_list_item,parent, false);

            viewHolder = new ViewHolder();
            viewHolder.upperText = convertView.findViewById(R.id.buttonList_upperText);
            viewHolder.bottomText = convertView.findViewById(R.id.buttonList_bottomText);

            convertView.setTag(viewHolder);//almacenamos los elementos en la vista para reutilizarlos
        }
        else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.upperText.setText(buttonItemList.get(position).getUpperText());
        viewHolder.bottomText.setText(buttonItemList.get(position).getBottomText());
        return convertView;
    }

    static class ViewHolder{
        TextView upperText;
        TextView bottomText;
    }
}

button_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        style="@style/list_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginEnd="1dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/buttonList_upperText"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_weight="1"
            android:text="12€"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/buttonList_bottomText"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="8dp"
            android:layout_weight="2.5"
            android:maxLines="2"
            android:minLines="2"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/buttonList_upperText" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

样式/列表按钮

<style name="list_button" parent="@android:style/Widget.Button">
        <item name="android:gravity">center_vertical|center_horizontal</item>
        <item name="android:textColor">#000000</item>
        <item name="android:shadowColor">#000000</item>
        <item name="android:shadowDx">0</item>
        <item name="android:shadowDy">0</item>
        <item name="android:layout_margin">0.5dp</item>
        <item name="android:shadowRadius">0.1</item>
        <item name="android:textSize">20dip</item>
        <item name="android:textStyle">normal</item>
        <item name="android:background">@drawable/default_button</item>        
        <item name="android:padding">2dp</item>
    </style>

更新:添加了ticket_sale_layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:background="@android:color/darker_gray">

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:baselineAligned="false"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:orientation="vertical"
            android:padding="0dp"
            app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0">

        </LinearLayout>

        <ListView
            android:id="@id/buttonList"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:layout_weight="1"
            android:background="#FFF"
            android:divider="@null"
            android:dividerHeight="0dp"
            app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/sellButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout3"
        tools:background="@android:color/darker_gray">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#FFF"
            android:focusableInTouchMode="false"
            android:maxLines="2"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#FFF"
            android:focusableInTouchMode="false"
            android:text="TextView" />
    </LinearLayout>

    <Button
        android:id="@+id/sellButton"
        style="@style/sell_button"
        android:layout_width="0dp"
        android:layout_height="75dp"
        android:layout_marginStart="4dp"
        android:layout_marginEnd="4dp"
        android:text="@string/btn_sell"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

您可以在适配器的getView方法中绑定点击侦听器,也可以在AdapterView上使用OnItemClickListener

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // ...
    }
});

为了在onItemClick中接收回调,项目视图中不应有任何可单击或可聚焦的视图。因此,请尝试从项目XML布局中删除@style/list_button,或将其替换为其他样式。