android只在recyclelerview里面显示第一个viewpager

时间:2017-06-07 13:59:49

标签: android-recyclerview fragmentpageradapter viewpage

我在recyclerview中有一个带有FragmentStatePagerAdapter的viewpager 它只显示了recyclerview的第一行 请帮帮我

viewpager适配器:     公共类CustomPagerAdapter扩展FragmentStatePagerAdapter {         private static int NUM_ITEMS = 3;

    public CustomPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        Log.e("fragmentManager",fragmentManager.toString());
    }

    // Returns total number of pages
    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    // Returns the fragment to display for that page
    @Override
    public Fragment getItem(int position) {
        Fragment currentFragment;
        switch (position) {
            case 0:
                currentFragment=new FirstFragment();
                break;
            case 1:
                currentFragment=new SecondFragment();
                break;
            case 2:
                currentFragment=new ThirdFragment();
                break;
            default:
                currentFragment=new FirstFragment();
                break;
        }

        return currentFragment;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}

recyclerview adapter:

public class JobAdapter extends RecyclerView.Adapter<JobAdapter.JobHolder> {
    private Context context;
    private List<String> mJobList = new ArrayList<>();

    public JobAdapter(List<String> mJobList,Context context) {
        this.mJobList = mJobList;
        this.context=context;
    }

    @Override
    public JobHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.find_job_line_content, parent, false);
        return new JobHolder(view);
    }

    @Override
    public void onBindViewHolder(final JobHolder holder, int position) {
        configurePagerHolder(holder, position);
    }

    private void configurePagerHolder(JobHolder holder, int position) {
        //Job job= mJobList.get(position);
        CustomPagerAdapter adapter = new CustomPagerAdapter(((MainActivity)context).getSupportFragmentManager());
        holder.viewPager.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        holder.viewPager.setCurrentItem(1);
        Log.e("configurePagerHolder",position+"");

    }

    public int getItemCount() {``
        return mJobList.size();
    }


    public class JobHolder extends RecyclerView.ViewHolder{
        ViewPager viewPager;
        public JobHolder(View itemView) {
            super(itemView);
            viewPager=(ViewPager)itemView.findViewById(R.id.slidesPager);
        }

    }

主:

public class MainActivity extends AppCompatActivity {
    RecyclerView mRecyclerView;
    JobAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView)findViewById(R.id.find_jobs_recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL,false));
        mRecyclerView.setHasFixedSize(true);
        List<String> list=new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        adapter = new JobAdapter(list,this);
        mRecyclerView.setAdapter(adapter);

    }
}

1 个答案:

答案 0 :(得分:0)

答案很晚,但可能对某些人有帮助...

尝试使用ViewPager2(ViewPager的改进版本):https://developer.android.com/training/animation/screen-slide-2

就我而言,它解决了问题。