我是初学Android开发者。
我需要通过点击当前项目上的按钮来更改listview中下一项的布局。我不明白如何访问下一个项目。
那么,如何更改下一个项目的布局?
这是我的代码:
适配器类
public class StepsAdapter extends ArrayAdapter<String> {
private static final int VIEW_TYPE_FIRST = 0;
private static final int VIEW_TYPE_NEXT = 1;
private static final int VIEW_TYPE_COUNT = 2;
private List<String> steps;
private Context mContext;
public StepsAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
mContext = context;
steps = objects;
}
public static class ViewHolder {
public final Button btnDone;
public final TextView txtView;
public final TextView txtViewMain;
public ViewHolder(View view) {
btnDone = (Button)view.findViewById(R.id.right);
txtView = (TextView)view.findViewById(R.id.list_item_step_textview_finish);
txtViewMain = (TextView)view.findViewById(R.id.list_item_step_textview);
}
}
@Override
public int getItemViewType(int position) {
return (position != 0) ? VIEW_TYPE_FIRST : VIEW_TYPE_NEXT;
}
@Override
public int getViewTypeCount() {
return VIEW_TYPE_COUNT;
}
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
int viewType = getItemViewType(position);
int layoutId = -1;
switch (viewType){
case VIEW_TYPE_FIRST: {
layoutId = R.layout.list_item_step;
break;
}
case VIEW_TYPE_NEXT: {
layoutId = R.layout.list_item_first_step;
break;
}
}
View view = LayoutInflater.from(mContext).inflate(layoutId, parent, false);
final ViewHolder viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
viewHolder.txtViewMain.setText(steps.get(position));
viewHolder.btnDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
int listVCount = listView.getCount();
int position = listView.getPositionForView(parentRow);
viewHolder.txtView.setVisibility(View.VISIBLE);
viewHolder.btnDone.setVisibility(View.GONE);
String textFinishStep = "Passed step " + (position + 1) + " : " + viewHolder.txtViewMain.getText();
viewHolder.txtView.setText(textFinishStep);
viewHolder.txtViewMain.setVisibility(View.GONE);
}
});
return view;
}
}
答案 0 :(得分:0)
在您的StepsAdapter中,按如下所示选择一个字段
private int cposition=-1;
并采取以下一种方法
public void setPosition(int cposition){
this.cposition=cposition;
}
并在
yourListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
adapter.setPosition(position);
adapter.notifyDatasetChanged();
}
});
并在
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
if(cposition!=-1&&position=cposition+1){
//change list item layout here
cposition=-1;
}
}
希望这会对你有所帮助。