自定义单选ListView [英] Custom Single choice ListView

查看:37
本文介绍了自定义单选ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个自定义列表视图,在一行中包含两个 TextView 和一个单选按钮.在列表项上单击单选按钮状态应该是切换.我不能在这里使用简单适配器.

我已经问过这个问题 .

虽然我还没有实施它建议的解决方案,但听起来它走在正确的轨道上.

基本上,单项选择 ListView 期望您提供给它的小部件实现 Checkable 接口.LinearLayout 等人没有.因此,您需要创建一个自定义布局,该布局继承 LinearLayout(或您希望用于项目的任何布局)并实现必要的接口.

I want to make a custom List View having Two TextViews and a radio Button in a single row. And on listitem click the radio button state should be toggle. I cannot use Simple Adapter here.

I have already asked that question Single choice ListView custom Row Layout but don't find any satisfactory solution.

What I am currently doing is I am using simple_list_item_single_choice and putting data of both TextViews in a single one separated by some white spaces. But here it is getting worse (shown in the image below).

What I want is to fix the location of size and price and make list view as single choice.

XML Layout for the list can be something like:

**<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_size"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="15sp"
        android:width="200dp" />

    <TextView
        android:id="@+id/tv_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="15sp"
        android:width="70dp" />

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>**

How to make custom adapter for that?

解决方案

I've been searching for an answer to this problem all morning, and the most useful thing I've found so far is this article.

While I haven't implemented the solution it suggests yet, it sounds like it's on the right track.

Basically, the single-choice ListView expects the widgets you provide it with to implement the Checkable interface. LinearLayout et al don't. So you need to create a custom layout that inherits LinearLayout (or whatever layout you want to use for your items) and implements the necessary interface.

这篇关于自定义单选ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆