RecylerView page to win possession of a control slide

A talk entitled
1. I think the Lord created Adam and Eve, day and night, sky, birds .... Then there was fortunately we sat drinking coffee and tea house comfortable, middle of the night pounding excitement rag code does not sleep. "Bible" was first recorded by the so-called Jewish human intelligence, I hope my fingers beating out the perception and thinking of the code, to help themselves and others.

Second, as shown: this interface may appear in the project, part of the decline to win possession of the controls, the slide appeared

Write pictures described here

Analysis: We can see that this is a list of controls here RecylerView course ListView, ScrollView can be had.
Best to use RecylerView, RecylerView appears to solve a lot of problems, for example, where we often go to the source if the time, you will find
RecylerView provided a good multi-interface, so that we can get to the sliding distance. Two early ListView Oh no.
1. an interface sliding distance. We can get to an active distance dy
2. We can also restore disappear, we can think of property

Third, we start writing code

  通过查找RecylerVied的滑动监听方法,我们发现RecyclerView.OnScrollListener接口。
  那么我这里去写一个类继承它,在这里获取dy。代码如下:
  1.我们可以获取到dy我们如何来判断是隐藏还是出现呢?这里我们来设定一个THRESHOLE=20,
  如果向上滑动20那么就显示,如果向下滑动20就消失。
  2.这里我们需要确保执行的动画不能同时执行。所以我们添加一个标签来确定到底是哪一个动画在执行。
  3.我们这里写一个接口让MainActivity来回调。

  public class FabScrollListenner extends RecyclerView.OnScrollListener {
    private static final int THRESHOLE = 20;
    private int distance = 0;//滑动超过一定的距离那么就执行
    private HideScrollListenner hideScrollListenner;
    private boolean visible=true;//是否可见
    public FabScrollListenner(HideScrollListenner hideScrollListenner) {
        this.hideScrollListenner = hideScrollListenner;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        /*
        * dy:Y轴方向的增量
        * 有正负之分的。
        * 当真正执行动画的时候就不要执行其他动画了
        * */
        if (distance > THRESHOLE&&visible) {
            visible=false;
            //执行隐藏动画
            hideScrollListenner.onHide();
            distance = 0;
        } else if(distance<-20&&!visible){
            visible=true;
            //执行显示动画
            hideScrollListenner.onShow();

            distance = 0;
        }
        if(visible&&dy>0||(!visible&&dy<0)) {
            distance += dy;
        }
    }
}  

Callback interface


public interface HideScrollListenner {
   void onHide();
   void onShow();
}

Next we have written an interface, then we go to achieve it, and hide the code shown:
if you do not know and do not know how the animation property to get some margin of control can go look up how RelativeLayout.LayoutParams multi-
use.


public class MainActivity extends AppCompatActivity implements HideScrollListenner {
    private RecyclerView recyclerView;
    private Toolbar mToobar;
    private ImageButton imageButton;
    private ArrayList<String> mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recylerView);
        imageButton = (ImageButton) findViewById(R.id.fab);
        mToobar = (Toolbar) findViewById(R.id.toolbar);
        //这样太臃肿了。我们还是自定义一个类
        recyclerView.addOnScrollListener(new FabScrollListenner(this));
        mData=new ArrayList<>();
        for (int i = 0; i <20 ; i++) {
            mData.add("item="+i);
        }
        RecylerViewApdater madapter=new RecylerViewApdater(this,mData);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(madapter);
        madapter.notifyDataSetChanged();
        mToobar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,ListViewActivity.class));
            }
        });


    }

    @Override
    public void onHide() {

        //赢藏--属性动画
        RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) imageButton.getLayoutParams();
        imageButton.animate().translationY((imageButton.getHeight()+params.bottomMargin)).setInterpolator(new AccelerateInterpolator(3));
        //Toolbar
        RelativeLayout.LayoutParams params1= (RelativeLayout.LayoutParams) mToobar.getLayoutParams();
        mToobar.animate().translationY(-(mToobar.getHeight()+params1.bottomMargin)).setInterpolator(new AccelerateInterpolator(3));
    }

    @Override
    public void onShow() {
        //显示--属性动画
        imageButton.animate().translationY(0).setInterpolator(new DecelerateInterpolator(3));
        mToobar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(3));
    }
}

晚安:github:https://github.com/luhenchang/FloatingActionBarStudy.git

Published 47 original articles · won praise 54 · Views 150,000 +

Guess you like

Origin blog.csdn.net/m0_37667770/article/details/79606434