CardView扩展时滚动

时间:2018-01-31 04:02:03

标签: android scroll android-recyclerview android-cardview

我有一个RecyclerView + CardView。 CardView可以在点击时扩展和缩小。问题是如果卡的扩展区域在屏幕外,用户可能认为没有发生任何事情。当卡片在屏幕外扩展时,如何滚动视图,以便可以看到整张卡片?

card.xml

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

    <RelativeLayout
        android:id="@+id/container_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_alignParentTop="true"
            android:scaleType="centerCrop" />    
        <TextView
            android:id="@+id/description_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/image"
            android:visibility="gone" />
    </RelativeLayout>

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

fragment_main.xml

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sample.SampleFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/card_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintTop_creator="1" />
</android.support.constraint.ConstraintLayout>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.sample.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

SampleFragment.java

public class SampleFragment extends Fragment {

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        ArrayList<Thing> things = new ArrayList<>();
        things.add(new Thing(...));

        RecyclerView recyclerView = rootView.findViewById(R.id.card_container);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);

        ThingAdapter thingAdapter = new ThingAdapter(things);
        recyclerView.setAdapter(thingAdapter);

        return rootView;
    }
}

ThingAdapter.java

public class ThingAdapter extends RecyclerView.Adapter<ThingHolder> {

    private ArrayList<Thing> things;

    ThingAdapter(ArrayList<Thing> things) {
        this.things = things;
    }

    @Override
    public ThingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card, parent, false);
        return new ThingHolder(v);
    }

    @Override
    public void onBindViewHolder(ThingHolder holder, int position) {
        holder.setThing(things.get(position));
    }

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

ThingHolder.java

class ThingHolder extends RecyclerView.ViewHolder {

    private TextView description;
    private ImageView image;
    private boolean isHidden = true;

    ThingHolder(View itemView) {
        super(itemView);
        description = itemView.findViewById(R.id.description_text);
        image = itemView.findViewById(R.id.image);

        //Sets the action to toggle when clicking the CardView.
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toggleVisibility();
            }
        });
    }

    void setThing(Thing thing) {
        description.setText(thing.getDescription());
        image.setImageResource(thing.getImageResourceId());
    }

    private void toggleVisibility() {
        if (isHidden) {
            description.setVisibility(View.VISIBLE);
        } else {
            description.setVisibility(View.GONE);
        }
        isHidden = !isHidden;
    }
}

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.your_recyclerview);

// get position of a View
CardView expandedCardView = (CardView) findViewById(R.id.your_card_view); //get the view by any means, if it is an array, just get the view that has been expanded
int cardViewbottomPosition = expandedCardView.getBottom();

// scroll to top of the view
recyclerView.scrollTo(0, cardViewbottomPosition);

您还可以提供一些代码,以便我们可以修改适合您代码的答案。

<强>更新

SampleFragment.java

public class SampleFragment extends Fragment {

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        ArrayList<Thing> things = new ArrayList<>();
        things.add(new Thing(...));

        RecyclerView recyclerView = rootView.findViewById(R.id.card_container);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);

        ThingAdapter thingAdapter = new ThingAdapter(things, new OnToggleVisibilityListener() {
            @Override
            public void onToggle(View view, int visibility) {
               int viewbottomPosition = view.getBottom();

               // scroll to top of the view
               recyclerView.scrollTo(0, viewbottomPosition);
            }
        });
        recyclerView.setAdapter(thingAdapter);

        return rootView;
    }
}

ThingAdapter.java

public class ThingAdapter extends RecyclerView.Adapter<ThingHolder> {

    private ArrayList<Thing> things;
    private OnToggleVisibilityListener onToggleVisibilityListener;

    ThingAdapter(ArrayList<Thing> things, OnToggleVisibilityListener onToggleVisibilityListener) {
        this.things = things;
        this.onToggleVisibilityListener = onToggleVisibilityListener;
    }

    @Override
    public ThingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card, parent, false);

        return new ThingHolder(v);
    }

    @Override
    public void onBindViewHolder(ThingHolder holder, int position) {
        holder.setThing(things.get(position));

        //Sets the action to toggle when clicking the CardView.
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.toggleVisibility();
                onToggleVisibilityListener.onToggle(holder.itemView, 
                    holder.description.getVisibility());       
            }
        });
    }

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

    public interface OnToggleVisibilityListener {
        void onToggle(View view, int visiblity);
    }
}

ThingHolder.java

class ThingHolder extends RecyclerView.ViewHolder {

    public TextView description;
    public ImageView image;
    private boolean isHidden = true;

    ThingHolder(View itemView) {
        super(itemView);
        description = itemView.findViewById(R.id.description_text);
        image = itemView.findViewById(R.id.image);
    }

    void setThing(Thing thing) {
        description.setText(thing.getDescription());
        image.setImageResource(thing.getImageResourceId());
    }

    public void toggleVisibility() {
        if (isHidden) {
            description.setVisibility(View.VISIBLE);
        } else {
            description.setVisibility(View.GONE);
        }
        isHidden = !isHidden;
    }
}

我已将部分代码从ThingHolder.java移至ThingAdapter.java。如果出现错误,请随时告诉我,特别是我有任何错别字。

答案 1 :(得分:0)

像这样更改您的recyclerView.Adapter

A,B

试试这个并告诉我们