使用RecyclerView快速返回

时间:2014-12-26 15:41:18

标签: android-recyclerview

我正在尝试将CheckBox(在RecyclerView上方)作为QuickReturn。简单的想法是在向下滚动时隐藏复选框,向上滚动并使其可见。 (如https://github.com/dbleicher/recyclerview-grid-quickreturn中所述。)

并且,它有点工作,但滚动时我有一个奇怪的闪烁。

对此有何看法?

MainActivity.java:

public class MainActivity extends ActionBarActivity {

    private CheckBox mCheckbox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mCheckbox = (CheckBox) findViewById(R.id.checkbox);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                // Simple check if moved vertically.
                // React to scrolls of a minimum amount (3, in this case)
                if (dy > 3) {

                    if (mCheckbox.getVisibility() == View.VISIBLE) {
                        mCheckbox.setVisibility(View.GONE);
                    }
                } else if (dy < -3) {

                    if (mCheckbox.getVisibility() == View.GONE) {
                        mCheckbox.setVisibility(View.VISIBLE);
                    }
                }
            }
        });

        List<String> dataset = Arrays.asList("one", "two", "three", "four", "five");
        CustomAdapter adapter = new CustomAdapter(dataset);
        recyclerView.setAdapter(adapter);
    }

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

        private List<String> mDataset;

        public CustomAdapter(List<String> dataset) {
            mDataset = dataset;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
            View v = LayoutInflater.from(arg0.getContext())
                    .inflate(R.layout.address_card, arg0, false);

            return new ViewHolder(v);
        }

        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.mText.setText(mDataset.get(position));
        }

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

        public class ViewHolder extends RecyclerView.ViewHolder  {

            private TextView mText;

            public ViewHolder(View v) {
                super(v);
                mText = (TextView) v.findViewById(R.id.cardText);
            }
        }
    }
}

activity_main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Check me" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

</LinearLayout>

adress_card.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:elevation="@dimen/cardview_default_elevation"
    card_view:cardCornerRadius="@dimen/cardview_default_radius" >

    <TextView
        android:id="@+id/cardText"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="TextView" />

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

1 个答案:

答案 0 :(得分:0)

在您的activity_main.xml中,您在LinearLayout中同时拥有复选框和RV。每当您将复选框设置为View.GONE时,它不仅会删除该视图,而且还必须重新计算RV的布局(并将其增长为full_size)。主线程上有很多工作要做。而不是LinearLayout,将两个项目添加到FrameLayout(RV首先,然后是复选框)。这将使复选框浮动在RV的顶部,但是当您隐藏复选框时,RV大小将不再更改。我怀疑这会消除闪烁。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

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

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Check me" />

</FrameLayout>

使用重力设置,您可以将复选框定位到您希望它出现的位置(在RV前面)。如果您希望它位于顶部,并且您不希望它隐藏RV中最顶层的项目(当它滚动到顶部时),请查看ItemDecorator类(QRBarDecoration)中的你在上面提到的同一个项目。

希望有所帮助。