gpt4 book ai didi

android - 如何在android中制作圆形 View 寻呼机?

转载 作者:行者123 更新时间:2023-11-29 02:39:48 26 4
gpt4 key购买 nike

enter image description here如何从viewpager的第0个index跳转到view pager的最后一个index?那就是如何让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);
}
}

最佳答案

以 View A、B、C 为例,添加如下 View :C、A、B、C、A。然后将当前页面设置为第 1 页(即 View 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”。它并不完美,无法处理试图同时滚动多个页面的用户。但这是一个可行的解决方案。

关于android - 如何在android中制作圆形 View 寻呼机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45011474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com