【发布时间】:2016-12-04 10:15:42
【问题描述】:
我有一个包含产品详细信息的 ListView。在列表视图的项目单击上,我打开具有特定产品详细信息的新活动。我想将活动转换为 ViewPager,以便我可以滑动以在同一片段中加载下一条和上一条记录。所有记录的片段结构都相同。我不知道我应该从哪里开始。你能给我概述一下如何实现这一点。这是我的模型类。
Product.java
public class Product implements Serializable{
public int id;
public String Name;
public String description;
public String longDescription;
public String amount;
public String image;
}
这是我的 FragmentPagerAdapter 类
ProductPagerAdapter.java
public class ProductPagerAdapter extends FragmentPagerAdapter {
private Context context;
private ArrayList<Product> list;
public ProductPagerAdapter(FragmentManager fm, Context context, ArrayList<Product> list) {
super(fm);
this.context = context;
this.list = list;
}
@Override
public Fragment getItem(int position) {
return ProductFragment.newInstance(position);
}
@Override
public int getCount() {
return list.size();
}
}
这是我的片段
ProductFragment.java
public class ProductFragment extends Fragment {
public ProductFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_product, container, false);
//findViewById...
return v;
}
public static Fragment newInstance(int id) {
Bundle args = new Bundle();
args.putInt("Id", id);
ProductFragment fragment = new ProductFragment();
fragment.setArguments(args);
return fragment;
}
}
现在在列表项上单击我正在打开新活动。我正在向它发送 Product 对象。
lv_itemRateList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), DetailsActivity.class);
Product r = new Product();
r = rateListArrayList.get(i);
intent.putExtra("product",r);
startActivity(intent);
}
});
我的 DetailsActivity 包含我的 viewpager。现在有人可以告诉我该怎么做吗?
【问题讨论】:
标签: android listview android-fragments android-viewpager