开发者

Using Android, how can I select rows from a ListView which contains Button controls

I have the following, very simple test program for using a ListView. I create a ListView and set it as the content view. I set a ListAdapter which supplies the rows. There are 30 rows, and each row consists of a LinearLayout ViewGroup. Into that ViewGroup, I place a TextView and a Button. When I run the program, I find that I cannot select rows of the list. I can, however, scroll the list and click the button.

If I remove the button from the LinearLayout (so that it contains only the TextView), then I am able to select rows of the list. I would like to be able to have buttons on my individual row views, and still be able to select rows of the list. On another forum, someone said that this was possible, but I am at a loss as to how to accomplish it.

Can anyone give me a clue?

Thanks.

public class ListViewTest extends Activity implements ListAdapter 
{
 int    m_count;
 DataSetObserver m_observer;

 public ListViewTest()
 {
  m_count  = 30;
  m_observer = null;
 }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        ListView lv = new ListView(this);
        lv.setAdapter(this);
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        setContent开发者_运维知识库View(lv);
   }

 @Override
 public boolean areAllItemsEnabled() {
  return true;
 }

 @Override
 public boolean isEnabled(int position) {
  return true;
 }

 @Override
 public int getCount() 
 {
  return m_count;
 }

 @Override
 public Object getItem(int position) {
  return null;
 }

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

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

 @Override
 public View getView(int position, View convertView, ViewGroup parent) 
 {
  LinearLayout vg = new LinearLayout(this);

  TextView tv = new TextView(this);
  tv.setText("ListItem");

  Button bv = new Button(this);
  bv.setText("Button");

  vg.addView(tv);
  vg.addView(bv);

  return(vg);
 }

 @Override
 public int getViewTypeCount() {
  return 1;
 }

 @Override
 public boolean hasStableIds() {
  return false;
 }

 @Override
 public boolean isEmpty() {
  return false;
 }

 @Override
 public void registerDataSetObserver(DataSetObserver observer) 
 {
  m_observer = observer;
 }

 @Override
 public void unregisterDataSetObserver(DataSetObserver observer) {

 }
}


As the other answers point out, whether or not you can select ListView rows as full items on their own depends on whether or not those rows contain focusable items. However, the solution is usually not setting focusable=false on your buttons or the like. That will prevent your app from being navigable with a d-pad, trackball, arrow keys, or what have you.

You want your list items to be able to control their own focus properties. You want setItemsCanFocus. This will disable the special focus/selection handling that ListView normally uses to treat list items as a single unit.

Now you can set a listener on the layout you use as the top-level element in your rows, set a stateful background drawable on it to display focus/press state, as well as focusLeft/Right properties to control focus shifting within the item itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜