我在FragmentActivity中使用ViewPager。现在,每当我的Activity激活时,我都会将position
值传递到我片段的newinstance
方法中。从那里我的片段现在将选择drawable的资源ID并将其设置为其背景。
我的片段活动
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.ViewGroup;
import android.view.Window;
public class HowToActivity extends FragmentActivity {
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.slide_out_bottom, R.anim.slide_in_top);
}
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // remove title
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.viewPager_howto);
setContentView(mViewPager);
getWindow().setLayout(LayoutParams.MATCH_PARENT /* width */ , LayoutParams.MATCH_PARENT /* height */);
FragmentManager fm = this.getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fm){
@Override
public void setPrimaryItem(ViewGroup container, int position,
Object object) {
super.setPrimaryItem(container, position, object);
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
return HowToFragment.newInstance(position);
}
});
}
}
我的片段
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HowToFragment extends Fragment {
private static int image_pos;
private int[] image_id = {R.drawable.image_1, R.drawable.image_2, R.drawable.image_3};
public static Fragment newInstance(int pos){
image_pos = pos;
return new HowToFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_how_to_image, container, false);
v.setBackgroundResource(image_id[image_pos]);
return v;
}
}
启动时始终会显示image_2.png
而不是image_1
。然后,您需要一直滑动到image_3
或2
位置,以便将image_1
固定在0
位置。从0
和1
来回切换只会在每个片段中显示image_2
。
任何想法我做错了什么?
答案 0 :(得分:2)
你无法使该变量成为静态
private static int image_pos;
所有片段都使用相同的片段,因此在创建时总是会看到第二个片段。
FG[0] newInstance -> image_pos=0;
FG[1] newInstance -> image_pos=1;
当碎片要创建视图时:
FG[0] onCreateView -> value of image_pos=1;
FG[1] onCreateView -> value of image_pos=1;
改为:
private int image_pos;
private int[] image_id = {R.drawable.image_1, R.drawable.image_2, R.drawable.image_3};
public static Fragment newInstance(int pos){
HowToFragment fg = new HowToFragment();
fg.image_pos = pos;
return fg;
}
您也可以使用适配器中的图像定义数组,并在片段create方法上传递Id。
答案 1 :(得分:1)
原因是适配器不仅会创建可见的片段,还会预加载右侧和左侧片段。不要使位置字段静止。将它用作数据成员并在返回片段之前设置该pos值。