2011-11-03 53 views
2

我已經創建了一個ListView,它將viewFlipper作爲ListItem。 ViewFlipper有兩個相對的佈局(ID是rView1,rView2)。我爲ListView設置onItemClickListener()和OnItemLongClickListener。 OnLongClickListener()viewFlipper將顯示nextView和onItemClickListener()檢查viewFlipper當前視圖。 案例1 ::如果ViewFlipper的當前視圖是 - rView1,則它開始新的活動。案例2 ::如果ViewFlipper的當前視圖是 - rView2,則它再次翻轉該視圖。 問題是 - 情況2無法正常工作。一些其他觀點正在列表中翻轉。這裏是我的代碼片段 -ViewFlipper用onItemClickListener來翻動listview項目不起作用

ListActivity-

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.d("onCreate", "ListActivity"); 
    YouponDatabase.getInstance(getApplicationContext()); 
    setContentView(R.layout.yp_coupon_list); 
    handler = new Handler(); 
    listView = (ListView) findViewById(R.id.yp_coupon_list); 
    adapter = new ListAdapter(getApplicationContext()); 
    listView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parentView, View view, 
       int position, long column) { 
      View v = ((ViewFlipper) view.findViewById(R.id.view_flipper)) 
        .getCurrentView(); 
      if (v.getId() == R.id.rView1) { 
       Log.d("ListActivity", "detail item click"); 
       Intent intent = new Intent(ListActivity.this, 
         DetailActivity.class); 
       startActivity(intent); 
      } else if (v.getId() == R.id.rView2) { 
       Log.d("ListActivity", "share item click::" 
         + view.toString()); 
       adapter.flipItem(view); 
      } 
     } 
    }); 
    listView.setOnItemLongClickListener(new OnItemLongClickListener() { 

     @Override 
     public boolean onItemLongClick(AdapterView<?> parentView, 
       View view, int position, long column) { 
      Log.d("ListActivity", "item long click"); 
      adapter.flipItem(view, position); 
      return true; 
     } 
    }); 
} 

ListAdapter-

public class ListAdapter extends BaseAdapter { 

private static class ViewHolder { 
    private ViewFlipper flipper; 
    private ImageView listImage; 
    private TextView Area; 
}; 

private LayoutInflater inflater; 
private Context appContext; 
private Handler handler = new Handler(); 
private List<item> currentList; 
private DrawableLoader drawableLoader = null; 
private ViewHolder viewHolder; 

public ListAdapter(Context context) { 
    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    appContext = context.getApplicationContext(); 
    drawableLoader = new DrawableLoader(appContext); 
} 

public void setList(List<item> list) { 
    currentList = list; 
} 

@Override 
public int getCount() { 
    if (currentList != null) 
     return currentList.size(); 
    return 0; 
} 

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

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

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    final item x = currentList.get(position); 

    if (convertView == null) { 
     convertView = inflater.inflate(R.layout.yp_coupon_list_content, 
       null); 
    } 
    viewHolder = (ViewHolder) convertView.getTag(); 
    if (viewHolder == null && convertView.getId() == -1) { 
     viewHolder = new ViewHolder(); 
     viewHolder.flipper = (ViewFlipper) convertView 
       .findViewById(R.id.view_flipper); 
     viewHolder.listImage = (ImageView) convertView 
       .findViewWithTag(appContext.getResources().getString(
         R.string.yp_image_tag)); 
     viewHolder.Area = (TextView) convertView 
       .findViewById(R.id.yp_coupon_area); 
     convertView.setTag(viewHolder); 
    } else { 
     View v = viewHolder.flipper.getCurrentView(); 
     if (v.getId() == R.id.deal_share) { 
      Log.d("list Adapter", "Reset it"); 
      viewHolder.flipper.setDisplayedChild(0); 
     } 
    } 
    viewHolder.Area.setText(x.getArea()); 
    return convertView; 
} 

public void flipItem(View view, int position) { 
    if (view != null) { 
     Log.d("flipItem", "" + view.toString()); 
     ViewFlipper viewFlipper = ((ViewFlipper) view 
       .findViewById(R.id.view_flipper)); 
     if (viewFlipper.getCurrentView().getId() == R.id.deal_detial) { 
      Log.d("flipItem", "now share comes"); 
      viewFlipper.showNext(); 
      ImageView share = (ImageView) view.findViewById(R.id.share); 
      share.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        Log.d("Share clicked", "Inflate layout"); 
       } 
      }); 
     } else { 
      Log.d("flipItem", "now detail comes"); 
      viewFlipper.showNext(); 
     } 

    } 
} 

public void flipItem(View view) { 
    if (view != null) { 
     Log.d("flipItem", "" + view.toString()); 
     ViewFlipper viewFlipper = ((ViewFlipper) view 
       .findViewById(R.id.view_flipper)); 
     viewFlipper.showNext(); 
    } 
} 

請幫助我,我無法弄清楚什麼是錯在我的代碼。

+0

檢查這一個:http://stackoverflow.com/questions/14531897/viewflipper-in-listview-row –

回答