【发布时间】:2015-08-15 15:50:11
【问题描述】:
我正在尝试使用 ViewFlipper 自动更改片段内的图像。我的活动代码是:
package com.example.scientia;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class HomeActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {
private NavigationDrawerFragment mNavigationDrawerFragment; //manages behaviors of navigation drawer
private CharSequence mTitle; //stores title of last screen
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
fragmentManager.beginTransaction().replace(R.id.fragment1, PlaceholderFragment.newInstance(position + 1))
.commit();
if(position == 0)
{
Fragment f1 = new HomeFragment();
ft.replace(R.id.fragment1, f1);
ft.commit();
}
if(position == 1)
{
Fragment f1 = new IqQuiz();
ft.replace(R.id.fragment1, f1);
ft.commit();
}
if(position == 2)
{
Fragment f1 = new LogicalQuiz();
ft.replace(R.id.fragment1, f1);
ft.commit();
}
if(position == 3)
{
Fragment f1 = new AptitudeQuiz();
ft.replace(R.id.fragment1, f1);
ft.commit();
}
if(position == 4)
{
Fragment f1 = new VerbalQuiz();
ft.replace(R.id.fragment1, f1);
ft.commit();
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
case 4:
mTitle = getString(R.string.title_section4);
break;
case 5:
mTitle = getString(R.string.title_section5);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
getMenuInflater().inflate(R.menu.home, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) { //handles action bar item clicks
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment { //placeholder containes a simple view
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment(); //returns new fragment for the section number
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
ViewFlipper viewflipper = (ViewFlipper) rootView.findViewById(R.id.view_flipper);
viewflipper.setAutoStart(true);
viewflipper.setFlipInterval(300);
viewflipper.startFlipping();
Toast.makeText(getActivity(), "Flipping", 300);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((HomeActivity) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
相关的片段XML文件编码为:
<RelativeLayout 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"
tools:context="com.example.scientia.HomeActivity$PlaceholderFragment" >
<ViewFlipper
android:id="@+id/view_flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/school" />
<TextView
style="@style/ImageTitle"
android:text="School" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/retina_wood" />
<TextView
style="@style/ImageTitle"
android:text="Retina Woods" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/skulls" />
<TextView
style="@style/ImageTitle"
android:text="Skulls" />
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
没有编译错误或应用程序崩溃,但图像不会自动翻转。请帮忙!
【问题讨论】:
-
onNavigationDrawerItemSelected 是否在创建后立即替换 PlaceholderFragment 片段 (ft.replace(R.id.fragment1, f1);)?
标签: android animation android-fragments viewflipper