在Listview的第一行添加ViewFlipper

时间:2016-01-04 06:48:24

标签: android android-listview android-viewpager

我想在ViewFlipper的第一行设置Listview,就像这样

enter image description here

单独我可以在listview上设置视图翻转器,如下所示:

mListView = (ListView) findViewById( R.id.mainListView );
viewFlipper =(ViewFlipper)findViewById(R.id.viewFlipper1);
setViewFlipperView();
CustomGestureDetector customGestureDetector = new CustomGestureDetector();
mGestureDetector = new GestureDetector(this, customGestureDetector);

String[] abc = new String[] { "A", "B", "C", "D",
                                  "E", "F", "G", "H","I","J","K","L","M","N","O","P","Q","R","S"
                                  ,"T","U","V","W","X","Y","Z"};  
ArrayList<String> abcList = new ArrayList<String>();
abcList.addAll( Arrays.asList(abc) );

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, abcList);
mListView.setAdapter( listAdapter );

private View setViewFlipperView(){
  int[] resources = {
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher
    };

    for (int i = 0; i < resources.length; i++) {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(resources[i]);
        viewFlipper.addView(imageView,i);
    }
  return viewFlipper;

 }
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
     mGestureDetector.onTouchEvent(ev);
    return super.dispatchTouchEvent(ev);
}
 @Override
public boolean onTouchEvent(MotionEvent event) {
    mGestureDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}
 class CustomGestureDetector extends GestureDetector.SimpleOnGestureListener    {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float   velocityX, float velocityY) {
             // Swipe left (next)
            if (e1.getX() > e2.getX()) {
                viewFlipper.setInAnimation(context, R.anim.left_in);
                viewFlipper.setOutAnimation(context, R.anim.left_out);

                viewFlipper.showNext();
            }

            // Swipe right (previous)
            if (e1.getX() < e2.getX()) {
                viewFlipper.setInAnimation(context, R.anim.right_in);
                viewFlipper.setOutAnimation(context, R.anim.right_out);

                viewFlipper.showPrevious();
            }

            return super.onFling(e1, e2, velocityX, velocityY);
        }
    }

和xml看起来像:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ViewFlipper
    android:id="@+id/viewFlipper1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ViewFlipper>

<ListView android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:id="@+id/mainListView">
</ListView>

但滚动listview视图鳍状肢时,我的问题是,是否可以在ViewFlipper的顶行设置listview,以便在listview时滚动if($x > 1 && $x <= 30){ statement }else{ statement } {1}}滚动。我想我的问题很清楚。请帮忙。

2 个答案:

答案 0 :(得分:2)

只需这样做..添加视图鳍状肢作为listview的标题,如果在列表视图中只需要一个视图鳍状肢,它将正常工作。如果您需要更多,那么您应该在适配器getView()中执行此操作。

LayoutInflater  inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
convertViewHeader = inflater.inflate(R.layout.YourFlipperview , null);
ViewFlipper flipper=(ViewFlipper)convertViewHeader.findViewById(R.id.flipperId); 

listView.addHeaderView(convertViewHeader, null, false);

根据您的意愿使用鳍状肢。如果有任何混淆,请告诉我

答案 1 :(得分:0)

问题已解决我通过将viewflipper视图添加为Listview HeaderView的{​​{1}}来获得我的问题的解决方案,现在viewFlipper视图也在滚动列表视图我在代码中完成的项目很少:

mListView.addHeaderView(flipperView);