如何在android中制作圆形视图寻呼机?

时间:2017-07-10 12:00:46

标签: android

enter image description here 如何从viewpager的第0个索引跳转到view pager的最后一个索引?那是如何使视图寻呼机循环?如果用户向左滑动,我想跳转到最后一个索引,如果用户向右滑动,我想跳到最后一页,它应跳转到第一页。

Java File 
package com.example.hakimi.firebasemessaging;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.eftimoff.viewpagertransformers.CubeInTransformer;
import com.eftimoff.viewpagertransformers.DefaultTransformer;
import com.eftimoff.viewpagertransformers.DrawFromBackTransformer;
import com.eftimoff.viewpagertransformers.RotateUpTransformer;
import com.eftimoff.viewpagertransformers.StackTransformer;
import com.eftimoff.viewpagertransformers.TabletTransformer;

import java.util.ArrayList;

    public class NewsCardView extends AppCompatActivity {
        private CustomPagerAdapter mCustomPagerAdapter;
        private ViewPager mViewPager;
        private ArrayList<NewsModel> arrayList = MainActivity.mArrayList;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.news_view_activity);



            Bundle bundle = getIntent().getExtras();
            int id = bundle.getInt("id");


            mCustomPagerAdapter = new CustomPagerAdapter(this,arrayList,mViewPager);





            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mCustomPagerAdapter);


            mViewPager.setPageTransformer(true, new TabletTransformer());

            mViewPager.setCurrentItem(id);


            mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    if(position==mViewPager.getAdapter().getCount()-1){
                     /*   Intent reg = new
                                Intent(Illustrations.this,Register_Page.class);*/
                     //   startActivity(reg);
                    }

                }

                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // TODO Auto-generated method stub

                }


            });

        }


        }


Xml file

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/relativeLayout">


        <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            >
        </android.support.v4.view.ViewPager>

    </RelativeLayout>




Adapter

    class CustomPagerAdapter extends PagerAdapter {
            Context mContext;
            LayoutInflater mLayoutInflater;


        private int[] pageIDsArray;
        private int count;


        ArrayList<NewsModel> news;


    public CustomPagerAdapter(Context context,ArrayList<NewsModel> news,final ViewPager pager) {
        this.news=news;



            mContext = context;
            mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
            View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

            ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
          //  imageView.setImageResource(mResources[position]);
        TextView newstitle,newsdes;

        TextView gotosrc;

        newstitle=(TextView) itemView.findViewById(R.id.pagertitle);
        newsdes=(TextView) itemView.findViewById(R.id.pagerdes);

        newsdes.setMovementMethod(new ScrollingMovementMethod());

        gotosrc=(TextView) itemView.findViewById(R.id.tvsrc);


        gotosrc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext,WebViewActivity.class);
                mContext.startActivity(intent);

            }
        });

        newstitle.setText(news.get(position).getNewsTitle());
        newsdes.setText(news.get(position).getNewsData());

        String imgurll=news.get(position).getNewsImgUrl();

        Picasso.with(mContext)
                .load(imgurll)
                .resize(300, 250)
                .centerCrop()
                .into(imageView);

            container.addView(itemView);

            return itemView;
            }

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

2 个答案:

答案 0 :(得分:0)

以具有视图A,B,C的示例为例,添加如下视图:C,A,B,C,A。然后将当前页面设置为页面1(即视图A)。

您将需要使用类似下面的代码检测ViewPager在第0页或第4页上停止滚动的情况。如果是,请切换到包含相同视觉内容但没有动画的相反页面,以便立即进行更改。

以下是检测何时在页面上停止滚动并切换到“相反”页面的代码:

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_IDLE) {
                if( newPage != lastPage ) {
                    lastPage = newPage;
                    if( newPage == 4 )
                        viewPager.setCurrentItem( 1, false );
                    else if( newPage == 0 )
                        viewPager.setCurrentItem( 3, false );
                 }
            }
        }

        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        public void onPageSelected(int position) {
            newPage = position;
        }
    });

例如,如果用户停止在第0页上滚动,请切换到第3页,两者都看起来像“ C”。它不是完美的,不能处理试图同时滚动多个页面的用户。但这是一个可行的解决方案。

答案 1 :(得分:0)

请尝试这个。当然,它会按预期运行。

viewPager?.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
      var currentPage : Int = 0
      var mPreviousPosition : Int = 0
      var mIsEndOfCycle = false
      override fun onPageScrollStateChanged(state: Int) {
        val pageCount = viewPager?.adapter?.count
        if (state == ViewPager.SCROLL_STATE_IDLE) {
          if (mPreviousPosition == currentPage && !mIsEndOfCycle) {
            if (currentPage == 0) {
              pageCount?.minus(1)?.let { viewPager?.setCurrentItem(it, false) };
            } else {
              viewPager?.setCurrentItem(0, false);
            }
            mIsEndOfCycle = true;
          } else {
            mIsEndOfCycle = false;
          }
          mPreviousPosition = currentPage;
        }
      }

      override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

      override fun onPageSelected(position: Int) {
        currentPage = position
      }

    })