gpt4 book ai didi

android - 在 fragment 中使用时 ListView 与 TabLayout 和按钮重叠

转载 作者:太空狗 更新时间:2023-10-29 13:58:57 24 4
gpt4 key购买 nike

我想在 fragment 中使用 ListView,顶部有可滑动的选项卡。这些选项卡与 ListView 重叠,并且当我尝试在选项卡之间滑动时应用程序崩溃。

它是这样的:

Screenshot_overlap

我一整天都在尝试寻找解决方案,但还没有找到适合我的解决方案...因为这是我在这里的第一个问题,我真的在努力尽可能地让人们理解我想要(如果您需要其他任何东西,该项目在 Git here 上)。

我将 CoordinatorLayout 用于应该膨胀的 fragment ,但我读到 ListView 并且该布局不能很好地协同工作,所以现在我只想让它使用 RelativeLayoutLinearLayout。我也尝试使用 RecyclerView 但那也不起作用。

我也尽可能地尝试在 LinearLayoutRelativeLayout 之间切换。所以现在我想听听是否有人可以帮助我。下面是代码现在的样子。

activity_main.xml-选项卡和 viewpager 有它们的 View

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</LinearLayout>

workout_list_layout.xml-设置liSTLayout的地方

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">

<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1.0">
</ListView>

</RelativeLayout>

fragment_workout.xml-应在选项卡下方显示列表的 fragment

<RelativeLayout
android:id="@+id/main_list"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_below="@id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/workoutList">

<include layout="@layout/workout_list_layout"
android:id="@+id/list"/>

</LinearLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_add_workout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_input_add"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_above="@id/workoutList"
/>

<Button
android:id="@+id/button_start_workout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_gravity="bottom|left"
android:text="Start"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_above="@id/workoutList"/>

</RelativeLayout>

PageFragment.java-处理所有 fragment

public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";

private int mPage;

public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view;
if(mPage == 1){
view = inflater.inflate(R.layout.fragment_workout, container, false);

WorkoutListFragment workoutListFragment = (WorkoutListFragment)getFragmentManager().findFragmentByTag("workoutlistfragment");
if (workoutListFragment == null){
workoutListFragment = new WorkoutListFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(android.R.id.content, workoutListFragment,"workoutlistfragment");
transaction.commit();
}


Button button = (Button) view.findViewById(R.id.button_start_workout);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_start_workout:
Intent intent = new Intent(view.getContext(), TimerWindow.class);
startActivity(intent);
break;
}
}
});

}else if(mPage == 2){
view = inflater.inflate(R.layout.fragment_performance, container, false);
}else{
view = inflater.inflate(R.layout.fragment_options, container, false);
}
return view;
}
}

WorkoutListFragment.java-膨胀 View 的列表 fragment

public class WorkoutListFragment extends ListFragment {
ArrayList<String> listItems = new ArrayList<String>(); //List items

ArrayAdapter<String> adapter;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.workout_list_layout, container, false);
return rootView;
}

public void onListItemClick(ListView listView, View view, int position, long id){
ViewGroup viewGroup = (ViewGroup) view;
TextView textView = (TextView) viewGroup.findViewById(R.id.textItem);
Toast.makeText(getActivity(), textView.getText().toString(), Toast.LENGTH_SHORT).show();

}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" , "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}

//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(String workoutTitle, View v) {
listItems.add(workoutTitle);
adapter.notifyDataSetChanged();
}
}

最佳答案

我可以看到您正在尝试用您的 fragment 替换您的“android.R.id.content”。但是 android.R.id.content 并不是你布局的一部分。相反,你应该为你的 fragment 创建一个占位符布局,在你的选项卡下和你的 viewpager 上,在你的主要 Activity xml 中,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />

<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</LinearLayout>

然后,在您的 PageFragment.java 中,用您的 fragment 替换(不要添加)您的占位符,如下所示:

    WorkoutListFragment workoutListFragment = (WorkoutListFragment) getFragmentManager().findFragmentByTag("workoutlistfragment");
if (workoutListFragment == null) {
workoutListFragment = new WorkoutListFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContainer, workoutListFragment).commit();
}

让我知道是否可行。

关于android - 在 fragment 中使用时 ListView 与 TabLayout 和按钮重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37287109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com