如何将片段添加到列表

时间:2018-01-09 12:32:22

标签: android android-fragments fragment fragmentpageradapter

我创建了以下包:

主页 - > 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 HomeFragment

    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;
    }
}

现在,类朋友和消息与HomeFragment相同,但需要进行一些必要的微小更改 在课程 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中创建片段对象并将其添加到带有行的SectionsPageAdapter

adapter.addFragment(new HomeFragment());

显示错误:

SectionsPageAdapter中的

addFragment(android.support.v4.app.Fragment)无法应用于(com.example.sk.voiceapplication.HomePage.HomeFragment)

类似

adapter.addFragment(new Messages());

SectionsPageAdapter中的

addFragment(android.support.v4.app.Fragment)无法应用于(com.example.sk.voiceapplication.Messages.Messages)

adapter.addFragment(new Friends());

SectionsPageAdapter中的

addFragment(android.support.v4.app.Fragment)无法应用于(com.example.sk.voiceapplication.Friends.Friends)

我做错了什么? 解决方案是什么?

2 个答案:

答案 0 :(得分:0)

您缺少导入Syntax error, unrecognized expression: mm/dd/yyyy。您必须使用V4片段而不是import android.support.v4.app.Fragment;

替换 - :

android.app.Fragment

要 - :

所有碎片中都有

import android.app.Fragment;

答案 1 :(得分:0)

将此更改为 - :

public class HomeFragment extends Fragment

{

}

要 - :

public class HomeFragment extends android.support.v4.app.Fragment

{


}

所有三个条款