2015-06-15 84 views
0

我是初學者的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

在你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; 
     } 
} 

希望這將幫助你。

+0

Yeap!有用!!謝謝!!)) –