-1

我有一个 List 类和一个 Viewflipper 类,我怎么知道从 Viewflipper 类的 List 类中单击了哪个项目,如果可能的话,我还想将每个视图链接到 Viewflipper 的特定视图。这是我到目前为止所尝试的:

列表类:

public class List extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // storing string resources into Array
    String[] service_list = getResources().getStringArray(R.array.List);

    // Binding resources Array to ListAdapter
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list, R.id.list, service_list));

}

@Override
protected void onListItemClick(ListView parent, View v, int position, long id) {
    // TODO Auto-generated method stub
     String name = (String) parent.getItemAtPosition(position); 
     //Toast.makeText(this, "you have clicked " + name, Toast.LENGTH_SHORT).show();


    switch (position) {
    case 0:
        Intent i = new Intent(List.this, Viewflipper.class);
        Bundle b = new Bundle();
        b.putString("a", "apple");
        i.putExtras(b);
        startActivity(i);
        break;

    case 1:
        Intent a = new Intent(List.this, Viewflipper.class);
        Bundle aa = new Bundle();
        aa.putString("b", "banana");
        a.putExtras(aa);
        startActivity(a);

        break;

    case 2:
        Intent ab = new Intent(List.this, Viewflipper.class);
        Bundle bb = new Bundle();
        bb.putString("m", "mango");
        ab.putExtras(bb);
        startActivityForResult(ab, 2);
        break;

    case 3:
        Intent c = new Intent(List.this, Viewflipper.class);
        Bundle cc = new Bundle();
        cc.putString("o", "orange");
        c.putExtras(cc);
        startActivity(c);
         break;
    default:
        break;
    }

    super.onListItemClick(parent, v, position, id);
}
} 

ViewFlipper 类:

      public class Viewflipper extends Activity implements OnClickListener,
      OnGestureListener {

private ViewFlipper viewflipper;
private Button btnNext, btnPrevious;
private GestureDetector gesturedetector = null;

TextView txt;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewflipper);

    viewflipper = (ViewFlipper) findViewById(R.id.viewSwitcher);
    btnNext = (Button) findViewById(R.id.btnNext);
    btnPrevious = (Button) findViewById(R.id.btnPrevious);
    txt = (TextView) findViewById(R.id.txtview);

    txt.setOnClickListener(this);
    btnNext.setOnClickListener(this);
    btnPrevious.setOnClickListener(this);

    gesturedetector = new GestureDetector(this, this);

    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {



        if (bundle.getString("a").equals("apple")) {
            Toast.makeText(this, " the first Item was clicked ",
                    Toast.LENGTH_LONG).show();
        } else if (bundle.getString("b").equals("banana")) {
            Toast.makeText(this, "the second Item was clicked",
                    Toast.LENGTH_LONG).show();
        } else if (bundle.getString("m").equals("mango")) {
            Toast.makeText(this, "the fourth Item was clicked",
                    Toast.LENGTH_LONG).show();
        } else if (bundle.getString("o").equals("orange")) {
            Toast.makeText(this, "the fifth Item was clicked",
                    Toast.LENGTH_LONG).show();
        }

        else {


        } // Toast.makeText(this, b , Toast.LENGTH_SHORT).show() ;
    }
}
// Animation
@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnNext:
        // Button Next Style
        Animation animationNext = AnimationUtils.loadAnimation(this,
                R.anim.btn_style_next);
        btnNext.startAnimation(animationNext);

        nextView();
        break;

    case R.id.btnPrevious:
        // Button Previous Style
        Animation animationPrevious = AnimationUtils.loadAnimation(this,
                R.anim.btn_style_previous);
        btnPrevious.startAnimation(animationPrevious);

        previousView();
        break;

    case R.id.txtview:
        Toast.makeText(this, "dail contact or visit website",
                Toast.LENGTH_SHORT).show();
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return gesturedetector.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
}

int SWIPE_MIN_VELOCITY = 100;
int SWIPE_MIN_DISTANCE = 100;

// Sự kiện khi bạn vuốt màn hình đưa sang một bên nào đó
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // Get Position
    float ev1X = e1.getX();
    float ev2X = e2.getX();

    // Get distance of X (e1) to X (e2)
    final float xdistance = Math.abs(ev1X - ev2X);
    // Get veclocity of cusor
    // Vận tốc = số điểm ảnh (px) / giây
    final float xvelocity = Math.abs(velocityX);

    // Vận tốc chuyển đổi X > 100 và khoảng cách từ điểm kéo đầu đến điểm
    // kéo cuối > 100
    if ((xvelocity > SWIPE_MIN_VELOCITY)
            && (xdistance > SWIPE_MIN_DISTANCE)) {
        if (ev1X > ev2X)// Switch Left
        {
            previousView();
        } else// Switch Right
        {
            nextView();
        }
    }

    return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    return false;
}

// Next, Previous Views
private void previousView() {

    // Previous View
    viewflipper.setInAnimation(this, R.anim.in_animation1);
    viewflipper.setOutAnimation(this, R.anim.out_animation1);
    viewflipper.showPrevious();

}

private void nextView() {

    // Next View
    viewflipper.setInAnimation(this, R.anim.in_animation);
    viewflipper.setOutAnimation(this, R.anim.out_animation);
    viewflipper.showNext();

}

}

请帮忙。谢谢你。

4

1 回答 1

0

首先,我建议更改List代码,使其如下所示:

switch (position) {
Intent i = new Intent(List.this, Viewflipper.class);
case 0:
    i.putExtra ("key", "apple");
    startActivity(i);
    break;

case 1:
    i.putExtra ("key", "banana");
    startActivity(i);
    break;

case 2:
    i.putExtra ("key", "mango");
    startActivity(i);
    break;

case 3:
    i.putExtra ("key", "orange");
    startActivity(i);
    break;
default:
    break;
}

然后在 中Viewflipper,更改您的代码,使其如下所示:

if (getIntent().hasExtra("key")) {

String extra = getIntent().getStringExtra ("key");

    if ("apple".equals (extra)){
        Toast.makeText(this, " the first Item was clicked ",
                Toast.LENGTH_LONG).show();
    } else if ("banana".equals (extra)){
        Toast.makeText(this, "the second Item was clicked",
                Toast.LENGTH_LONG).show();
    } else if  ("mango".equals (extra)){
        Toast.makeText(this, "the fourth Item was clicked",
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "the fifth Item was clicked",
                Toast.LENGTH_LONG).show();
    }
}

我改变了什么:

  • Intent在开关的开头声明,以节省一些代码。
  • 使其额外传递的始终具有相同的密钥。您之前所做的(每种情况使用一个单独的键效果不佳,您可以获得 NPE 并且它破坏了键值对的要点。
  • 取消了 Bundle,因为它是多余的。Intent#putExtra()并且Intent#getStringExtra()很好。
  • 既然方法可以处理值而不抛出"string".equals (stringVariable).String#equals()nullNPE
于 2013-02-13T00:19:38.850 回答