ViewPage流程:
贴代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater from = getLayoutInflater().from(this);
View inflate1 = from.inflate(R.layout.layout1, null);
View inflate2 = from.inflate(R.layout.layout2, null);
View inflate3 = from.inflate(R.layout.layout3, null);
ArrayList<View> views = new ArrayList<>();
views.add(inflate1);
views.add(inflate2);
views.add(inflate3);
ViewPager viewPager = findViewById(R.id.vp_pager);
Adapter adapter = new Adapter(views);
viewPager.setAdapter(adapter);
}
}
package com.example.myviewpager;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import java.util.List;
public class Adapter extends PagerAdapter {
private List<View> list;
public Adapter(List<View> list) {
this.list = list;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(list.get(position), null);
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(list.get(position));
}
}
|