2015-04-22 205 views
1

我有一個活動,在其上顯示一個問題。 此屏幕上有上一個,下一個按鈕。在活動上滑動左/右效果

每次用戶按下上一個/下一個 - 顯示不同的問題。 現在我想要在按下上一個/下一個按鈕(活動保持不變)時在該視圖上滑動(右側左側)。

我可以做滑動,像這樣跨越活動轉變(從一個活動視圖之間)影響:

... 
startActivity(intent); 
overridePendingTransition(R.anim.anim1, R.anim.anim2); 
... 

但我怎麼給一個單一的活動影響?

+0

是每個問題一個相對佈局還是一個列表觀點還是它是什麼? – Droidekas

+0

viewpager中的片段 –

回答

1

肯定可以使用ViewPager

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

做到這一點:這是您需要一個漂亮整潔的例子的Link

我會解釋給你

這裏是主活動佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:orientation="vertical" 
tools:context="com.example.androidviewpagerapp.MainActivity" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:autoLink="web" 
    android:text="http://android-er.blogspot.com/" 
    android:textStyle="bold" /> 

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

</LinearLayout> 

您MainActivity應該是這樣的

import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.app.Activity; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.os.Bundle; 

public class MainActivity extends Activity { 

ViewPager viewPager; 
MyPagerAdapter myPagerAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    viewPager = (ViewPager)findViewById(R.id.myviewpager); 
    myPagerAdapter = new MyPagerAdapter(); 
    viewPager.setAdapter(myPagerAdapter); 

} 

private class MyPagerAdapter extends PagerAdapter{ 

    int NumberOfPages = 5; 

    int[] res = { 
    android.R.drawable.ic_dialog_alert, 
    android.R.drawable.ic_menu_camera, 
    android.R.drawable.ic_menu_compass, 
    android.R.drawable.ic_menu_directions, 
    android.R.drawable.ic_menu_gallery}; 
    int[] backgroundcolor = { 
    0xFF101010, 
    0xFF202020, 
    0xFF303030, 
    0xFF404040, 
    0xFF505050}; 

    @Override 
    public int getCount() { 
    return NumberOfPages; 
    } 

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

    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 


    TextView textView = new TextView(MainActivity.this); 
    textView.setTextColor(Color.WHITE); 
    textView.setTextSize(30); 
    textView.setTypeface(Typeface.DEFAULT_BOLD); 
    textView.setText(String.valueOf(position)); 

    ImageView imageView = new ImageView(MainActivity.this); 
    imageView.setImageResource(res[position]); 
    LayoutParams imageParams = new LayoutParams(
    LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); 
    imageView.setLayoutParams(imageParams); 

    LinearLayout layout = new LinearLayout(MainActivity.this); 
    layout.setOrientation(LinearLayout.VERTICAL); 
    LayoutParams layoutParams = new LayoutParams(
    LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); 
    layout.setBackgroundColor(backgroundcolor[position]); 
    layout.setLayoutParams(layoutParams); 
    layout.addView(textView); 
    layout.addView(imageView); 

    final int page = position; 
    layout.setOnClickListener(new OnClickListener(){ 

@Override 
public void onClick(View v) { 
Toast.makeText(MainActivity.this, 
    "Page " + page + " clicked", 
    Toast.LENGTH_LONG).show(); 
}}); 

    container.addView(layout); 
    return layout; 
    } 

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

} 

}