在 Listview 的第一行添加 ViewFlipper
Posted
技术标签:
【中文标题】在 Listview 的第一行添加 ViewFlipper【英文标题】:Adding ViewFlipper on first row of a Listview 【发布时间】:2016-04-07 18:18:09 【问题描述】:我想像这样在Listview
的第一行设置一个ViewFlipper
我可以像这样在列表视图顶部设置视图翻转器:
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_
android:layout_>
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_
android:layout_ >
</ViewFlipper>
<ListView android:layout_
android:layout_
android:id="@+id/mainListView">
</ListView>
但是,当滚动 listview
视图翻转器保持在顶部时,现在我的问题是,是否可以在 listview
的顶行设置 ViewFlipper
以便它在 listview
滚动时也滚动。我想我的问题很清楚。请帮忙。
【问题讨论】:
***.com/questions/14531897/… @AmarbirSingh 网址未打开 ***.com/questions/14531897/… 您可以在 listview 上添加虚拟视图。 只需添加 viewflipper 布局作为列表视图的标题 :) 【参考方案1】:只需这样做 .. 添加视图翻转器作为列表视图的标题,如果您在列表视图中只需要一个视图翻转器,它将正常工作。如果您需要更多,那么您应该在适配器 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);
随心所欲地使用脚蹼。如果有任何困惑,请告诉我
【讨论】:
【参考方案2】:问题已解决我已经得到了我的问题的解决方案,通过将 viewflipper
视图添加为 Listview mListView.addHeaderView(flipperView);
的 HeaderView
,现在 viewFlipper 视图也在滚动列表视图项目,我在代码中做了一些更改:
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
convertViewHeader = inflater.inflate(R.layout.myflipperiew , null);
ViewFlipper flipper=(ViewFlipper)convertViewHeader.findViewById(R.id.myflipper);
mListView = (ListView) findViewById( R.id.mainListView );
mListView.addHeaderView(flipper);
【讨论】:
以上是关于在 Listview 的第一行添加 ViewFlipper的主要内容,如果未能解决你的问题,请参考以下文章