自定义ViewPager [英] Custom ViewPager
本文介绍了自定义ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能创建一个定制的ViewPager? 实例化一个页面的ViewPager它是这样的:
公共对象instantiateItem(查看收集,INT位置){
TextView的电视=新的TextView(CXT);
tv.setText(卓悦PAUG+位置);
tv.setTextColor(Color.WHITE);
tv.setTextSize(30);
((ViewPager)集合).addView(电视,0);
返回电视;
}
有没有办法抬高像这样一类从适配器扩展:
公开查看getView(INT位置,查看convertView,ViewGroup中父){
视图V = convertView;
如果(V == NULL){
LayoutInflater VI =(LayoutInflater)的getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
V = vi.inflate(R.layout.the_layout,NULL);
}
CustomView CV = items.get(位置);
如果(CV!= NULL){
TextView的文字=(TextView的)v.findViewById(R.id.text);
如果(文字!= NULL){
ticker.setText(cv.getText());
}
解决方案
这仅仅是一个例子,但它的工作Quiete酒店很好。
公共对象instantiateItem(查看收集,INT位置){
LayoutInflater充气=(LayoutInflater)cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看布局= inflater.inflate(R.layout.search_result,NULL);
//
TextView的标题=(TextView中)layout.findViewById(R.id.search_result_title);
TextView的身体=(TextView中)layout.findViewById(R.id.search_result_body);
TextView的署名=(TextView中)layout.findViewById(R.id.search_result_byline);
TextView的天=(TextView中)layout.findViewById(R.id.search_result_date_day);
TextView的月份=(TextView中)layout.findViewById(R.id.search_result_date_month);
TextView的一年=(TextView中)layout.findViewById(R.id.search_result_date_year);
//
title.setText(list_title.get(位置)的ToString());
body.setText(list_body.get(位置)的ToString());
day.setText(list_day.get(位置)的ToString());
month.setText(list_month.get(位置)的ToString());
year.setText(list_year.get(位置)的ToString());
byline.setText(list_byline.get(位置)的ToString());
//
((ViewPager)集合).addView(布局,0);
返回布局;
}
How can i create a customized ViewPager ? To instantiate a page in the ViewPager it's something like this :
public Object instantiateItem(View collection, int position) {
TextView tv = new TextView(cxt);
tv.setText("Bonjour PAUG " + position);
tv.setTextColor(Color.WHITE);
tv.setTextSize(30);
((ViewPager) collection).addView(tv,0);
return tv;
}
is there a way to inflate like this in a Class extended from an Adapter :
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.the_layout, null);
}
CustomView cv = items.get(position);
if (cv != null) {
TextView text = (TextView) v.findViewById(R.id.text);
if (text != null) {
ticker.setText(cv.getText());
}
解决方案
This is a mere example, but it's working quiete well.
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.search_result, null);
//
TextView title = (TextView)layout.findViewById(R.id.search_result_title);
TextView body = (TextView)layout.findViewById(R.id.search_result_body);
TextView byline = (TextView)layout.findViewById(R.id.search_result_byline);
TextView day = (TextView)layout.findViewById(R.id.search_result_date_day);
TextView month = (TextView)layout.findViewById(R.id.search_result_date_month);
TextView year = (TextView)layout.findViewById(R.id.search_result_date_year);
//
title.setText(list_title.get(position).toString());
body.setText(list_body.get(position).toString());
day.setText(list_day.get(position).toString());
month.setText(list_month.get(position).toString());
year.setText(list_year.get(position).toString());
byline.setText(list_byline.get(position).toString());
//
((ViewPager) collection).addView(layout,0);
return layout;
}
这篇关于自定义ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文