android - 在android中的每个选项卡旁边添加按钮

标签 android tabs

我需要创建一个动态标签主机,其中涉及动态添加和删除标签。我能够动态添加和删除选项卡。但我需要在每个选项卡中都有删除按钮。还需要区分标签点击和删除标签点击的点击。请引用图片。我用谷歌搜索向每个选项卡添加按钮,找不到任何帮助。 enter image description here

编辑: 我已将自定义 View 添加到 tablayout。但我需要执行,单击关闭按钮以删除选项卡。我已经想通了如何添加和删除选项卡。但是我不知道如何获取关闭按钮的点击监听器。

自定义标签.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@string/font_fontFamily_medium"
        android:gravity="center_horizontal"
        android:textColor="@color/colorAccent"
        android:textSize="@dimen/tab_label" />

    <Button
        android:background="@drawable/close"
        android:id="@+id/btnClose"
        android:layout_width="40dp"
        android:layout_height="40dp"
        />
</LinearLayout>

MainActivity 和适配器:

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
    }

    /**
     * Adding custom view to tab
     */
    private void setupTabIcons() {


        View rootView1 = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        TextView tabOne = rootView1.findViewById(R.id.tab);

        tabOne.setText("ONE");

        tabLayout.getTabAt(0).setCustomView(rootView1);

        View rootView2 = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        TextView tabTwo = rootView2.findViewById(R.id.tab);
        tabTwo.setText("TWO");

        tabLayout.getTabAt(1).setCustomView(rootView2);

        View rootView3 = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        TextView tabThree = rootView3.findViewById(R.id.tab);
        tabThree.setText("THREE");

        tabLayout.getTabAt(2).setCustomView(rootView3);


    }

    /**
     * Adding fragments to ViewPager
     *
     * @param viewPager
     */
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new OneFragment(), "ONE");
        adapter.addFrag(new TwoFragment(), "TWO");
        adapter.addFrag(new ThreeFragment(), "THREE");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();
        private String tabTitles[] = new String[]{"Tab1", "Tab2", "Tab3"};
        private int[] imageResId = {R.drawable.ic_tab_contacts, R.drawable.ic_tab_call, R.drawable.ic_tab_favourite};

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

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

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }


    }
}

activity_main.xml:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="@dimen/custom_tab_layout_height"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

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

最佳答案

创建点击监听器,

View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case 1:
                    Toast.makeText(CircleActivity.this, "1", Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(CircleActivity.this, "2", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(CircleActivity.this, "Default", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

修改您的setupTabIcons() 方法,我只是删除了一些样板代码,

 private void setupTabIcons() {
        String[] arrTabTile = new String[]{"ONE", "TWO", "THREE"};
        for (int i = 0; i < arrTabTile.length; i++) {
            View rootView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
            TextView tabOne = rootView.findViewById(R.id.tab);
            tabOne.setText(arrTabTile[i]);
            Button btnClose = rootView.findViewById(R.id.btnClose);
            btnClose.setId(i + 1);
            btnClose.setOnClickListener(onClickListener);
            tabLayout.getTabAt(i).setCustomView(rootView);
        }
    }

解释如下:

btnClose.setId(i + 1);//use to idntify uniq button
btnClose.setOnClickListener(onClickListener);//assign click listener

关于android - 在android中的每个选项卡旁边添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58162637/

相关文章:

android - HttpUrlConnection 未联系服务器

带有 ImageButton 的 Android 自定义 ListView 没有获得焦点

android - 从 Android 本地主机 10.0.2.2 提供 GPS 坐标

javascript - 在 firefox 的 xul 中更改选项卡时获取当前 url

javascript - Jquery click 事件重复自身......有时

java - 卡片 View 未出现在 RecyclerView 中

android - 公共(public)地理推文是选择 Twitter 的 Search 还是 Streaming API?

c# - WPF 删除选项卡不处理内容?

javascript - 如何通过单击 IONIC 中的链接或 url 从一个选项卡导航到另一个选项卡?

tabs - 如何将 ionic 标签放置在屏幕底部?