轮播图
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private ArrayList<ImageView> imageViews = new ArrayList<>();
private int[] imageResIds = {
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d,
R.drawable.e
};
private String[] descs = {
"巩俐不低俗,我就不能低俗",
"扑树又回来啦!再唱经典老歌引万人大合唱",
"揭秘北京电影如何升级",
"乐视网TV版大派送",
"热血屌丝的反杀"
};
private LinearLayout ll_dot;
private TextView vp_tv;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
int currentItem = vp.getCurrentItem();
vp.setCurrentItem(currentItem+1);
sendEmptyMessageDelayed(1,3000);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
ll_dot = findViewById(R.id.ll_dot);
vp_tv = findViewById(R.id.vp_tv);
vp = findViewById(R.id.vp);
for (int i=0;i<imageResIds.length;i++){
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource(imageResIds[i]);
imageViews.add(imageView);
createDot(i);
}
vp.setAdapter(new MyPagerAdapter());
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
int currentItem = vp.getCurrentItem();
changeTextanddot(currentItem%imageResIds.length);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
vp.setCurrentItem(Integer.MAX_VALUE/2-3);
handler.sendEmptyMessageDelayed(1,3000);
ViewPagerTouchEvent();
}
public void ViewPagerTouchEvent() {
vp.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
//当时MotionEvent.ACTION_DOWN和ACTION_MOVE,就移除handler发送的message.removeMessages.333
handler.removeMessages(1);
break;
case MotionEvent.ACTION_MOVE:
handler.removeMessages(1);
break;
//当用户手松开时ACTION_UP,就继续使用sendEmptyMessageDelayed发送handler的消息.333
case MotionEvent.ACTION_UP:
handler.sendEmptyMessageDelayed(1, 3000);
default:
break;
}
return false;
}
});
}
class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = imageViews.get(position % imageResIds.length);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
public void changeTextanddot(int position) {
vp_tv.setText(descs[position]);
for (int i=0;i<imageResIds.length;i++){
View childAt = ll_dot.getChildAt(i);
childAt.setBackgroundResource(i==position?R.drawable.dot_focus:R.drawable.dot_normal);
}
}
private void createDot(int i) {
View view = new View(this);
view.setBackgroundResource(R.drawable.dot_normal);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(8,8);
layoutParams.leftMargin= 8;
view.setLayoutParams(layoutParams);
ll_dot.addView(view);
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeMessages(1);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="wrap_content"
android:layout_height="150dp" />
<!-- 定义lineatLayout时,background="#88000000",gravity="center_horizontal" padding="8dp"
android:layout_alignBottom="@+id/viewpager_vp" 小点用一个LinearLayout来包裹.-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#88000000"
android:orientation="vertical"
android:padding="8dp"
android:gravity="center_horizontal"
android:layout_alignBottom="@+id/vp">
<TextView
android:id="@+id/vp_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:text="我是耿裴宣"/>
<LinearLayout
android:id="@+id/ll_dot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
public static final String IMAGE_URL = "https://www.zhaoapi.cn/quarter/getAd";
public static final String NEWS_URL = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=10&page=";
private int page = 1;
private boolean isLoadMore = false;
private ViewPager vpImg;
private XListView xlvNews;
private VpAdapter adapter;
private List<Images.DataBean> ilist;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
int currentItem = vpImg.getCurrentItem();
if (currentItem == ilist.size()-1){
currentItem = 0;
}else {
currentItem ++;
}
vpImg.setCurrentItem(currentItem);
handler.sendEmptyMessageDelayed(0,2000);
break;
}
}
};
private List<News.ResultBean> xlist;
private NewsAdapter newsadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
vpImg = findViewById(R.id.vp_img);
xlvNews = findViewById(R.id.xlv_news);
//设置列表第一个条目用banner轮播图
getVpImg();
xlist = new ArrayList<>();
//创建适配器
newsadapter = new NewsAdapter(xlist,MainActivity.this);
//设置适配器
xlvNews.setAdapter(newsadapter);
getData();
//设置允许上拉下拉
xlvNews.setPullLoadEnable(true);
xlvNews.setPullRefreshEnable(true);
//xlistView监听
xlvNews.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
if (page >1){
page --;
}else {
Toast.makeText(MainActivity.this,"已经是最新的数据了",Toast.LENGTH_SHORT).show();
}
isLoadMore = false;
getData();
handler.postDelayed(new Runnable() {
@Override
public void run() {
xlvNews.stopRefresh();
}
},1000);
long l = System.currentTimeMillis();
Date date = new Date(l);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
xlvNews.setRefreshTime(format.format(date));
}
@Override
public void onLoadMore() {
page ++;
isLoadMore = true;
getData();
News news = new News();
List<News.ResultBean> result = news.getResult();
if (result == null){
Toast.makeText(MainActivity.this,"没有更多的数据了",Toast.LENGTH_SHORT).show();
}
handler.postDelayed(new Runnable() {
@Override
public void run() {
xlvNews.stopLoadMore();
}
},1000);
}
});
xlvNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this,ShowActivity.class);
intent.putExtra("name",xlist.get(position-1).getName());
startActivity(intent);
}
});
}
private void getData() {
new AsyncTask<String, Integer, String>() {
@Override
protected String doInBackground(String... strings) {
return HttpUtils.getHttpUrlConnection(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
News news = gson.fromJson(s, News.class);
if (news != null){
List<News.ResultBean> result = news.getResult();
if (result != null){
if (!isLoadMore){
xlist.clear();
}
xlist.addAll(result);
newsadapter.notifyDataSetChanged();
}
}
}
}.execute(NEWS_URL + page);
}
private void getVpImg() {
//创建list集合
ilist = new ArrayList<>();
//创建适配器
adapter = new VpAdapter(ilist,MainActivity.this);
//设置适配器
vpImg.setAdapter(adapter);
//延时发送
handler.sendEmptyMessageDelayed(0,2000);
new AsyncTask<String, Integer, String>() {
@Override
protected String doInBackground(String... strings) {
return HttpUtils.getHttpUrlConnection(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Images images = gson.fromJson(s, Images.class);
if (images != null){
List<Images.DataBean> data = images.getData();
if (data != null){
ilist.clear();
ilist.addAll(data);
adapter.notifyDataSetChanged();
}
}
}
}.execute(IMAGE_URL);
}
}
public class VpAdapter extends PagerAdapter {
private List<Images.DataBean> ilist;
private Context context;
public VpAdapter(List<Images.DataBean> ilist, Context context) {
this.ilist = ilist;
this.context = context;
}
@Override
public int getCount() {
return ilist.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView img = new ImageView(context);
ImageLoader.getInstance().displayImage(ilist.get(position).getIcon(),img);
img.setScaleType(ImageView.ScaleType.FIT_XY);
container.addView(img);
return img;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}