RecyclerView中的轮播

时间:2017-02-13 09:15:31

标签: java android android-viewpager android-recyclerview carousel

我想在RecyclerView中显示类似项目的轮播。我使用lib CarouselView

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView list = (RecyclerView) findViewById(R.id.list);
        list.setHasFixedSize(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        list.setLayoutManager(layoutManager);
        List<Integer> numbers = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            numbers.add(i);
        }
        final CarouselAdapter adapter = new CarouselAdapter(this, numbers);
        list.setAdapter(adapter);
        final SwipeRefreshLayout swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
        swipeRefresh.setColorSchemeResources(R.color.green);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
                public void onRefresh() {
                swipeRefresh.setRefreshing(false);
                adapter.notifyDataSetChanged();
            }
        });
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout           
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/green"
        android:scrollbars="vertical"/>

</android.support.v4.widget.SwipeRefreshLayout>

CarouselAdapter.java

public class CarouselAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<Integer> numbers;

    public CarouselAdapter(Context context, List<Integer> numbers) {
        this.context = context;
        this.numbers= numbers;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, null);
        return new CarouselViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        ((CarouselViewHolder) holder).carousel.setPageCount(numbers.size());
        ((CarouselViewHolder) holder).carousel.setViewListener(viewListener);
    }

    @Override
    public int getItemCount() {
        return numbers.size();
    }

    private ViewListener viewListener = new ViewListener() {
        @Override
        public View setViewForPosition(int position) {
            TextView textView = (TextView) LayoutInflater.from(context).inflate(R.layout.item_carousel, null);
            textView.setText(String.valueOf(numbers.get(position)));
            return textView;
        }
    };

    private class CarouselViewHolder extends RecyclerView.ViewHolder {

        public CarouselView carousel;

        private CarouselViewHolder(View itemView) {
            super(itemView);
            carousel = (CarouselView) itemView.findViewById(R.id.carousel);
        }
    }
}

item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<com.synnapps.carouselview.CarouselView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/carousel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:fillColor="@color/green"/>

item_carousel.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="16sp"/>

我从CarouselView更改了CarouselViewPager,添加了下一个代码

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = 0;
    if (getChildCount() != 0) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if (h > height) height = h;
        }
    } else {
        height = heightMeasureSpec;
    }
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

但是当调用刷卡刷新和适配器更新时,轮播中的所有视图都会消失。我认为在adapter.notifyDataSetChanged()之后没有调用onMeasure这个问题。我怎么解决它?

0 个答案:

没有答案