Custom Single choice ListView

前端 未结 4 712
滥情空心
滥情空心 2020-12-01 01:16

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

相关标签:
4条回答
  • 2020-12-01 01:23

    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

    0 讨论(0)
  • 2020-12-01 01:33

    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.

    0 讨论(0)
  • 2020-12-01 01:37

    Handled a similar issue with a Hack (Not a perfect solution..but still works..)

     listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
    
    
    
                    for (int i = 0; i < parent.getCount() ; i ++)
                    {
    
                        View v = parent.getChildAt(i);
                        RadioButton radio = (RadioButton) v.findViewById(R.id.radio);
                        radio.setChecked(false);
    
                    }
    
                        RadioButton radio = (RadioButton) view.findViewById(R.id.radio);
                        radio.setChecked(true);
    
    
    
                }
            });
    
    0 讨论(0)
  • 2020-12-01 01:43

    **

    <TextView
        android:id="@+id/tv_size"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="15sp"
        android:width="200dp" />
    
    <TextView
        android:id="@+id/tv_price"
        android:layout_width="70dp"
        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" />
    

    **

    0 讨论(0)
提交回复
热议问题