如何在ViewPager每个项目的片段底部上方添加动作布局

时间:2019-11-09 06:18:08

标签: android android-studio android-layout android-fragments android-viewpager

我正在尝试使用tablayout创建一个viewpager,并且我想在button底部添加一个清晰的fragment,以清除{{1} },如图所示。

Image link or figure link

1 个答案:

答案 0 :(得分:0)

1-您的MainActivity布局:

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tab_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab1" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tab_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab2" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tab_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab3" />

    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </LinearLayout>

2-您的MainActivity和您的mPagerAdapter类:

 import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;

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

        tabLayout = findViewById(R.id.tablayout);
        viewPager = findViewById(R.id.viewPager);

        viewPager.setAdapter(new mPagerAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);
    }

    public class mPagerAdapter extends FragmentPagerAdapter {

        public mPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new Item1();
                case 1:
                    return new Item2();
                case 2:
                default:
                    return new Item3();
            }
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Tab 1";
                case 1:
                    return "Tab 2";
                case 2:
                default:
                    return "Tab 3";
            }
        }
    }

    public void removeFragment(int position) {
        tabLayout.removeTabAt(position);
    }
}

3-您的片段布局:

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


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_centerVertical="true"
        android:text="item1"/>

    <ImageView
        android:id="@+id/btnDelete"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:src="@android:drawable/ic_delete"/>
</RelativeLayout>

4-和您的Fragment类:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Item1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_1, container, false);

        view.findViewById(R.id.btnDelete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity) getActivity()).removeFragment(1);
            }
        });

        return view;
    }
}