如何将片段添加到列表
How to add a Fragment to List
我创建了以下包:
首页 -> HomeActivity.class,HomeFragment.class,SectionsPageAdapter.class
好友 -> Friends.class
消息 -> Messages.class
SectionsPageAdapter 扩展了 FragmentPagerAdapter 来管理片段,
SectionsPageAdapter中写的代码是:
package com.example.sk.voiceapplication.HomePage;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class SectionsPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragment_hierarchy = new ArrayList<>();
public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragment_hierarchy.get(position);
}
@Override
public int getCount() {
return fragment_hierarchy.size();
}
public void addFragment(Fragment fragment){
fragment_hierarchy.add(fragment);
}
}
Class 主页片段:
package com.example.sk.voiceapplication.HomePage;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.example.sk.voiceapplication.R;
public class HomeFragment extends Fragment {
private static final String TAG = "HomeFragment";
private Button btn_test;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment,container,false);
btn_test = (Button) view.findViewById(R.id.button2);
btn_test.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"Testing Button Click 2", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
现在 Class Friends 和 Messages 与 HomeFragment 相同,但需要进行一些小改动
在 class HomeActivity 我写了:
package com.example.sk.voiceapplication.HomePage;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.example.sk.voiceapplication.Friends.Friends;
import com.example.sk.voiceapplication.Messages.Messages;
import com.example.sk.voiceapplication.Profile.MyProfile;
import com.example.sk.voiceapplication.R;
public class HomeActivity extends AppCompatActivity {
private static final String TAG = "HomeActivity";
private SectionsPageAdapter sectionsPagerAdapter;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setupFragments();
}
private void setupFragments() {
sectionsPagerAdapter = new SectionsPageAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0);
tabLayout.getTabAt(1);
tabLayout.getTabAt(2);
}
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new HomeFragment());
adapter.addFragment(new Messages());
adapter.addFragment(new Friends());
viewPager.setAdapter(adapter);
}
}
在 HomeActivity 中,当我创建 Fragments 对象并使用 Line
将它们添加到 SectionsPageAdapter 时
adapter.addFragment(new HomeFragment());
显示错误:
SectionsPageAdapter 中的 addFragment(android.support.v4.app.Fragment) 无法应用于 (com.example.sk.voiceapplication.HomePage.HomeFragment)
与
类似
adapter.addFragment(新消息());
SectionsPageAdapter 中的 addFragment(android.support.v4.app.Fragment) 无法应用于 (com.example.sk.voiceapplication.Messages.Messages)
adapter.addFragment(新朋友());
SectionsPageAdapter 中的 addFragment(android.support.v4.app.Fragment) 无法应用于 (com.example.sk.voiceapplication.Friends.Friends)
我做错了什么?
解决方法是什么?
您缺少导入 import android.support.v4.app.Fragment;
。您必须使用 V4 片段而不是 android.app.Fragment
.
替换-:
import android.app.Fragment;
收件人-:
import android.support.v4.app.Fragment;
在你所有的片段中。
将此更改为-:
public class HomeFragment extends Fragment
{
}
至-:
public class HomeFragment extends android.support.v4.app.Fragment
{
}
在你所有的三个片段中
我创建了以下包:
首页 -> HomeActivity.class,HomeFragment.class,SectionsPageAdapter.class
好友 -> Friends.class
消息 -> Messages.class
SectionsPageAdapter 扩展了 FragmentPagerAdapter 来管理片段, SectionsPageAdapter中写的代码是:
package com.example.sk.voiceapplication.HomePage;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class SectionsPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragment_hierarchy = new ArrayList<>();
public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragment_hierarchy.get(position);
}
@Override
public int getCount() {
return fragment_hierarchy.size();
}
public void addFragment(Fragment fragment){
fragment_hierarchy.add(fragment);
}
}
Class 主页片段:
package com.example.sk.voiceapplication.HomePage;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.example.sk.voiceapplication.R;
public class HomeFragment extends Fragment {
private static final String TAG = "HomeFragment";
private Button btn_test;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment,container,false);
btn_test = (Button) view.findViewById(R.id.button2);
btn_test.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"Testing Button Click 2", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
现在 Class Friends 和 Messages 与 HomeFragment 相同,但需要进行一些小改动 在 class HomeActivity 我写了:
package com.example.sk.voiceapplication.HomePage;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.example.sk.voiceapplication.Friends.Friends;
import com.example.sk.voiceapplication.Messages.Messages;
import com.example.sk.voiceapplication.Profile.MyProfile;
import com.example.sk.voiceapplication.R;
public class HomeActivity extends AppCompatActivity {
private static final String TAG = "HomeActivity";
private SectionsPageAdapter sectionsPagerAdapter;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setupFragments();
}
private void setupFragments() {
sectionsPagerAdapter = new SectionsPageAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0);
tabLayout.getTabAt(1);
tabLayout.getTabAt(2);
}
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new HomeFragment());
adapter.addFragment(new Messages());
adapter.addFragment(new Friends());
viewPager.setAdapter(adapter);
}
}
在 HomeActivity 中,当我创建 Fragments 对象并使用 Line
将它们添加到 SectionsPageAdapter 时adapter.addFragment(new HomeFragment());
显示错误:
SectionsPageAdapter 中的addFragment(android.support.v4.app.Fragment) 无法应用于 (com.example.sk.voiceapplication.HomePage.HomeFragment)
与
类似adapter.addFragment(新消息());
SectionsPageAdapter 中的addFragment(android.support.v4.app.Fragment) 无法应用于 (com.example.sk.voiceapplication.Messages.Messages)
adapter.addFragment(新朋友());
SectionsPageAdapter 中的addFragment(android.support.v4.app.Fragment) 无法应用于 (com.example.sk.voiceapplication.Friends.Friends)
我做错了什么? 解决方法是什么?
您缺少导入 import android.support.v4.app.Fragment;
。您必须使用 V4 片段而不是 android.app.Fragment
.
替换-:
import android.app.Fragment;
收件人-:
import android.support.v4.app.Fragment;
在你所有的片段中。
将此更改为-:
public class HomeFragment extends Fragment
{
}
至-:
public class HomeFragment extends android.support.v4.app.Fragment
{
}
在你所有的三个片段中