1
所以我有一個TabbedActivity與一個額外的類擴展片段,但列表不會留在我的屏幕。Android的片段佈局不在屏幕
正如你可以看到應該是在屏幕的底部附加小時。
這裏是我的片段layout.xml
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CreateGameActivity">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/create_game_participants_tab_title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/colorPrimary"/>
<ListView
android:id="@+id/listParticipants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView4"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"/>
<TextView
android:id="@+id/textNoParticipants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_alignStart="@+id/textView4"
android:layout_alignTop="@+id/listParticipants"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:text="@string/no_participants"/>
</RelativeLayout>
這是由以下onCreateView()方法加載
View view = inflater.inflate(R.layout.fragment_create_game_participants_layout, container, false);
ListView listParticipants = (ListView) view.findViewById(R.id.listParticipants);
TextView textNoParticipants = (TextView) view.findViewById(R.id.textNoParticipants);
List<String> list = new LinkedList<>();
if (savedInstanceState != null && savedInstanceState.containsKey("participants"))
list.addAll((List<String>)savedInstanceState.getSerializable("participants"));
else {
SharedPreferences prefs = context.getSharedPreferences("participants", Context.MODE_PRIVATE);
list.addAll(prefs.getStringSet("lastParticipants", new HashSet<>()));
}
ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_1, android.R.id.text1, list);
listParticipants.setAdapter(adapter);
SwipeDismissListViewTouchListener touchListener = new SwipeDismissListViewTouchListener(
listParticipants,
new SwipeDismissListViewTouchListener.DismissCallbacks {
boolean canDismiss(int position) {
return true;
}
void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions)
list.removeAt(position);
}
});
listParticipants.setOnTouchListener(touchListener);
listParticipants.setOnScrollListener(touchListener.makeScrollListener());
return view;
片段使用其構造函數創建和
活動加載的onCreate()super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_game_activity_tabbed)
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
supportActionBar.setDisplayHomeAsUpEnabled(true);
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
viewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager)
最後我的選項卡式活動layout.xml被定義爲像這樣
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="janniskaiser.matchplanner.CreateGameActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:layout_scrollFlags="scroll|enterAlways">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_ok" />
</android.support.design.widget.CoordinatorLayout>
它應該向下滾動直到你的'H'。 –
它應該是的,但正如你可能從我屏幕底部的淺藍色半弧看到的那樣,它沒有。 – succcubbus
數據來自哪裏?你在哪裏存儲你的包/共享偏好? –