在片段里面的Recyclerview在viewpager里面

时间:2017-10-01 19:54:02

标签: java android android-recyclerview android-viewpager fragment

我想在我的3片段viewpager的片段中使用recyclerview。我编写了代码,它运行得很完美,但我不知道为什么它太慢了。当我从一个片段换成另一个片段时,它几乎需要半分钟。不知道我在这里犯了什么错误。

主要活动

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

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

        setContentView(R.layout.activity_main);
        toolbar = (Toolbar)findViewById(R.id.toolBar);
        tabLayout = (TabLayout)findViewById(R.id.tabs);
        viewPager = (ViewPager)findViewById(R.id.viewPager);

        setSupportActionBar(toolbar);
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPagerAdapter.addFragment(new OneFragment(), "One");
        viewPagerAdapter.addFragment(new TwoFragment(), "Two");
        viewPagerAdapter.addFragment(new ThreeFragment(), "Three");

        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);

    }
}

这是我尝试实施recyclerview的片段之一。

片段

public class TwoFragment extends Fragment {

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_two, container, false);

        OurData ourData = new OurData();
        ourData.pic.add(R.drawable.b);
        ourData.title.add("Hello");
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.listRecyclerView);
        ListAdapter listAdapter = new ListAdapter();
        recyclerView.setAdapter((listAdapter));
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        return view;
    }
}

public class ListAdapter extends RecyclerView.Adapter {

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        return new ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ListViewHolder) holder).bindView(position);
    }

    @Override
    public int getItemCount() {
        return OurData.title.size();
    }

    private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView text;
        private ImageView image;

        public ListViewHolder(View itemview) {
            super(itemview);
            text = (TextView) itemview.findViewById(R.id.textF);
            image = (ImageView) itemview.findViewById(R.id.imageF);
            itemview.setOnClickListener(this);
        }

        public void bindView(int position) {
            text.setText(OurData.title.get(position));
            image.setImageResource(OurData.pic.get(position));
        }

        public void onClick(View view) {

        }
    }
}

public class OurData {

    public static ArrayList<String> title = new ArrayList<String>();
    public static ArrayList<Integer> pic = new ArrayList<Integer>();

}

我花了一整天时间思考我做错了什么。请帮帮我!

2 个答案:

答案 0 :(得分:0)

设置适配器recyclerView.setAdapter((listAdapter)); insse onViewCreated而不是onCreateView

答案 1 :(得分:0)

我认为问题是因为你的一个方法。 也许&#39;我们的数据&#39; 你也确定这个:

 @Override
    public int getItemCount() {
        return OurData.title.size();
    }

还尝试使用登录每个代码并查看需要时间的部分,尤其是&Our;&#39;