1

当我从左向右滑动诗歌按钮列表视图时,它在文章按钮列表视图上移动,与文章按钮相同,以推文按钮列表视图

我是android新手,我想在listview中实现onfling手势,[当我从左向右滑动诗歌按钮listview时,它在文章按钮listview上移动,与文章按钮到tweet按钮listview相同]请提前帮助thanxs

我的代码..

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inspire);       
     DisplayMetrics dm = getResources().getDisplayMetrics();
     REL_SWIPE_MIN_DISTANCE = (int)(120.0f * dm.densityDpi / 160.0f + 0.5); 
     REL_SWIPE_MAX_OFF_PATH = (int)(250.0f * dm.densityDpi / 160.0f + 0.5);
     REL_SWIPE_THRESHOLD_VELOCITY = (int)(200.0f * dm.densityDpi / 160.0f + 0.5);

    Context contex;
    activity = this;

    rowItems = fetchTwitterPublicTimeline();
    rowItems1 = fetchTwitterPublicTimeline1();
    rowItems2 = fetchTwitterPublicTimeline2();

    btn1 = (Button) findViewById(R.id.button1);
    btn2 = (Button) findViewById(R.id.button2);
    btn3 = (Button) findViewById(R.id.button3);

    TextView txtDesc = (TextView) findViewById(R.id.desc);
    TextView txtTitle = (TextView) findViewById(R.id.title);
    ImageView imageView = (ImageView) findViewById(R.id.icon);      

    final ListView listView = (ListView) findViewById(R.id.listview);
    adapter = new InspireAdapter(this, R.layout.row, rowItems);
    listView.setAdapter(adapter);

    final GestureDetector gestureDetector = new GestureDetector(
            new MyGestureDetector());
    View.OnTouchListener gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    };

    listView.setOnTouchListener(gestureListener);       
    OnItemClickListener listener = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                final int position, long id) {
    };

    listView.setOnItemClickListener(listener);

    final ListView listView1 = (ListView) findViewById(R.id.listview);
    adapter1 = new InspireAdapter1(this, R.layout.row1, rowItems1);

    final GestureDetector gestureDetector1 = new GestureDetector(
            new MyGestureDetector1());
    View.OnTouchListener gestureListener1 = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector1.onTouchEvent(event);
        }
    };
    listView1.setOnTouchListener(gestureListener1);

    // listView1.setAdapter(adapter1);
    OnItemClickListener listener1 = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
        }
    };
    listView2.setOnItemClickListener(listener2);

    btn1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            listView1.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
            listView2.setVisibility(View.GONE);

            btn1.setTextColor(Color.BLACK);
            btn2.setTextColor(Color.WHITE);
            btn3.setTextColor(Color.WHITE);
        }
    });

    btn2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {


        }
    });

    btn3.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            listView2.setVisibility(View.VISIBLE);
            listView1.setVisibility(View.GONE);
            listView.setVisibility(View.GONE);
            listView2.setAdapter(adapter2);
            btn3.setTextColor(Color.BLACK);
            btn1.setTextColor(Color.WHITE);
            btn2.setTextColor(Color.WHITE);
        }
    });
}

private void Right2Left() {

    Toast.makeText(this, "Right-to-left fling", Toast.LENGTH_SHORT).show();     
     final ListView listView2 = (ListView) findViewById(R.id.listview);
     adapter2 = new InspireAdapter2(this, R.layout.row2, rowItems2);
     listView2.setAdapter(adapter2);

       btn3.setTextColor(Color.BLACK);
       btn1.setTextColor(Color.WHITE);
       btn2.setTextColor(Color.WHITE);   
}
private void Left2Right() {
     Toast.makeText(this, "Left-to-right fling", Toast.LENGTH_SHORT).show();

     final ListView listView1 = (ListView) findViewById(R.id.listview);
     adapter1 = new InspireAdapter1(this, R.layout.row1, rowItems1);
     listView1.setAdapter(adapter1);

     btn2.setTextColor(Color.BLACK);
     btn1.setTextColor(Color.WHITE);
     btn3.setTextColor(Color.WHITE);        
}

class MyGestureDetector extends SimpleOnGestureListener {       
    @Override 
    public boolean onSingleTapUp(MotionEvent e) {           
         ListView listView =  getListView();
         ListView listView1 = getListView();
         ListView listView2 = getListView();
          return false;
    }
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // TODO Auto-generated method stub
            Log.e("Item Click","Item Click");
        return super.onSingleTapConfirmed(e);
    }

    private ListView getListView() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
        if (Math.abs(e1.getY() - e2.getY()) > REL_SWIPE_MAX_OFF_PATH) 
            return false; 
        if(e1.getX() - e2.getX() > REL_SWIPE_MIN_DISTANCE && 
            Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {               
            Right2Left();                                
        }  else if (e2.getX() - e1.getX() > REL_SWIPE_MIN_DISTANCE && 
            Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {               
            Left2Right();                                
        } else if (e2.getX() - e1.getX() > REL_SWIPE_MIN_DISTANCE && 
            Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {               
            Left2Right123();
        }
        return true; 
    }        

    @Override
    public boolean onDown(MotionEvent e) {           
        return true;
    }    
}
4

1 回答 1

0

作为最佳实现,您的自定义实现并不是您真正可以做的。您可能想改用ViewPager

于 2013-01-02T05:55:43.590 回答