仿美团悬浮效果

filter_image.gif

PullToRefreshListView实现到顶部悬浮效果,这里主要是在ListView的addHeaderView里实现的,后面会讲在item里实现,以下是主要代码:

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private PullToRefreshListView pullToRefreshListView;
private List dataList = new ArrayList<>();
private FilterAdapter filterAdapter;

private View headerView;
private View headerView2;
private View filterView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    filterView = findViewById(R.id.filterView);
    pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pullToRefreshListView);

    //第一个headerView 这里加载美女图片的地方
    headerView = LayoutInflater.from(this).inflate(R.layout.item_head_filter, null);
    //setLayoutParams防止PullToRefreshListView报Caused by: java.lang.ClassCastException
    AbsListView.LayoutParams headerLayoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
    headerView.setLayoutParams(headerLayoutParams);

    //第二个headerView 这里到顶部后
    AbsListView.LayoutParams rgLayoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
    headerView2 = LayoutInflater.from(this).inflate(R.layout.layout_filter, null);
    headerView2.setLayoutParams(rgLayoutParams);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            ListView tempView = pullToRefreshListView.getRefreshableView();
            //添加HeaderView
            tempView.addHeaderView(headerView);
            tempView.addHeaderView(headerView2);
        }
    }, 0);


    pullToRefreshListView.getRefreshableView().setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            // 第二个headerView 到顶部时  悬浮tab出现
            if (firstVisibleItem < 2) {
                filterView.setVisibility(View.GONE);
            } else {
                filterView.setVisibility(View.VISIBLE);
            }
        }
    });
    for (int i = 0; i < 20; i++) {
        dataList.add("标题:" + i);
    }
    filterAdapter = new FilterAdapter(this, dataList);
    ListView actualListView = pullToRefreshListView.getRefreshableView();
    actualListView.setAdapter(filterAdapter);
    pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
}

class FilterAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List dataList;

    public FilterAdapter(Context context, List dataList) {
        this.mInflater = LayoutInflater.from(context);
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(android.R.id.text1);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(dataList.get(position));
        return convertView;
    }
  }

  public static class ViewHolder {
      public TextView textView;
  }
}

布局:

 
 

  

    

    



    

      
  

注意:因为ListView中的headView和悬浮在顶部的是两个不同的View,所以在点击时需要做状态同步

你可能感兴趣的:(仿美团悬浮效果)