怎样在collectionView头部添加轮播图,要求能随collectionView滚动

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样在collectionView头部添加轮播图,要求能随collectionView滚动相关的知识,希望对你有一定的参考价值。

参考技术A 你把轮播图那个View
[self.collectionView registerClass:[lunboView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:findCollectionViewSectionHeaderIdentifier];

其中lunboView继承与UICollectionReusableView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

lunboView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:findCollectionViewSectionHeaderIdentifier forIndexPath:indexPath];


你把你的轮播View加到新建的那个View中就行了!

Main3Activity侧滑加轮播图

public class Main3Activity extends AppCompatActivity {
private int num = 10;
private int some = 1;
private String myurl = "http://api.tianapi.com/social/?key=48a7d7193e11bd2dd4a683b6e2f90a4f&num= " + num;

private Handler myhandler = new Handler() {

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int position = vp.getCurrentItem();
position++;
vp.setCurrentItem(position);
sendEmptyMessageDelayed(0, 1000);


}
};
private List<ImageView> li = new ArrayList<>();
private List<String> as;
private List<NewsBean.NewslistBean> list = new ArrayList<>();
private PullToRefreshScrollView psv;
private MyListView mylv;
private DisplayImageOptions options;
private ViewPager vp;
private LinearLayout linpot;
private int type = 1;
private int one = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
vp = findViewById(R.id.vp);
psv = findViewById(R.id.psv);
mylv = findViewById(R.id.mylv);
linpot = findViewById(R.id.linpot);

intpsv();

getdatas();

options = new DisplayImageOptions.Builder()
.cacheInMemory(true)//使用内存缓存
.cacheOnDisk(true)//使用磁盘缓存
.showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片
.showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时
.showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片
.bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 1px=2字节
.imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式
//displayer(new RoundedBitmapDisplayer(100))//设置圆角 30代表半径 自定义
.build();
vp.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
myhandler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_POINTER_UP:
myhandler.sendEmptyMessageDelayed(0, 1000);
break;
case MotionEvent.ACTION_CANCEL:
myhandler.sendEmptyMessageDelayed(0, 2000);

break;


}


return false;
}
});

}

private void setSelectedPoint(int pageIndex) {
for (int i = 0; i < li.size(); i++) {
if (i == pageIndex) {
li.get(i).setImageResource(R.drawable.selected);
} else {
li.get(i).setImageResource(R.drawable.unselected);
}
}


}

private void getdatas() {
MyTask myTask = new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson = new Gson();
NewsBean newsBean = gson.fromJson(jsonstr, NewsBean.class);
List<NewsBean.NewslistBean> data = newsBean.getNewslist();
if (type == 1) {
list.clear();
}
list.addAll(data);
as = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
String picurl = data.get(i).getPicUrl();
as.add(picurl);
}
as = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
String picurl = data.get(i).getPicUrl();
as.add(picurl);
}
if (one == 1) {
for (int i = 0; i < data.size(); i++) {
ImageView iv = new ImageView(Main3Activity.this);
if (i == 0) {
iv.setImageResource(R.drawable.selected);
} else {
iv.setImageResource(R.drawable.unselected);
}
linpot.addView(iv);
li.add(iv);

}


}
vp.setAdapter(new MyVpAdapter());
mylv.setAdapter(new MyAdapter4(Main3Activity.this, data));
myhandler.sendEmptyMessageDelayed(0, 1000);

psv.onRefreshComplete();

}
});
myTask.execute(myurl);


}


private void intpsv() {
psv.setMode(PullToRefreshBase.Mode.BOTH);
psv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
some = 1;
num = 10;
myhandler.removeCallbacksAndMessages(null);
myurl = "http://api.tianapi.com/social/?key=48a7d7193e11bd2dd4a683b6e2f90a4f&num=" + num;
getdatas();
}

@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
some = 2;
num++;

myhandler.removeCallbacksAndMessages(null);

myurl = "http://api.tianapi.com/social/?key=48a7d7193e11bd2dd4a683b6e2f90a4f&num=" + num;
getdatas();
}
});


}


private class MyVpAdapter extends PagerAdapter {

@Override
public int getCount() {
return Integer.MAX_VALUE;
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(Main3Activity.this);
ImageLoader.getInstance().displayImage(as.get(position % as.size()), imageView, options);
return imageView;
}

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



<com.handmark.pulltorefresh.library.PullToRefreshScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/psv">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--轮播图-->
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:background="#f0f"
android:layout_height="200dp"
android:id="@+id/vp"></android.support.v4.view.ViewPager>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linpot"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="bottom"></LinearLayout>

</FrameLayout>


<com.example.myapplication.MyListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mylv"></com.example.myapplication.MyListView>



</LinearLayout>

</com.handmark.pulltorefresh.library.PullToRefreshScrollView>




以上是关于怎样在collectionView头部添加轮播图,要求能随collectionView滚动的主要内容,如果未能解决你的问题,请参考以下文章

Flask实战第50天:cms添加轮播图的模态对话框制作

Swift3.0 轮播图

如何在ios中的自定义UITableViewCell中添加轮播视图

在 Textview 中添加轮播视图

Main3Activity侧滑加轮播图

如何在python中为GET请求添加轮询间隔