1

我想要一个每页显示 10 个元素的列表。但是当列表中有 11 个元素时,我希望我的列表显示 2 页 1 显示前 10 个元素,1 显示剩余元素。但是,如果列表中有 31 个元素,那么它将仅显示 3 页每页 10 个元素,其余 1 个元素没有单独的页面。

此外,当我尝试从左到右滑动列表时,它可以工作到第一页,但从第一页开始它不会转到最后一页。

活动:

public class MainActivity extends Activity implements OnGestureListener {
    private static final int SWIPE_MIN_DISTANCE = 10;
    private static final int SWIPE_THRESHOLD_VELOCITY = 300;

    private GestureDetector gestureScanner;

    Button btnLoad;
    ListView list;
    private String [] names = 
        { "a" , "b", "c", "d", "e", "f", "g", "h", "i", "j",
            "a1" , "b1", "c1", "d1", "e1", "f1", "g1", "h1", "i1", "j1",
            "a2" , "b2", "c2", "d2", "e2", "f2", "g2", "h2", "i2", "j2",
            "a3" , "b3", "c3", "d3", "e3", "f3", "g3", "h3", "i3", "j3","j4"
        }; 

    MyAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gestureScanner = new GestureDetector(this);

        setContentView(R.layout.activity_main);

        btnLoad = (Button) findViewById(R.id.btnLoad);
        list = (ListView) findViewById(R.id.list);

        adapter = new MyAdapter(this, names);

        list.setAdapter(adapter);
        View.OnTouchListener gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return gestureScanner.onTouchEvent(event); 
            }
        };
        list.setOnTouchListener(gestureListener);

        /* 
        btnLoad.setOnClickListener( new OnClickListener() {
            public void onClick(View v) {
                adapter.setNextPage();
            }
        });
        */
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

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

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
            {
                adapter.setNextPage();
                Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
            } 
            else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
            {
                Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
                adapter.setPreviousPage();
            }
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return true;
    }

    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub
    }

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub
    }

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

适配器:

public class MyAdapter extends BaseAdapter{
    private int pageNo = 0 ;
    Context context;
    String [] names;

    public MyAdapter( Context context, String [] names ) {
        this.context = context;
        this.names = names;
    }

    public void setNextPage() {
        pageNo++;
        notifyDataSetChanged();
    }

    public void setPreviousPage() {
        pageNo--;
        notifyDataSetChanged();
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return 10;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return pageNo;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        TextView txt;
        if( convertView == null ) {
            txt = new TextView(context);
            txt.setTextSize(20);
            txt.setPadding(20, 10, 0, 10);
            txt.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }
        else {
            txt = (TextView) convertView;
        }

        int index = ( 10 * pageNo + position );
        if( index >= names.length ) {
            index = 0;
            pageNo = 0;
        }
        txt.setText( names[ index ]);
        return txt;
    }
}
4

0 回答 0