【问题标题】:custom list view with edit text in fragment在片段中编辑文本的自定义列表视图
【发布时间】:2018-11-26 03:32:46
【问题描述】:

我有一个Fragment 选项卡,当用户单击添加Button 时,我需要生成一个新的EditText 行,用户在其中添加数据和一个保存按钮来保存这张图片的数据。

标签片段类

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);
}

}

【问题讨论】:

  • 为什么投反对票你能解释我的问题不清楚的地方
  • 你应该添加一个浮动操作按钮
  • 这段代码有什么问题?

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


【解决方案1】:

将 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);


}

【讨论】:

  • 我的问题是我需要一个自定义列表视图,其中包含 0 行的编辑文本,当我单击您在列表视图中创建的新行的 fab 按钮时,我可以在其中输入数据它
  • 这张图片只是一个设计,我需要创建我的列表视图,就像我所做的设计一样,当我点击工厂时,会创建一个新的编辑文本行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多