在cardviews之间滑动

时间:2016-05-18 15:05:05

标签: android android-viewpager android-recyclerview android-cardview

我正在使用Recyclerview和Cardviews在Android上进行简单的任务。我想在具有分页样式的cardviews之间滑动(滚动)(如viewpager)。

问题的一个示例(屏幕上有两个卡片视图,我只想显示一个,在页面之间滑动)

An example of the issue (you have two cardviews on the screen, I want to show only one, slide between them like pages)

有没有提示呢?

代码:

RecyclerViewAdapter(java)

public class RVASuggestion extends RecyclerView.Adapter<RVASuggestion.SuggestionViewHolder> {

    Context mContext;

    public static class SuggestionViewHolder extends RecyclerView.ViewHolder {

        TextView placeName;
        RatingBar rating;
        TextView openClose;
        TextView distance;
        ImageView picture;
        RelativeLayout openCloseLayout;
        ImageView phoneIcon;
        ImageView webIcon;
        ImageView mapIcon;

        public SuggestionViewHolder(View itemView) {
            super(itemView);
            placeName = (TextView) itemView.findViewById(R.id.placeName);
            rating = (RatingBar) itemView.findViewById(R.id.rating);
            openClose = (TextView) itemView.findViewById(R.id.openClose);
            distance = (TextView) itemView.findViewById(R.id.distanceText);
            picture = (SimpleDraweeView) itemView.findViewById(R.id.picture_place_suggest);
            phoneIcon = (ImageView) itemView.findViewById(R.id.phoneIcon);
            webIcon = (ImageView) itemView.findViewById(R.id.webIcon);
            mapIcon = (ImageView) itemView.findViewById(R.id.mapIcon);
            openCloseLayout = (RelativeLayout) itemView.findViewById(R.id.relativeLayoutOpenClose);
        }

    }

    List<Suggestion> suggestions;

    GlobalState gs;

    public RVASuggestion(GlobalState gs) {
        this.gs = gs;
        this.suggestions = gs.selectedCity.suggestions;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public SuggestionViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.suggestion_item, viewGroup, false);
        mContext = viewGroup.getContext();
        return new SuggestionViewHolder(v);
    }

    @Override
    public void onBindViewHolder(SuggestionViewHolder suggestionViewHolder, int i) {

        if (suggestions.get(i).rating != (float)0) {
            int numStars = (int) Math.ceil((Double.valueOf(suggestions.get(i).rating)));
            suggestionViewHolder.rating.setNumStars(numStars);
            suggestionViewHolder.rating.setRating(suggestions.get(i).rating);
        } else {
            suggestionViewHolder.rating.setNumStars(0);
            suggestionViewHolder.rating.setRating((float) 0);
            suggestionViewHolder.rating.setVisibility(View.GONE);
        }

        suggestionViewHolder.placeName.setText(suggestions.get(i).placeName);

        final Location depart = new Location("");
        depart.setLatitude(Double.parseDouble(gs.prefs.getString("currentLat", "0.0")));
        depart.setLongitude(Double.parseDouble(gs.prefs.getString("currentLng", "0.0")));

        final Location arrivee = new Location("");
        arrivee.setLatitude(Double.parseDouble(suggestions.get(i).latitude));
        arrivee.setLongitude(Double.parseDouble(suggestions.get(i).longitude));

        String distance = String.format("%.2f", depart.distanceTo(arrivee) / 1000) + "kms";
        suggestionViewHolder.openClose.setText(suggestions.get(i).openClose);
        suggestionViewHolder.distance.setText(distance);

        setOpenCloseColor(suggestionViewHolder,i);

        Uri uri = Uri.parse(suggestions.get(i).picture);
        suggestionViewHolder.picture.setImageURI(uri);

        final String currentPhone = suggestions.get(i).phone;
        final String currentName = suggestions.get(i).placeName;
        final String currentWeb = suggestions.get(i).web;
        final String currentLatitude = suggestions.get(i).latitude;
        final String currentLongitude = suggestions.get(i).longitude;

    }

    @Override
    public int getItemCount() {
        return (suggestions == null) ? 0 : suggestions.size();
    }

    // Fonction permettant de changer la couleur de l'horaire d'ouverture
    public void setOpenCloseColor(SuggestionViewHolder suggestionViewHolder, int i) {
        if (suggestions.get(i).openClose.equals("Ouvert")) {
            suggestionViewHolder.openCloseLayout.setBackground(ContextCompat.getDrawable(mContext, R.drawable.rounded_bg_green));
        } else if (suggestions.get(i).openClose.equals("Fermé")) {
            suggestionViewHolder.openCloseLayout.setBackground(ContextCompat.getDrawable(mContext, R.drawable.rounded_bg_red));
        } else {
            suggestionViewHolder.openCloseLayout.setBackground(ContextCompat.getDrawable(mContext, R.drawable.rounded_bg_orange));
        }
    }
}

Cardview项目(xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        xmlns:fresco="http://schemas.android.com/tools"
        android:id="@+id/cv_suggest"
        card_view:cardCornerRadius="5dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:background="#3A3A3A"
            >

            <com.facebook.drawee.view.SimpleDraweeView
                android:id="@+id/picture_place_suggest"
                android:layout_width="match_parent"
                android:layout_height="170dp"
                android:scaleType="centerCrop"
                android:alpha="0.8"
                />


            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/relativeLayoutOpenClose"
                android:layout_above="@+id/mapIcon"
                android:layout_alignStart="@+id/mapIcon"
                android:layout_toStartOf="@+id/relativeLayoutOpenClose"
                android:gravity="center"
                >

                <me.grantland.widget.AutofitTextView
                    android:maxLines="3"
                    android:singleLine="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/placeName"
                    android:text="Lieu"
                    android:textColor="@color/white"
                    android:textSize="25sp"
                    android:fontFamily="sans-serif-light"
                    android:textStyle="normal"

                    android:layout_alignParentTop="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentEnd="true"
                    android:gravity="center"
                    android:textAlignment="textStart"
                    android:layout_above="@+id/rating" />

                <RatingBar
                    android:id="@+id/rating"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:stepSize="0.1"
                    style="@style/RatingBar"

                    android:layout_alignParentBottom="true"
                    android:layout_alignParentStart="true" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/relativeLayoutOpenClose"
                android:background="@drawable/rounded_bg_green"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp"
                android:alpha="0.7">
                <ImageView
                    android:id="@+id/timeIcon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_query_builder_24dp"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentStart="true"
                    android:paddingRight="2dp"
                    android:paddingLeft="2dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/openClose"
                    android:textSize="20sp"
                    android:textColor="@color/white"
                    android:text="open"
                    android:layout_toEndOf="@+id/timeIcon"
                    android:paddingRight="4dp"/>
            </RelativeLayout>




            <ImageView
                android:layout_marginStart="10dp"
                android:padding="5dp"
                android:id="@+id/mapIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_map"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true" />

            <ImageView
                android:padding="5dp"
                android:id="@+id/phoneIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_phone"
                android:layout_alignTop="@+id/webIcon"
                android:layout_toEndOf="@+id/webIcon" />

            <ImageView
                android:padding="5dp"
                android:id="@+id/webIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_web"
                android:layout_alignTop="@+id/mapIcon"
                android:layout_toEndOf="@+id/mapIcon" />

            <ImageView
                android:id="@+id/navIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_navigation_14dp"
                android:paddingRight="2dp"
                android:paddingLeft="2dp"
                android:layout_alignParentBottom="true"
                android:paddingBottom="12dp"
                android:layout_toStartOf="@+id/distanceText" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/distanceText"
                android:textSize="13sp"
                android:textColor="@color/white"
                android:text="15 kms"
                android:paddingBottom="10dp"
                android:layout_alignParentBottom="true"
                android:layout_alignEnd="@+id/relativeLayoutOpenClose" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</RelativeLayout>

由于

0 个答案:

没有答案