recyclerView与viewPager一起使用时不显示数据

时间:2018-08-10 17:30:40

标签: android android-recyclerview

我试图在viewPager中使用recyclerView。但是我无法在“ TAB 1”下获得该列表。我的fragment_first.xml中有一个TextView和Imageview。我试图在recyclerView上获取5个文本和5个图像的列表。问题似乎出在 testArray,array_image 第94行,即 FirstFragment中的返回testArray.size()。 java 第75行,即 public int getCount(){             在 MainActivity.java 中返回2;} 。我将不胜感激任何帮助。文件如下:

MainActivity.java

package com.example.viewpag;

import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ViewPager viewPager;
    TabLayout tabLayout;
    FrameLayout frameLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager)findViewById(R.id.view_pager);
        tabLayout = (TabLayout)findViewById(R.id.tabLay);
        setupViewPager(viewPager);
        tabLayout.setupWithViewPager(viewPager);

//        frameLayout=(FrameLayout)findViewById(R.id.frameLayout);
//        FirstFragment fragment = new FirstFragment();
//        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
//        fragmentTransaction.replace(R.id.frameLayout, fragment);
//        fragmentTransaction.commit();
    }

    private void setupViewPager(ViewPager viewPager){
        MyViewPagerAdapter myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager());
        myViewPagerAdapter.addFragment(new FirstFragment(), "Tab 1");
        myViewPagerAdapter.addFragment(new SecondFragment(),"Tab 2");

        viewPager.setAdapter(myViewPagerAdapter);
    }


    public class MyViewPagerAdapter extends FragmentPagerAdapter{
        private final List<Fragment> fragmentList = new ArrayList<>();
        private final List<String> fragmentTitleList = new ArrayList<>();


        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title){
            fragmentList.add(fragment);
            fragmentTitleList.add(title);
        }
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return fragmentTitleList.get(position);
        }

        @Override
        public int getCount() {
            return 2;
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:tabMode="fixed"
        app:tabGravity="fill"
        android:id="@+id/tabLay"/>

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

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id ="@+id/frameLayout"/>

</LinearLayout>

FirstFragment.java

package com.example.viewpag;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class FirstFragment extends Fragment{

//    TextView textView;
//    ImageView imageView;
    RecyclerView recyclerView;
    List<String> testArray;
    ArrayList<Integer> array_image;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_first, container,false);
//        textView = (TextView)view.findViewById(R.id.text_View);
//        imageView = (ImageView)view.findViewById(R.id.image_View);

        recyclerView = (RecyclerView)view.findViewById(R.id.recycler_View);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        MyAdapter myAdapter = new MyAdapter();
        recyclerView.setAdapter(myAdapter);

        testArray = new ArrayList<>();
        testArray.add("One");
        testArray.add("Two");
        testArray.add("Three");
        testArray.add("Four");
        testArray.add("Five");

        array_image = new ArrayList<Integer>();
        array_image.add(android.R.drawable.arrow_up_float);
        array_image.add(android.R.drawable.arrow_down_float);
        array_image.add(android.R.drawable.btn_star);
        array_image.add(android.R.drawable.ic_lock_idle_charging);
        array_image.add(android.R.drawable.ic_lock_idle_low_battery);


        return view;
    }

    public class MyAdapter extends RecyclerView.Adapter<MainViewHolder>{

        @Override
        public int getItemViewType(int position) {
            return super.getItemViewType(position);
        }

        @NonNull
        @Override
        public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_first,parent,false);
            return new MainViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
            holder.textView.setText(testArray.get(position));
            holder.imageView.setImageResource(array_image.get(position));
        }

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

    public class MainViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        ImageView imageView;
        public MainViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = (TextView)itemView.findViewById(R.id.text_View);
            imageView = (ImageView)itemView.findViewById(R.id.image_View);

        }
    }
}

fragment_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <ImageView
        android:id="@+id/image_View"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="abc" />

    <TextView
        android:id="@+id/text_View"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_View"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

此外, SecondFragment.java 文件如下:

package com.example.viewpag;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class SecondFragment extends Fragment{

    TextView textView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_second,container,false);
        textView = (TextView)view.findViewById(R.id.textView2);
        return view;
    }
}

4 个答案:

答案 0 :(得分:1)

首先初始化testArray

List<String> testArray = new ArrayList<String>();

最好将此ArrayList作为参数传递给Adapter类。

MyAdapter myAdapter = new MyAdapter(testArray);

然后在您的MyAdapter类中,创建一个参数化的构造函数

 public class MyAdapter extends RecyclerView.Adapter<MainViewHolder>
 {
    public MyAdapter(List<String> testArray)
    {
        this.testArray = testArray
    }
 }

并通知arrayList中的更改

    testArray = new ArrayList<>();
    testArray.add("One");
    testArray.add("Two");
    testArray.add("Three");
    testArray.add("Four");
    testArray.add("Five");

    array_image = new ArrayList<Integer>();
    array_image.add(android.R.drawable.arrow_up_float);
    array_image.add(android.R.drawable.arrow_down_float);
    array_image.add(android.R.drawable.btn_star);
    array_image.add(android.R.drawable.ic_lock_idle_charging);
    array_image.add(android.R.drawable.ic_lock_idle_low_battery);

  myAdapter.notifyDataSetChanged();

您的适配器类应该是

 public class MyAdapter extends RecyclerView.Adapter<MainViewHolder>{

    List<String> testArray1 = new ArrayList<String>(); 
    ArrayList<Integer> array_image1 = new ArrayList<Integer>();

    // parameterized constructor
    public MyAdapter(List<String> testArray, ArrayList<Integer> array_image)
    {
        this.testArray1 = testArray;
        this.array_image1 = array_image;
    }
    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_first,parent,false);
        return new MainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        holder.textView.setText(testArray1.get(position));
        holder.imageView.setImageResource(array_image1.get(position));
    }

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

public class MainViewHolder extends RecyclerView.ViewHolder{
    TextView textView;
    ImageView imageView;
    public MainViewHolder(@NonNull View itemView) {
        super(itemView);
        textView = (TextView)itemView.findViewById(R.id.text_View);
        imageView = (ImageView)itemView.findViewById(R.id.image_View);

    }
}

答案 1 :(得分:0)

尝试保持 recyclerView.setAdapter(myAdapter);返回之前查看;线并检查它。希望它能工作。

答案 2 :(得分:0)

我不知道您是否已将所有这些内容都写在一个文件中,这是一种不好的编码习惯,但是假设是这样,假设您正在使用MyAdapter而不向其发送任何数据。将列表发送到其构造函数,即

  

MYAdapter(ArrayList <>,ArrayList <>)

创建两个数组列表,然后再将其发送给构造函数,然后使用这些数组列表。返回计数为arrayList.size()

        testArray = new ArrayList<>();
        testArray.add("One");
        testArray.add("Two");
        testArray.add("Three");
        testArray.add("Four");
        testArray.add("Five");

        array_image = new ArrayList<Integer>();
        array_image.add(android.R.drawable.arrow_up_float);
        array_image.add(android.R.drawable.arrow_down_float);
        array_image.add(android.R.drawable.btn_star);
        array_image.add(android.R.drawable.ic_lock_idle_charging);
        array_image.add(android.R.drawable.ic_lock_idle_low_battery);

 MyAdapter myAdapter = new MyAdapter();
        recyclerView.setAdapter(myAdapter);

答案 3 :(得分:0)

问题是您将应该在列表项布局中的视图放在与回收者视图相同的视图中。

ViewHolder应该只为您的列表项添加一种新的布局,而不是为您的回收站视图添加一种新的布局。创建一个名为list_item的新xml,然后在其中放置文本和图像视图。不要忘记使高度为wrap_content。现在,在您的ViewHolder onCreate中膨胀它,而不是膨胀fragment_first布局。

它只显示一个项目,因为片段优先布局设置为match_parent,它占用了一个项目的所有屏幕。现在,它可以正确地充气物品了。