2014-04-09 52 views
10

我建立一个Android应用程序,需要一个“张牌” UI控件的Android StackView类型的布局。与水平刷卡

这是一个活动/布局的例子,可以执行以下操作的请求:

1)垂直滑动支持:

列表的可垂直滚动扑克牌/刷卡。 StackView这样做,但我愿意行之有效任何解决方案(例如,一些CardUI项目)

2)横向轻扫支持:

对于任何卡,有2个字典定义:

  • 如果我们做一个左刷 - 然后我们就可以看到清晰A.
  • 如果我们做了正确的刷卡,我们看到的清晰度B
  • 水平轻扫更新不更新整个屏幕布局,只是被刷过的卡片。所以,如果我刷卡卡2来看看A2的权利,我仍然有卡1落后A2

例子:

[A1][Card1][B1] 
[A2][Card2][B2] 
[A3][Card3][B3] 

我没有看到这个相关post here,答案也提供了一些提示和参考信息..但不幸的是,我仍然试图弄清楚。

回答

8

您有两种可能的方法:采取一些开源项目并根据您的需要进行调整,否则,从详细教程构建您的卡片刷卡作为图像滑块。

对于第一个选项,看看Github,在那里你会发现几个小型项目,通常在垂直或水平滚动上做一副牌features。我猜你可能感兴趣的以下项目:

  • CardDeck:Deck of Cards为Android

  • DeckPicker:与卡预览的浏览器屏幕中的一些附加功能完整的Android anki机器人项目。在预览屏卡将显示类似浏览模式上card browser窗口。

如果您所做的其他更改看起来不错结束时,它可以是值得补丁提交到原始项目。

对于第二种选择,对于情况下,你更愿意从头开始实现它,然后采取简单的步骤,越来越多的项目进入更具体的/复杂的细节在你的意愿进行定制。全屏图像滑块将适合该法案,这将包括用于查看页面的活动:

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

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</LinearLayout> 

并配有全屏幕观看者Java class

public class FullScreenImageAdapter extends PagerAdapter { 

    private Activity _activity; 
    private ArrayList<String> _imagePaths; 
    private LayoutInflater inflater; 

    // constructor 
    public FullScreenImageAdapter(Activity activity, 
      ArrayList<String> imagePaths) { 
     this._activity = activity; 
     this._imagePaths = imagePaths; 
    } 

    @Override 
    public int getCount() { 
     return this._imagePaths.size(); 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return view == ((RelativeLayout) object); 
    } 

    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     ImageView imgDisplay; 
     Button btnClose; 

     inflater = (LayoutInflater) _activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container, 
       false); 

     imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay); 
     btnClose = (Button) viewLayout.findViewById(R.id.btnClose); 

     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options); 
     imgDisplay.setImageBitmap(bitmap); 

     // close button click event 
     btnClose.setOnClickListener(new View.OnClickListener() {   
      @Override 
      public void onClick(View v) { 
       _activity.finish(); 
      } 
     }); 

     ((ViewPager) container).addView(viewLayout); 

     return viewLayout; 
    } 

    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     ((ViewPager) container).removeView((RelativeLayout) object); 

    } 
} 

然后你实现图像滑动horizontally,如: enter image description here

并添加垂直运动,只是包括额外的垂直布局:

main.xml 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    android:orientation="vertical" > 

    <TextView 

     android:layout_width="fill_parent" 

     android:layout_height="wrap_content" 

     android:text="Swipe Demo" 

     android:gravity="center" 

     android:layout_margin="10dip" /> 

     <LinearLayout 

      android:layout_width="fill_parent" 

      android:layout_height="fill_parent" 

      android:orientation="vertical" 

      android:gravity="center"> 

       <ImageView 

        android:layout_width="wrap_content" 

        android:layout_height="wrap_content" 

        android:layout_gravity="center" 

        android:gravity="center" 

        android:layout_margin="10dip" 

        android:id="@+id/image_place_holder"/> 

      </LinearLayout> 

</LinearLayout> 

,这将使你滑的东西vertically

enter image description here

在这一天结束时,你想拥有的是一个网格,如垂直和水平滚动起来。对于你有水平和垂直滑动结合成一个table layout

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

    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent"> 

     <TableLayout 
        android:id="@+id/amortization" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 

       <TableRow 
        android:background="#ffff00"> 
        <TextView 
         android:text="@string/amortization_1" 
         android:padding="3dip"/> 
        <TextView 
         android:text="@string/amortization_2" 
         android:padding="3dip"/> 
        <TextView 
         android:text="@string/amortization_3" 
         android:padding="3dip"/> 
        <TextView 
         android:text="@string/amortization_4" 
         android:padding="3dip"/> 
        <TextView 
         android:text="@string/amortization_5" 
         android:padding="3dip"/> 
        <TextView 
         android:text="@string/amortization_6" 
         android:padding="3dip"/> 
        <TextView 
         android:text="@string/amortization_7" 
         android:padding="3dip"/> 
       </TableRow> 
     </TableLayout> 
    </HorizontalScrollView> 
</ScrollView> 

采取这些步骤,并结合它,可以让你达到你想要的效果。

+0

嗨Avanz,感谢您对您的文章和屏幕截图 - 他们帮助。你所展示的水平滑动显示全屏变化。我试图完成一个前景的视图可以水平滑动,其背后的意见仍然存在。 – gnB

+1

啊..我现在在笔记中看到,我应该继续从已列出的入门代码进行迭代,并尝试结合不同的方法来适应手头的需求。感谢您以不同方式分享代码和想法,我可以继续使用上述方法。 – gnB

+0

我正在尝试你的建议,使用这个项目https://github.com/cthibault/CardDeck,但我没有弄清楚如何运行代码,以便我可以看到Card&Deck UI元素。我只看到Hello World屏幕和一个非工作选项菜单。我没有将该根文件夹下的两个项目导入到ADT中,并尝试运行这两个项目。测试项目似乎只是单元测试,没有任何UI可以查看。我猜你在运行CardDeck主项目时必须能够在你的环境中看到某些东西? – gnB

0

试试这个示例代码 -

public class Main extends Activity { 

    int position=0; 
    LinearLayout full; 
    Intent intent; 

    public Integer[] images= { 
       R.drawable.image1, R.drawable.image2, 
       R.drawable.image3, R.drawable.image4, 
       R.drawable.image5, R.drawable.image6 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main);  
     full = (LinearLayout) findViewById(R.id.full); 
     changeImage(); 
     ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this); 
     full.setOnTouchListener(activitySwipeDetector); 
    } 



    private void changeImage(){ 
     full.setBackgroundResource(images[position]); 
    } 

    public class ActivitySwipeDetector implements View.OnTouchListener { 

     static final String logTag = "ActivitySwipeDetector"; 
     static final int MIN_DISTANCE = 100; 
     private float downX, upX; 
     Activity activity; 

     public ActivitySwipeDetector(Activity activity){ 
      this.activity = activity; 
     } 

     public void onRightToLeftSwipe(){ 
      Log.i(logTag, "RightToLeftSwipe!"); 
      if(position < images.length - 1){ 
       position++; 
       changeImage(); 
      } 
    } 

     public void onLeftToRightSwipe(){ 
      Log.i(logTag, "LeftToRightSwipe!"); 
      if(position > 0){ 
       position--; 
       changeImage(); 
      } 
     } 

     public boolean onTouch(View v, MotionEvent event) { 
      switch(event.getAction()){ 
       case MotionEvent.ACTION_DOWN: { 
        downX = event.getX(); 
        return true; 
       } 
       case MotionEvent.ACTION_UP: { 
        upX = event.getX(); 

        float deltaX = downX - upX; 

        // swipe horizontal? 
        if(Math.abs(deltaX) > MIN_DISTANCE){ 
         // left or right 
         if(deltaX < 0) { this.onLeftToRightSwipe(); return true; } 
         if(deltaX > 0) { this.onRightToLeftSwipe(); return true; } 
        } 
        else { 
          Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE); 
          return false; // We don't consume the event 
        } 


        return true; 
       } 
      } 
      return false; 
     } 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
    } 

    @Override 
    public void onResume() 
    { 
     super.onResume(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
    } 

    @Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
    }  
} 

XML的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/full" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scaleType="centerInside" 
    android:orientation="vertical" /> 
+0

我想这个解决方案,但因变量'mThumbId'错误,因为声明丢失;发生在这条线上:if(position gnB

+0

@gnB将mThumbId替换为图像。请参阅我编辑的帖子。 –