Кнопка не отображается под ListView в TabLayout - PullRequest
0 голосов
/ 25 апреля 2018

Я использую Android Studio v3.1.2. У меня есть пара действий с ListView и кнопкой. Кнопка должна выровнятьParentBottom независимо от количества элементов в ListView.

На моей MainActivity ListView и Button отлично отображаются на RelativeLayout. Я могу добавить множество элементов в ListView, и кнопка остается на месте.

Мой JournalTabFragment, с другой стороны, является фрагментом, использующим RelativeLayout в TabLayout. Фрагмент создается в коде с помощью PetDetailTabbedActivity. Макет фрагмента является модифицированной версией макета MainActivity, но кнопка не отображается, даже если ListView не содержит данных. Я попытался использовать LinearLayout с весами с ограниченным успехом - кнопка появляется, но она плавает ниже содержимого списка и перемещается вниз по мере добавления данных в ListView. Затем он сжимается по вертикали, когда в ListView есть несколько строк.

Вот самый последний макет, который я пробовал:

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

    <LinearLayout
        android:id="@+id/topLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:padding="8dp"
        >

        <ListView
            android:id="@+id/journallistview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

    <Button
        android:id="@+id/addentrybtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_margin="8dp"
        android:text="@string/addentrybtn" />

</RelativeLayout>

Поскольку он так похож на макет в MainActivity, он кажется таким, каким он должен работать, поэтому я не могу не думать, что TabLayout как-то с этим связан.

Чего мне не хватает ?! Как мне заставить кнопку прилипать к нижней части макета «Деятельности», не нажимая на нее? ТИА за помощь. Вот Java для родительского действия, которое создает фрагменты.

public class PetDetailTabbedActivity extends AppCompatActivity implements DeleteConfirmDialog.DeleteConfirmDialogListener {

    private SectionsPageAdapter mSectionsPageAdapter;
    private ViewPager mViewPager;
    DatabaseHelper myDB;
    Integer iPetID = 0;
    private int REQUEST_CODE = 1;
    JournalTabFragment journalFrag;

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

        myDB = new DatabaseHelper(this);

        //load the toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //let's give it a back button in the title bar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        assert getSupportActionBar() != null;

        Bundle bundle = getIntent().getExtras();
        if(bundle != null) {
            //get the pet ID from the extras
            iPetID = bundle.getInt("PetID");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if(iPetID > 0) {
            String sPetName = myDB.getPetNameByID(iPetID);
            getSupportActionBar().setTitle(sPetName);
        }
        mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        mViewPager = findViewById(R.id.container);
        setupViewPager(mViewPager);

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

    private void setupViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter((getSupportFragmentManager()));

        String sStatus = myDB.getPetStatusByPetID(iPetID);

        journalFrag = new JournalTabFragment();
        Bundle jbundle = new Bundle();
        jbundle.putInt("PetID", iPetID);
        journalFrag.setArguments(jbundle);

        PetDetailTabFragment petFrag = new PetDetailTabFragment();
        Bundle gbundle = new Bundle();
        gbundle.putInt("PetID", iPetID);
        petFrag.setArguments(gbundle);

        //add the tabs
        adapter.addFragment(journalFrag, "Journal");
        adapter.addFragment(petFrag,"Details");

        viewPager.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.detailsmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
            case R.id.home:
                goHome();
                return true;
            case R.id.edit:
                openEditActivity();
                return true;
            case R.id.delete:
                deletePet();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

    private void goHome() {
        Intent intent = new Intent(PetDetailTabbedActivity.this, MainActivity.class);
        startActivity(intent);
    }

    private void openEditActivity() {
        Intent editIntent;
        editIntent = new Intent(PetDetailTabbedActivity.this, EditPetActivity.class);
        editIntent.putExtra("PetID", iPetID);
        startActivityForResult(editIntent, REQUEST_CODE);
    }


    private void deletePet() {

        //bundle up the iEntryID for the dialog to pass back;
        //    otherwise it's null when the dlg closes
        Bundle args = new Bundle();
        args.putInt("ItemID", iPetID);

        DeleteConfirmDialog dlg = new DeleteConfirmDialog();
        dlg.setArguments(args);
        dlg.show(getSupportFragmentManager(), "Confirm Delete Dialog");
    }

    @Override
    public void applyValue(int itemid) {

        //the deletePet method in the db helper class also deletes journal entries for this pet
        Integer iPetID = (Integer)itemid;
        boolean bSuccess = myDB.deletePet(iPetID);
        if (bSuccess == true) {
            displayToastShort("Successfully deleted");
            Intent gobacktoprevactivity = new Intent();
            setResult(RESULT_OK,gobacktoprevactivity);
            finish();
        }
        else
            displayToastLong("Cannot delete the pet");
    }

    public void displayToastLong(String message) {
        StyleableToast.makeText(this, message, Toast.LENGTH_LONG, R.style.AccentToast).show();
    }
    public void displayToastShort(String message) {
        StyleableToast.makeText(this, message, Toast.LENGTH_SHORT, R.style.AccentToast).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                recreate();
                journalFrag.adapter.notifyDataSetChanged();
            }
        }
    }

}

Screenshot of ListView but no button on the bottom

ОБНОВЛЕНИЕ: Вот компоновка, которая частично работает (показывает кнопку). Как вы можете видеть, он всплывает снизу, когда в ListView мало строк, и опускается, когда есть несколько строк.

<?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="match_parent"
    android:orientation="vertical"
    android:padding="8dp">

    <ListView
        android:id="@+id/journallistview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0" />

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="3"/>


    <LinearLayout
        android:id="@+id/buttonlayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/addentrybtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/addentrybtn" />

    </LinearLayout>

</LinearLayout>

enter image description here

Ответы [ 2 ]

0 голосов
/ 25 апреля 2018

Напишите это внутри вашего тега LinearLayout

android:layout_above="@+id/addentrybtn"

Это поместит ваш список над кнопкой, которая находится внизу.

0 голосов
/ 25 апреля 2018

ОБНОВЛЕНИЕ

Перечитав вашу проблему, я настроил проект, который использует действие с двумя фрагментами, как вы описали выше.

Вот макет JournalFragment(Схема, предложенная в предыдущем ответе):

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

    <LinearLayout
        android:id="@+id/topLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/addentrybtn"
        android:orientation="vertical"
        android:padding="8dp"
        >

        <ListView
            android:id="@+id/journallistview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

    <Button
        android:id="@+id/addentrybtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_margin="8dp"
        android:text="ADD ENTRY" />

</RelativeLayout>

с кодом для JournalFragment:

public class JournalFragment extends Fragment {


  public static JournalFragment newInstance(){
    return new JournalFragment();
  }

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


  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    ListView listView = view.findViewById(R.id.journallistview);
    final ListAdapter listAdapter = new ArrayAdapter<>(view.getContext(),  android.R.layout.simple_list_item_1, getItems(100));
    listView.setAdapter(listAdapter);
  }


  private String []  getItems(int count){
    String [] items = new String[count];
    for(int i = 0; i < count; i++){
      items[i] = "Item "+ i;
    }
    return items;
  }
}

DetailFragment здесь бесполезен (заглушен для целей отображения), но для полноты я включу его:

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

    <android.support.v7.widget.AppCompatTextView
        android:layout_centerInParent="true"
        android:text="Detail Fragment!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

код для DetailFragment (опять же для полноты):

public class DetailFragment extends Fragment {

  public static DetailFragment newInsance(){
    return new DetailFragment();
  }

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

Вот макет Activity (скелеты RelativeLayout, Toolbar, TabLayout и ViewPager):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/journalToolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/journalToolbar"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:tabTextColor="#fff"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tabs" />

</RelativeLayout>

Код активности:

public class MainActivity extends AppCompatActivity {

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

    Toolbar toolbar = findViewById(R.id.journalToolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    ViewPager viewPager = findViewById(R.id.viewPager);
    TabLayout tabLayout = findViewById(R.id.tabs);
    viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
    tabLayout.setupWithViewPager(viewPager);
  }


  private final class PagerAdapter extends FragmentPagerAdapter {

    PagerAdapter(FragmentManager fm) {
      super(fm);
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
      return position == 0 ? getString(R.string.journal) : getString(R.string.details);
    }

    @Override
    public Fragment getItem(int position) {

      if(position == 0){
        return JournalFragment.newInstance();
      }else{
        return DetailFragment.newInsance();
      }
    }

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

Результат: Activity with Tabs and docked button

Удачи и счастливого кодирования!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...