Android:在ViewPager上滑动单个片段即可加载下一个或上一个记录

阿什什·库代尔

我有一个包含产品详细信息的ListView。在列表视图的项目单击上,我打开具有特定产品详细信息的新活动。我想将活动转换为ViewPager,以便可以滑动以在同一片段中加载下一个和上一个记录。所有记录的片段结构都相同。我不知道我应该从哪里开始。您能否概述一下如何实现这一目标。这是我的模型课。

产品.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;
    }
}

现在在列表项上,单击我正在打开新活动。我正在向其发送产品对象。

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。现在有人可以告诉我该怎么做吗?

Burak Cakir尚未在Medium上活跃。

1-)需要一个用于ViewPager的自定义适配器。

我假设所有页面都将具有相同的内容,所以所有页面都具有相同的布局,因此ProductPagerAdapter扩展了PagerAdapter

public class ProductPagerAdapter extends PagerAdapter
{
     //variables
     private Context context;
     private ArrayList<Product> list;
     private Product product

     //views
    TextView  txtName ;

    public ProductPagerAdapter(Context context, List<Product> list) 
    {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount()
    {
        return  list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object)
    {
        return view == ( object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) 
    {
        product = list.get(position);

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.fragment_product, container,false);

        txtName = (TextView) itemView.findViewById(R.id.txtName);
        txtName.setText(product.Name)

        ((ViewPager) container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) 
    {
        ((ViewPager) container).removeView((LinearLayout) object); 
    }
}

2-)适配器已准备就绪。现在,让我们准备显示细节的活动。首先初始化ViewPager和ProductPagerAdapter。然后显示通过单击上一个活动选择的项目的详细信息。

public class DetailsActivity extends Activity
{
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            ...

            //getting product object
            Intent intent = getIntent();
            Product product = (Product) getIntent().getSerializableExtra("product");

            //get selected item id
            int selectedItemID = 0;
            for(int i = 0 ; i < list.size() ; i++) 
            {
               if(list.get(i).id == product.id)
               {
                selectedItemID = i;
                break;
               }
            }

            //Init ViewPager and adapter
            ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

            ProductPagerAdapter detailAdapter = new ProductPagerAdapter(DetailActivity.this, list);

            // Binds the Adapter to the ViewPager
            viewPager.setAdapter(detailAdapter);
            viewPager.setCurrentItem(selectedItemID); // set selection
            viewPager.setPageTransformer(true, new ForegroundToBackgroundTransformer()); //custom transformer

            ...
        }
}

3-)到目前为止,我们已经完成了必须要做的事情。因此它将像这样工作。但是我想指出的是,有一些事情可以使ViewPager变得更好。

性能:使用Parcelable而不是SerializableParcelable需要花费更多的时间来实现,但是它的执行速度将提高10倍,并且占用的资源更少。请检查此答案

动画:您可能需要动画来转换ViewPager。我建议您使用ViewPagerTransforms

指示器:如果要使用分页指示器,建议使用ViewPagerIndicator

有关ViewPager的更多信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

单击按钮滑动到下一个片段

下一个和上一个按钮将在OWL轮播上滑动2个项目

在按钮上滑动下一个div

ViewPager防止加载下一个视图

在单个页面中导航的上一个和下一个按钮

上一个/下一个点击

分页下一个上一个

如何在Bootstrap轮播的下一个/上一个单击上加载JSP页面

Mysql:获取重复的记录中的下一个和上一个

Laravel上一个和下一个记录,返回对象

学说-获取下一个和上一个记录

根据SQL中的下一个和上一个记录排序

下一个/上一个按钮上的图像描述

在上一个和下一个单击上添加<LI>的类

滑动到下一个片段并返回后,该片段不显示任何内容

Android滑动视图并在屏幕上显示下一个视图

单击上一个抽屉项目的特定片段时如何打开下一个抽屉

返回Android中onBackPressed上的上一个片段

获取同一查询中的下一个和上一个记录

在具有上一个和下一个按钮的文本框中显示单个日期

如何在MySQL中获取下一个/上一个双重记录?

在页面中获取ssrs报告中的上一个,当前和下一个记录

Rails获取上一个和下一个活动记录对象的最佳方法

根据SQL中的下一个和上一个记录进行复杂的排序

LINQ,获取表中的下一个和上一个记录(我很困惑)

使用C#中的“下一个/上一个”按钮显示XML记录

选择上一个和下一个记录时,Symfony DQL语法错误

如何使用“上一个/下一个”按钮滑动“猫头鹰轮播”中的所有可见项目,而不是一个

如何直接在 ViewPager 中更新下一个片段的数据?