我需要将来自不同XML的一些布局组显示为ViewPager的项目。我创建了2种不同的布局。我想使用card.xml 1次,并使用其他多个。当我点击card.xml的视图时,我将编写一个代码来添加一个新的credit_cards.xml视图。在这里,我添加前两个布局,我可以在viewpager上看到它们。我正在尝试手动添加新视图,但我遇到了错误。这是我第一次使用viewpager,所以我不确定问题出在哪里。
这是我的活动。
public class MainActivity extends AppCompatActivity {
final Context mContext = this;
public List<View> mList = new ArrayList<>();
ViewPager pager;
ViewPagerAdapter adapter;
LinearLayout first, second;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = LayoutInflater.from(getApplication()).inflate(R.layout.credit_cards, null);
first = (LinearLayout) view.findViewById(R.id.addNewCard);
second = (LinearLayout) view.findViewById(R.id.creditCard);
pager = (ViewPager) findViewById(R.id.viewpager);
mList.add(first);
adapter = new ViewPagerAdapter(mContext, mList);
adapter.addView(second,0);
pager.setAdapter(adapter);
pager.setPageTransformer(true, new ZoomOutPageTransformer());
}
}
这是ViewPager。
<?xml version="1.0" encoding="utf-8"?>
<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.seray.myapplication.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:layout_marginBottom="1sp">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"/>
</RelativeLayout>
</RelativeLayout>
适配器。
public class ViewPagerAdapter extends PagerAdapter {
public List<View> mList;
private final Context mContext;
LinearLayout first, second;
View mPrimaryItem;
public ViewPagerAdapter(Context mContext, List<View> mList){
this.mContext = mContext;
this.mList = mList;
}
@Override
public int getItemPosition(Object object) {
if (mList.contains((View) object)) {
return mList.indexOf((View) object);
} else {
return POSITION_NONE;
}
}
@Override
public void setPrimaryItem(ViewGroup container, int position,
Object object) {
super.setPrimaryItem(container, position, object);
mPrimaryItem = (View) object;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public int getCount() {
return mList.size();
}
public void addView(View view, int index) {
mList.add(index, view);
notifyDataSetChanged();
}
public void removeView(int index) {
mList.remove(index);
notifyDataSetChanged();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View v = mList.get(position);
((ViewPager)container).addView(v);
return v;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}