这个类在listview中显示项目
public class FragHome extends SherlockFragment {
GlobalData getset;
private ListView listView;
EditText contact_editSearch;
private View root;
public FragHome() {
// TODO Auto-generated constructor stub
}
String[] itemname = { "होम", "देश समाचार", "छत्तीसगढ़", "विदेश समाचार",
"कारोबार", "खेल समाचार", "मनोरंजन", "लाइफ" };
Integer[] imgid = { R.drawable.pic1, R.drawable.pic1, R.drawable.pic1,
R.drawable.pic1, R.drawable.pic1, R.drawable.pic1, R.drawable.pic1,
R.drawable.pic1, };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
root = inflater.inflate(R.layout.frag_home, container, false);
listView = (ListView) root.findViewById(R.id.contact_listview);
// this is just dummy data
getset = new GlobalData();
CustomListAdapter adapter = new CustomListAdapter(getActivity(),
itemname, imgid);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent myint = new Intent(getActivity(), DetailPage.class);
int pos = arg2;
getset.setId(String.valueOf(pos));
startActivity(myint);
}
});
return root;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
}
DetailPage类,我点击项目并打开页面:
public class DetailPage extends Activity {
String[] itemname = { "होम", "देश समाचार", "छत्तीसगढ़", "विदेश समाचार",
"कारोबार", "खेल समाचार", "मनोरंजन", "लाइफ" };
Integer[] imgid = { R.drawable.pic1, R.drawable.pic1, R.drawable.pic1,
R.drawable.pic1, R.drawable.pic1, R.drawable.pic1, R.drawable.pic1,
R.drawable.pic1, };
ViewPager viewPager;
ViewPagerAdapte adapter;
GlobalData getset;
String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.detailpage);
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new ViewPagerAdapte(DetailPage.this, itemname, imgid);
getset = new GlobalData();
id = getset.getId();
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
}
}
这里是view pager class:
public class ViewPagerAdapte extends PagerAdapter {
String[] itemname = { "होम", "देश समाचार", "छत्तीसगढ़", "विदेश समाचार",
"कारोबार", "खेल समाचार", "मनोरंजन", "लाइफ" };
Integer[] imgid = { R.drawable.pic1, R.drawable.pic1, R.drawable.pic1,
R.drawable.pic1, R.drawable.pic1, R.drawable.pic1, R.drawable.pic1,
R.drawable.pic1, };
Context context;
LayoutInflater inflater;
GlobalData getset;
public ViewPagerAdapte(Context context, String[] itemname, Integer[] imgid) {
this.context = context;
this.itemname = itemname;
this.imgid = imgid;
getset = new GlobalData();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemname.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
TextView txtpopulation;
ImageView imgflag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container,
false);
txtpopulation = (TextView) itemView.findViewById(R.id.population);
txtpopulation.setText(itemname[position]);
// Locate the ImageView in viewpager_item.xml
imgflag = (ImageView) itemView.findViewById(R.id.flag);
imgflag.setImageResource(imgid[position]);
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
我可以翻页,但是当我点击项目然后打开第一页项目详细信息而它应该打开项目编号5时遇到的问题。请帮助我或建议我想如果点击5然后详细页面应该显示item5数据和前后翻和后翻应相应的工作请建议我如何实现这一点。
答案 0 :(得分:1)
通过意图传递pos值
intent.putExtra("position", pos);
并通过
id = intent.getIntExtra("position", 0);
只需在DetailPage类的onCreate方法中添加此行,就在setAdapter调用的正下方
viewpager.setCurrentItem(id)