使用SwipeView进行布局而不是图像

时间:2012-01-30 09:46:44

标签: android layout

我正在使用jason fry的SwipeView,但是他将它用于图像视图,我正在努力用布局替换它。

目前,如果我用TextView替换ImageView,它将如何工作,但我如何用布局替换Imageview 感谢任何帮助

package com.example;

import android.graphics.Typeface;
import android.text.Layout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.R;
import uk.co.jasonfry.android.tools.ui.PageControl;
import uk.co.jasonfry.android.tools.ui.SwipeView;
import uk.co.jasonfry.android.tools.ui.SwipeView.OnPageChangedListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MyActivity extends Activity
{
SwipeView mSwipeView;

LinearLayout ll;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ll = new LinearLayout(this);
    ll = loadLayout();

    PageControl mPageControl = (PageControl) findViewById(R.id.page_control);
    mSwipeView = (SwipeView) findViewById(R.id.swipe_view);

    //loadImages();

    for(int i=0; i<4;i++)
    {
        mSwipeView.addView(new FrameLayout(this));
    }

    TextView i0 = new TextView(this);
    TextView i1 = new TextView(this);
    i0.setText("page 1");
    i1.setText("page 2");

    ((FrameLayout) mSwipeView.getChildContainer().getChildAt(0)).addView(ll);
    ((FrameLayout) mSwipeView.getChildContainer().getChildAt(1)).addView(ll);

    SwipeImageLoader mSwipeImageLoader = new SwipeImageLoader();

    mSwipeView.setOnPageChangedListener(mSwipeImageLoader);
    mSwipeView.setPageControl(mPageControl);
}

private class SwipeImageLoader implements OnPageChangedListener
{

    public void onPageChanged(int oldPage, int newPage)
    {
        if(newPage>oldPage)//going forwards
        {
            if(newPage != (mSwipeView.getPageCount()-1))//if at the end, don't load one page after the end
            {
                TextView v = new TextView(MyActivity.this);
                v.setText("page :"+(newPage+1));
                ((FrameLayout) mSwipeView.getChildContainer().getChildAt(newPage+1)).addView(ll);
            }
            if(oldPage!=0)//if at the beginning, don't destroy one before the beginning
            {
                ((FrameLayout) mSwipeView.getChildContainer().getChildAt(oldPage-1)).removeAllViews();
            }

        }
        else //going backwards
        {
            if(newPage!=0)//if at the beginning, don't load one before the beginning
            {

                TextView v = new TextView(MyActivity.this);
                v.setText("page :"+(newPage+1));
                ((FrameLayout) mSwipeView.getChildContainer().getChildAt(newPage-1)).addView(ll);
            }
            if(oldPage != (mSwipeView.getPageCount()-1))//if at the end, don't destroy one page after the end
            {
                ((FrameLayout) mSwipeView.getChildContainer().getChildAt(oldPage+1)).removeAllViews();
            }
        }

    }

}

private LinearLayout loadLayout()
{
    //logo
    ImageView logo = new ImageView(this);
    logo.setImageResource(R.drawable.image001);
    logo.setLayoutParams(new     LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

    // espace
    TextView espace = new TextView(this);
    espace.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    espace.setText(" ");

    // wrap prest
    LinearLayout wprest = new LinearLayout(this);

    //Prestation
    TextView txt_pres = new TextView(this);
    txt_pres.setText("   Prestation n° ");
    txt_pres.setTextColor(R.color.black);
    // plaid
    TextView plaid = new TextView(this);
    plaid.setTextColor(R.color.black);
    plaid.setTypeface(null, Typeface.BOLD);
    plaid.setText("4558");
    // -
    TextView tiret = new TextView(this);
    tiret.setTextColor(R.color.black);
    tiret.setTypeface(null, Typeface.BOLD);
    tiret.setText(" - ");
    // plaid
    TextView platyp = new TextView(this);
    platyp.setTextColor(R.color.black);
    platyp.setTypeface(null, Typeface.BOLD);
    platyp.setText("ECHANGE");

    wprest.addView(txt_pres);
    wprest.addView(plaid);
    wprest.addView(tiret);
    wprest.addView(platyp);



    LinearLayout ll = new LinearLayout(this);

    ll.setBackgroundResource(R.color.white);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setLayoutParams(new      LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,     LinearLayout.LayoutParams.FILL_PARENT));
    ll.addView(logo);
    ll.addView(espace);
    ll.addView(wprest);

    return ll;
}
}

2 个答案:

答案 0 :(得分:2)

为什么不尝试其他ViewPager库,例如:

  • android-viewflow(https://github.com/pakerfeldt/android-viewflow)
  • Android-ViewPagerIndicator(https://github.com/JakeWharton/Android-ViewPagerIndicator)

因为使用它,您可以轻松自定义子布局。

我发现它是最受欢迎的ViewPager库。有关更多详细信息,您可以在Market上查看我的“Android UI Patterns”应用:https://market.android.com/details?id=com.groidify.uipatterns。开发人员有许多有用的样本。

答案 1 :(得分:0)

我在这个地方使用这个库不仅仅是图像。我已经将它用于复杂的布局,所以它确实有用。

我已经添加了一些新的布局对象,但没有记录它们或创建了如何使用它们的示例(我是一个忙人!:p)其中一个叫做PageView,它使用一个适配器,就像一个ListView,所以如果你知道如何使用ListView,你应该能够使用它。关键是使用.setAdapter(BaseAdapter适配器)方法。

确保您使用的是github中的最新版本。这是一个直接链接到github上的PageView类代码的链接:https://github.com/fry15/uk.co.jasonfry.android.tools/blob/master/src/uk/co/jasonfry/android/tools/widget/PageView.java

相关问题