【发布时间】:2018-10-27 17:59:27
【问题描述】:
我正在构建一个要显示 3 个页面的应用。为此,我正在使用选项卡式活动。
我的选项卡式活动与文本视图、按钮等一起工作正常。工作正常是指我可以随时看到选项卡栏并可以在选项卡之间导航。
现在,我正在尝试在其中一个选项卡中填充列表视图。此列表视图使用我构建的自定义行。我在启动时使用数组列表和适配器填充此列表视图,然后显示它。
现在,问题是每当列表视图使用我的自定义行(routelistingrow.xml)填充页面时,列表视图会立即接管活动全屏并覆盖选项卡栏,并且应用程序卡在此列表视图中。
然后我尝试使用基本活动(没有任何选项卡)执行这些步骤,只是为了查看列表视图是否会留在正确的位置并避免控制屏幕。这很有效,操作栏始终可见。
所以问题是当我尝试在多个页面之一中显示此列表视图时。
我将此视频用于标签
https://www.youtube.com/watch?v=ediklbippkA
我也尝试在导航抽屉中使用片段而不是选项卡,但仍然遇到同样的问题。
所以我想知道是什么导致了这个问题,以及为什么当我尝试从 main 填充它时列表视图会以这种方式运行。
我还发现当我尝试在单击按钮时动态添加行时会出现此问题
我将 Main2Activity 保留为默认设置并添加了以下内容:
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.tab_routelistings);
listView = findViewById(R.id.routelistingslistview);
dataModels= new ArrayList<>();
dataModels.add(new RouteTemplate("Canada", "USA"));
dataModels.add(new RouteTemplate("USA", "Canda"));
dataModels.add(new RouteTemplate("Canada", "USA"));
dataModels.add(new RouteTemplate("USA", "Canda"));
dataModels.add(new RouteTemplate("Canada", "USA"));
dataModels.add(new RouteTemplate("USA", "Canda"));
adapter = new RouteAdapter(dataModels, getApplicationContext());
listView.setAdapter(adapter);
}
//i only added the switch statement within this method found in main2activity.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch(position)
{
case 0:
TabRouteListings tabRouteListings = new TabRouteListings();
return tabRouteListings;
case 1:
TabCreateRoute tabCreateRoute = new TabCreateRoute();
return tabCreateRoute;
default:
TabRouteListings tabRouteListings2 = new TabRouteListings();
return tabRouteListings2;
}
}
TabCreateRoute.java
public class TabCreateRoute extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_createroute, container, false);
return view;
}
}
TabRouteListings.xml
<?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">
<ListView
android:id="@+id/routelistingslistview"
android:layout_width="wrap_content"
android:layout_height="match_parent"></ListView>
</LinearLayout>
routelistingrow.xml
<?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:orientation="vertical"
android:padding="10dp">>
<TextView
android:id="@+id/txtstartaddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Start Address"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/txtendaddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtstartaddress"
android:layout_marginTop="5dp"
android:text="End Address"
android:textColor="@android:color/black" />
<ImageView
android:id="@+id/item_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@android:drawable/ic_dialog_info" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<!--<Button-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Modify"-->
<!--android:textColor="@android:color/black"-->
<!--android:textStyle="bold" />-->
<!--<Button-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Delete"-->
<!--android:textAppearance="?android:attr/textAppearanceButton"-->
<!--android:textColor="@android:color/black"-->
<!--android:textStyle="bold" />-->
</LinearLayout>
</RelativeLayout>
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ca.mcgill.ecse321.driver">
<uses-permission android:name="android.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".LoginActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".RegisterActivity" />
<activity
android:name=".Main2Activity"
android:label="@string/title_activity_main2"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
过去 3 天我一直在为此苦苦挣扎,想知道我是否最好采用其他方法(以防您找不到解决方案)。
【问题讨论】:
标签: android android-studio listview tabs