片段中带有编辑文本的自定义列表视图

时间:2018-11-26 03:32:47

标签: android listview android-fragments tabs google-cloud-firestore

我有一个Fragment标签,当用户单击添加Button时,我需要生成EditText的新行,并在其中添加用户添加的数据以及一个保存按钮,将数据保存到此图片中。

listview in fragment with add button picture

标签片段类

public class ItemsCatTabActivity extends Fragment {

public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 8;




@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //this inflates out tab layout file.
    View x =  inflater.inflate(R.layout.items_pager_activity,null);
    // set up stuff.
    tabLayout = (TabLayout) x.findViewById(R.id.tabs);
    viewPager = (ViewPager) x.findViewById(R.id.viewpager);

    // create a new adapter for our pageViewer. This adapters returns child fragments as per the positon of the page Viewer.
    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

    // this is a workaround
    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            //provide the viewPager to TabLayout.
            tabLayout.setupWithViewPager(viewPager);
        }
    });
    //to preload the adjacent tabs. This makes transition smooth.
    viewPager.setOffscreenPageLimit(5);

    return x;
}

class MyAdapter extends FragmentPagerAdapter {

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

    //return the fragment with respect to page position.
    @Override
    public Fragment getItem(int position)
    {
        switch (position){
            case 0 : return new AddNewItemFragment();
            case 1 : return new AddNewItemFragment();
            case 2 : return new AddNewItemFragment();
            case 3 : return new AddNewItemFragment();
            case 4 : return new AddNewItemFragment();
            case 5 : return new AddNewItemFragment();
            case 6 : return new AddNewItemFragment();
            case 7 : return new AddNewItemFragment();
            case 8 : return new AddNewItemFragment();
        }
        return null;
    }

    @Override
    public int getCount() {

        return int_items;

    }

    //This method returns the title of the tab according to the position.
    @Override
    public CharSequence getPageTitle(int position) {

        switch (position){
            case 0 :
                return "CIGARETTE";
            case 1 :
                return "Sweet";
            case 2:
                return "coin";
            case 3:
                return "hot drinks";
            case 4:
                return "cold drinks";
            case 5:
                return "cold drinks";
            case 6:
                return "cold drinks";
            case 7:
                return "cold drinks";
            case 8:
                return "cold drinks";
        }
        return null;
    }
}

}

我需要在其中添加代码的地方

AddNewItemFragment类

public class AddNewItemFragment extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.new_items_swipe_activity_design,null);
}

}

1 个答案:

答案 0 :(得分:0)

在框架布局中放置一个FloatingActionButton和edittext,并相应设置其重力和填充。

    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        <android.support.design.widget.FloatingActionButton
                android:id="@+id/floatingButtonAdd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"   
                android:backgroundTint="@color/Green"
                android:contentDescription="@null"
                android:gravity="bottom|end"
                android:padding="3dp"
                android:src="@drawable/check" />

 <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/fab"
        android:clickable="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true">
       <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Item name"
            android:id="@+id/input" />
    </android.support.design.widget.TextInputLayout>
    </FrameLayout>

在您的代码中

public class AddNewItemFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

 mView = inflater.inflate(R.layout.new_items_swipe_activity_design, false);

 fab = (FloatingActionButton) mView.findViewById(R.id.floatingButtonAdd);
        fab.bringToFront();
        input = (EditText) mView.findViewById(R.id.input);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

          //do your code
            }
        });

    return inflater.inflate(R.layout.new_items_swipe_activity_design,null);


}