-
首先看一下效果吧
20170123112552092.png
自android5.0以来,RecyclerView的已经是android开发中不可缺少的控件了,他的方便不用多说了,那么使用RecyclerView当然不能像listview一样直接添加头布局了,那么RecyclerView该怎么一键添加头布局,并实现轮播图效果呢?
-
首先要引入几个类库
compile 'com.android.support:recyclerview-v7:25.1.0' compile 'com.bartoszlipinski:recyclerviewheader2:2.0.1' compile ('cn.trinea.android.view.autoscrollviewpager:android-auto-scroll-view-pager:1.1.2') { exclude module: 'support-v4' }
recyclerview-v7:25.1.0当然就是引入recyclerview了;
recyclerviewheader2:2.0.1是为了添加头布局;
android-auto-scroll-view-pager:1.1.2为轮播图。
- 现在来看他们三个该怎么布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.hnshangyu.autoscrollandrecyclerviewheader.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader
android:id="@+id/recyclerView_header"
android:layout_width="match_parent"
android:layout_height="180dp">
<cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
android:id="@+id/autoScrollViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_centerHorizontal="true"-->
<!--android:layout_alignParentBottom="true"-->
<!--android:text="header" />-->
</com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader>
</RelativeLayout>
RecyclerView与RecyclerViewHeader在同一节点下,AutoScrollViewPager在RecyclerViewHeader节点下,RecyclerView使用代码加入头布局!
- 代码设置(MainActivity)
package cn.hnshangyu.autoscrollandrecyclerviewheader;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader;
import java.util.concurrent.CopyOnWriteArrayList;
import butterknife.Bind;
import butterknife.ButterKnife;
import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;
public class MainActivity extends AppCompatActivity {
@Bind(R.id.recyclerView)
RecyclerView recyclerView;
@Bind(R.id.recyclerView_header)
RecyclerViewHeader recyclerViewHeader;
@Bind(R.id.autoScrollViewPager)
AutoScrollViewPager autoScrollViewPager;
private CopyOnWriteArrayList<String> mData = new CopyOnWriteArrayList<>();
private LinearLayoutManager manager;
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initData();
recyclerViewHeader.attachTo(recyclerView);
initListener();
}
private void initData() {
for (int i = 0; i < 50; i++) {
mData.add("huangxiaoguo" + i);
}
manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
mAdapter = new MyAdapter(this, mData);
recyclerView.setAdapter(mAdapter);
}
@Override
protected void onPause() {
super.onPause();
autoScrollViewPager.stopAutoScroll();
}
private void initListener() {
mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void OnItemClick(int position) {
Toast.makeText(MainActivity.this, mData.get(position), Toast.LENGTH_LONG).show();
}
});
// autoScrollViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
// @Override
// public Fragment getItem(int position) {
// return FragmentFactory.createFragment(position);
// }
//
// @Override
// public int getCount() {
// return 2;
// }
// });
autoScrollViewPager.setAdapter(new myPagerAdapter());
autoScrollViewPager.startAutoScroll(3000);
autoScrollViewPager.setInterval(3000);
autoScrollViewPager.setDirection(AutoScrollViewPager.RIGHT);
autoScrollViewPager.setCycle(true);
autoScrollViewPager.setScrollDurationFactor(20);
autoScrollViewPager.setStopScrollWhenTouch(true);
autoScrollViewPager.setSlideBorderMode(AutoScrollViewPager.SLIDE_BORDER_MODE_CYCLE);
autoScrollViewPager.setBorderAnimation(false);
autoScrollViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
UIUtils.showToast("" + position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private class myPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return 5;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = View.inflate(MainActivity.this, R.layout.image_view, null);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
方法简介:
recyclerViewHeader.attachTo(recyclerView);方法把头布局加入recyclerView中。
startAutoScroll() 开始自动滚动,延迟时间是2000毫秒。
startAutoScroll(int) 开始自动滚动延迟。
stopAutoScroll() 停止自动滚动。
setInterval(long) 设置自动滚动时间以毫秒为单位,默认是DEFAULT_INTERVAL。
setDirection(int) 设置自动滚动的方向,默认是RIGHT。
setCycle(boolean) 设置是否自动循环自动滚动到最后一个或第一项时,默认是true。
setScrollDurationFactor(double) 设置ViewPager滑动动画间隔时间的倍率,达到减慢动画或改变动画速度的效果
setSlideBorderMode(int) 将如何处理当滑动在最后或第一项,默认是SLIDE_BORDER_MODE_NONE。
setStopScrollWhenTouch(boolean) 设置是否停止自动滚动接触时,默认是true。
setBorderAnimation(boolean) 设置动画是否自动滚动在最后或第一项时,默认是true。
- RecyclerView的adatpter
package cn.hnshangyu.autoscrollandrecyclerviewheader;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context mContext;
private List<String> mData;
public MyAdapter(Context context, CopyOnWriteArrayList<String> mData) {
this.mContext = context;
this.mData = mData;
}
private OnItemClickListener onItemClickListener;
public interface OnItemClickListener {
void OnItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_view, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.textview.setText(mData.get(position));
holder.textview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onItemClickListener != null) {
onItemClickListener.OnItemClick(position);
}
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.textview)
TextView textview;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
好了,实现了....
Demo下载地址:http://download.csdn.net/detail/huangxiaoguo1/9743644
网友评论