用onItemClickListener翻转listview项的ViewFlipper不起作用

时间:2011-11-03 12:43:24

标签: android listview viewflipper onitemclicklistener

我创建了一个ListView,它将viewFlipper作为ListItem。 ViewFlipper有两个相对布局(id是rView1,rView2)。我为ListView设置onItemClickListener()和OnItemLongClickListener。

OnLongClickListener()viewFlipper将显示nextView和onItemClickListener()检查viewFlipper当前视图。 案例1 ::如果ViewFlipper的当前视图是-rView1,那么它将启动新活动。 情况2 ::如果ViewFlipper的当前视图是-rView2,那么它再次翻转该视图。 问题是 - 案例2无法正常工作。其他一些观点在列表中翻转。这是我的代码片段 -

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();
    }
}

请帮助我,我无法弄清楚我的代码中有什么问题。

1 个答案:

答案 0 :(得分:0)

确保正确实施视图持有者回收概念。请参阅以下链接答案以获取视图持有者实施。

https://stackoverflow.com/a/44106602/651377