Android: 从外部按钮点击控制列表视图项目

Android: control listview item from external button click

我的片段中有一个“下一步”按钮和列表视图。 当我点击外部 "Next" 时,我必须更改列表项的背景, 第二次单击它应该转到列表中的下一个项目并更改背景。 当我单击下一步时,它位于最后一行,它应该位于第一行项目。

下面是我的简单 ArrayAdapter

public class MyListAdapter extends ArrayAdapter<String> {

    private List<String> list;
    private Context mContext;

    public MyListAdapter(Context context, int resource,
            int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);
        list = objects;
        mContext = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return super.getItem(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View mView = super.getView(position, convertView, parent);
        return mView;
    }
}

-维护一个指针(从0开始),它记录了改变背景的listview item的位置。

在每次下一次点击时,如果它小于列表的左侧,则将其增加 1 并调用 notifydatasetchanged,否则将其设置为 0 并调用 notifydataSetChanged。

-在适配器class中,在getview中,如果位置小于或等于指针,则更改背景,否则设置为默认值。

一种方法是使用 getChildAt() 您应该维护当前索引 如果用户点击下一步 ->

private int index = 0;
public void nextButtonClicked(){
    updateViewAt(index,false)
    if(index==(listView.getCount()-1)){
      index = 0
    }else{
      index++
   }
    updateViewAt(index,true)
}

private void updateViewAt(int index,boolean isSelected){
    View v = listView.getChildAt(index - 
        listView.getFirstVisiblePosition());

    if(v == null)
       return;

    TextView textView= (TextView) v.findViewById(R.id.text_view);
if(isSelected){
//TODO if view is selected functionality
}else{
//  TODO if view is NOT selected functionality
}
}