Android顶部三个TabLayout,滑动到某条目,就会切换到相应的TabLayout

先上效果图 凑合看:

Android顶部三个TabLayout,滑动到某条目,就会切换到相应的TabLayout_第1张图片

 

xml布局:


    

        

        

        
    
    
MainActivity中:
public class MainActivity extends AppCompatActivity implements AbsListView.OnScrollListener {

    private TabLayout tb;
    private ListView lv;
    private ArrayList list;
    private int position;
    private CustomAdapter adapter1;
    int post;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = new ArrayList<>();
        for (int i = 0; i <200; i++) {
            list.add("龙雀" + i);
        }
        initView();

    }

    private void initView() {
        tb = (TabLayout) findViewById(R.id.tb);
        lv = (ListView) findViewById(R.id.lv);
        adapter1 = new CustomAdapter(list,this);
        lv.setAdapter(adapter1);
        lv.setOnScrollListener(this);
        for (int i = 0; i < tb.getTabCount(); i++) {
            TabLayout.Tab tab = tb.getTabAt(i);
            if (tab == null)
                return;

            Class c = tab.getClass();
            try {

                Field field = c.getDeclaredField("mView");

                field.setAccessible(true);
                final View view = (View) field.get(tab);
                if (view == null)
                    return;
                view.setTag(i);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        position = (int) view.getTag();
                        switch (position) {

                            case 0:
                                lv.setSelection(0);
                                break;
                            case 1:

                                lv.setSelection(50);


                                break;
                            case 2:
                                lv.setSelection(70);
                                break;
                        }

                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
            case 0:
                switch (post) {
                    case 0:

                        TabLayout.Tab tab = tb.getTabAt(0);
                        tab.select();
                        break;

                    case 50:
                        TabLayout.Tab tab2 = tb.getTabAt(1);
                        tab2.select();

                        break;
                    case 70:
                        TabLayout.Tab tab3 = tb.getTabAt(2);
                        tab3.select();

                        break;


                }
                Toast.makeText(this, "我现在处于不滑动状态", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(this, "我现在处于滑动状态", Toast.LENGTH_SHORT).show();
                break;
        }

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        switch (firstVisibleItem) {

            case 0:
                post = 0;
                break;

            case 50:
                post = 50;
                break;
            case 70:
                post = 70;
                break;
            case 49:
                post = 0;
                break;

            case 69:
                post = 50;
                break;

        }
    }
}

适配器:

public class CustomAdapter extends BaseAdapter {
    private ArrayList list = new ArrayList<>();
    private Context context;

    public CustomAdapter(ArrayList list, Context context) {
        this.list = list;
        this.context = context;
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        viewHolder.tv.setText(list.get(position));
        return convertView;
    }

    public static class ViewHolder {
        public View rootView;
        public TextView tv;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv = (TextView) rootView.findViewById(R.id.tv);
        }
    }
}

你可能感兴趣的:(Android顶部三个TabLayout,滑动到某条目,就会切换到相应的TabLayout)