gpt4 book ai didi

java - 如何成功实现 viewpager 和 recyclerView 从 Activity 迁移到 fragment

转载 作者:行者123 更新时间:2023-11-29 02:17:38 26 4
gpt4 key购买 nike

我正在学习 udacity android basic nanodegree 类(class)并实现 recyclerView viewPager 我们被指示将代码从 Activity 移动到 fragment ,我已经按照说明进行操作但是当我运行该应用程序时,它甚至在显示第一页和我的之前就崩溃了调试器将我指向 MainActivity.java 文件中的一行(第 18 行),即 setContentView(R.layout.activity_main);我已经完成了在堆栈溢出和我找到的其他教程中可以找到的所有修复程序,但似乎对我没有任何帮助。我不知道还能做什么。帮帮忙。请

package com.example.tournaija;

import android.os.Bundle;
import androidx.viewpager.widget.PagerAdapter;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);

// Find the view pager that will allow the user to swipe between fragments
ViewPager viewPager = findViewById(R.id.viewpager);

// Create an adapter that knows which fragment should be shown on each page
CategoryAdapter adapter = new CategoryAdapter(getSupportFragmentManager());

// Set the adapter onto the view pager
viewPager.setAdapter(adapter);
}
}

这是主要 Activity 的xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.example.tournaija.MainActivity">

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

</LinearLayout>

适配器

package com.example.tournaija;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentActivity;


/**
* {@link CategoryAdapter} is a {@link FragmentPagerAdapter} that can provide the layout for
* each list item based on a data source which is a list of {@link Place} objects.
*/
public class CategoryAdapter extends FragmentPagerAdapter {

/**
* Create a new {@link CategoryAdapter} object.
*
* @param fm is the fragment manager that will keep each fragment's state in the adapter
* across swipes.
*/
public CategoryAdapter(FragmentManager fm) {
super(fm);
}

/**
* Return the {@link Fragment} that should be displayed for the given page number.
*/
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new HotelsFragment();
} else if (position == 1) {
return new RestuarantsFragment();
} else if (position == 2) {
return new StadiumsFragment();
} else if (position == 3) {
return new MuseumsFragment();
} else {
return new MallsFragment();
}
}

/**
* Return the total number of pages.
*/
@Override
public int getCount() {
return 5;
}
}

其中一个 Activity 和相应的带有xml的 fragment

Activity

package com.example.tournaija;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class HotelsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new HotelsFragment())
.commit();
}
}

fragment

package com.example.tournaija;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

/**
* A simple {@link Fragment} subclass.
*/
public class HotelsFragment extends Fragment {

ArrayList<Place> placeArrayList = new ArrayList<>();

public HotelsFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.place_list, container, false);
placeArrayList.add(new Place("Radisson Blu Anchorage Hotel, Lagos",
"Situated in the heart of Lagos, Nigeria, Radisson " +
"Blu Anchorage Hotel, is a stunning masterpiece styled " +
"by famous Swedish hospitality designer, Christian Lundwall. " +
"The Radisson Blu Anchorage Hotel, Lagos, Nigeria is nestled " +
"along the Lagos Lagoon on Victoria Island in the midst of the " +
"business district and banking area. This Lagos hotel has " +
"170 air-conditioned rooms, including 8 unique and opulently " +
"furnished suites, offering both stunning city and lagoon views. " +
"The Voyage buffet-style restaurant serves a range of eclectic, " +
"international cuisine and local dishes, while overlooking " +
"the Lagos Lagoon. The View bar lounge is ideal for snacks and " +
"drinks, and perfect for a Classic Cocktail as the sun sets. " +
"Providing jetty access, the Surface Bar and Grill on the terrace " +
"offers outdoor dining overlooking the lagoon. Guests enjoy free " +
"access to the hotel fitness area, complete with cardiovascular gym, " +
"massage room, outdoor infinity swimming pool, and steam room.The hotel " +
"boasts two upscale boardrooms and four spacious meeting rooms, all " +
"offering natural light as well as all the modern, audiovisual " +
"equipment expected of a World Class Hotel. All you need for " +
"a successful stay!",
R.drawable.radisson_blu));

PlaceAdapter placeAdapter = new PlaceAdapter(getActivity(), placeArrayList);
ListView placeList = rootView.findViewById(R.id.place_layout);

// hooking the adapter
placeList.setAdapter(placeAdapter);

return rootView;
}


}

XML

<?xml version="1.0" encoding="utf-8"?>
<ListView 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:id="@+id/place_layout"/>

最佳答案

只需在 MainActivity.onCreate() 中添加一个 Fragment ...否则这些字段将成为 MainActivity 的成员,而不是任何 Fragment,因为它通常被认为是。此外,在 XML 中使用 android.support.v4.view.ViewPager 是无意义的。

关于java - 如何成功实现 viewpager 和 recyclerView 从 Activity 迁移到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59382910/

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