Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

android - Custom Single choice ListView

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).

http://db.tt/ulP6pn7V

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had a similar situation but fixed that easily. Try this:

  1. Extend ListActivity and set your custom list adapter

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); //if you need title to be hidden
        setContentView(R.layout.activity_add_to_cart_select_service_plan);
        setListAdapter(adapter);
    }
    
  2. Create a field to store selected index position in adapter

    int selectedIndex = -1;
    
  3. Provide an interface to it

    public void setSelectedIndex(int index){
        selectedIndex = index;
    }
    
  4. Set the checked state to true or false based on the selected index inside getView()

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RadioButton rbSelect = (RadioButton) convertView
                            .findViewById(R.id.radio1);
        if(selectedIndex == position){
        rbSelect.setChecked(true);
        }
        else{
        rbSelect.setChecked(false);
        }
    }
    
  5. Set radio button focusable and clickable attribs to 'false'

     <RadioButton
         android:id="@+id/radio1"
         android:checked="false"
         android:focusable="false"
         android:clickable="false"
     />
    
  6. Set listview descendantFocusability attrib to 'beforeDescendants'

    <ListView
        android:id="@android:id/list"
        android:choiceMode="singleChoice"
        android:descendantFocusability="beforeDescendants"
    />
    
  7. Override onListItemClick

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        adapter.setSelectedIndex(position);
        adapter.notifyDataSetChanged();
    }
    

That's it..run and check


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...