更改Listview焦点

时间:2014-06-26 10:11:51

标签: android listview

当一个按钮(外部 - 不在列表项目中)单击列表视图时,我想关注下一个列表项。 我试过这样但是它不起作用:

vegetableList.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, vegList));
vegetableList.setOnItemClickListener(new OnItemClickListener() 
{

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            {
                current_veg_item = vegList[position];
                parent.getChildAt(position).setBackgroundColor(Color.parseColor("#33cc99"));

                if (save != -1 && save != position)
                {
                    parent.getChildAt(save).setBackgroundColor(Color.WHITE);
                }
                save = position; 
        }
});

按钮OnClickListener

OnClick{
    vegetableList.setItemChecked(3, true);
}

当我点击按钮时,如何关注当前所选项目的下一项?

2 个答案:

答案 0 :(得分:0)

我认为你的代码应该有效,但为什么不工作我不知道&amp;
你可以尝试这样。

ItemAdapter itemAdapter = new ItemAdapter (vegList);
list.setAdapter(itemAdapter );



public class ItemAdapter extends BaseAdapter
{
  ArrayList<String> vegList;
  public ItemAdapter(ArrayList<String> vegList) 
  {
     this.vegList= vegList;
  }

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    convertView = getLayoutInflater().inflate(R.layout.item_cell, null);


    TextView tvItem = (TextView) convertView.findViewById(R.id.tvItem);
    tvItem .setText(vegList.get(position));
    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

             current_veg_item = vegList[position];
            convertView.setBackgroundColor(Color.parseColor("#33cc99"));

            if (prvsview!=null && prvsview!= convertView)
            {
                prvsview.setBackgroundColor(Color.WHITE);
            }
            prvsview= convertView; 
        }
    });
    return convertView;
}

答案 1 :(得分:0)

mList.performItemClick(
        mList.getAdapter().getView(mActivePosition, null, null),
        mActivePosition,
        mList.getAdapter().getItemId(mActivePosition));