如何使用viewpager从选项卡布局中的recyclerview重新加载片段?

时间:2018-01-08 05:51:30

标签: android android-layout android-fragments android-viewpager android-tablayout

我在StackOverflow上搜索了各种答案,但无法找到附近的内容。我知道如何刷新片段,但我不知道如何刷新片段布局中的实际片段。我有一个活动,我已经实现了tablayout与viewpager。我在tablayout的一个片段中有recyclerview。在该回收站视图中,我有一个重新加载页面的按钮。但问题是我重新加载完整的活动,我看到第一个标签。而recyclerview片段在第二个标签中。那么我如何从recyclerview重新加载片段并且不要让完整的活动刷新。如果我刷新完整活动,则再次在前面屏幕上看到第一个布局,并且用户必须再次滑动到第二个标签,这不是优选的。继承我的第二个标签片段的recylerview适配器代码:

    public class Recieved_Reviews_adapter extends RecyclerView.Adapter<Recieved_Reviews_adapter.ViewHolder>
{

Context context;
ArrayList<reviews_model> arraylist_reviews_models;



        public Recieved_Reviews_adapter(Context context, ArrayList<reviews_model> reviews_models) {
            this.context=context;
            this.arraylist_reviews_models = reviews_models;




        }

        @Override
        public Recieved_Reviews_adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview__recieved_reviews, viewGroup, false);
            return new ViewHolder(view);


        }

        @Override
        public void onBindViewHolder(final Recieved_Reviews_adapter.ViewHolder viewHolder, int i) {


            viewHolder.imageView.setImageResource(arraylist_reviews_models.get(i).getReview_images());
            viewHolder.reviewers_name.setText(" "+ arraylist_reviews_models.get(i).getReviewer_name());
            viewHolder.reviews.setText("\" "+ arraylist_reviews_models.get(i).getReviews()+"\"");
            viewHolder.reviews_ratingbar.setRating(arraylist_reviews_models.get(i).getReviews_rating());

    viewHolder.img_btn_reply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            viewHolder.img_btn_reply.setVisibility(View.GONE);
            EditText et_reply= new EditText(context);
            et_reply.setHint("Reply");
            Button btn_sumbit= new Button(context);
            btn_sumbit.setText("Submit");
            btn_sumbit.setGravity(Gravity.CENTER);
            btn_sumbit.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
          btn_sumbit.setTextColor(context.getResources().getColor(R.color.TextColorWhite));
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(120, 40);
            params.setMargins(20, 20, 20, 20);
            params.gravity=Gravity.CENTER;
            btn_sumbit.setLayoutParams(params);

          et_reply.setGravity(Gravity.CENTER);
            viewHolder.linearLayout_received_reviews.addView(et_reply,viewHolder.linearLayout_received_reviews.getChildCount());
            viewHolder.linearLayout_received_reviews.addView(btn_sumbit);

            btn_sumbit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


                           //Refreshing base activity rather 
                            ((Reviews)context).finish();
                            context.startActivity(((Reviews)context).getIntent());

                        });
        }

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

        }



        public class ViewHolder extends RecyclerView.ViewHolder {
    CircleImageView imageView;
    RatingBar reviews_ratingbar;
    TextView reviewers_name;
    TextView reviews;
    LinearLayout linearLayout_received_reviews;
    ImageButton img_btn_reply;
    Button submit;
            public ViewHolder(View view) {
                super(view);
                imageView=(CircleImageView) view.findViewById(R.id.img_review);
                imageView.setBorderColor(context.getResources().getColor(R.color.darkgrey));
                reviews_ratingbar=(RatingBar)view.findViewById(R.id.ratingbar_reviews);
                reviewers_name=(TextView)view.findViewById(R.id.tv_reviewer_name);
                reviews=(TextView) view.findViewById(R.id.tv_the_reviews);
    img_btn_reply =(ImageButton)view.findViewById(R.id.img_btn_received_review_reply);
            linearLayout_received_reviews=(LinearLayout)view.findViewById(R.id.linear_layout_recieved_reviews);


            }

        }



    }

继承我的活动代码:

public class Reviews extends AppCompatActivity {


    ViewPager simpleViewPager;
    TabLayout tabLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reviews);

        init();
        listener();

    }



    private void init() {
        //this method initializes all the views

        simpleViewPager = (ViewPager) findViewById(R.id.simpleViewPager);
        tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);
        TabLayout.Tab firstTab = tabLayout.newTab();
        firstTab.setText("Given");
        TabLayout.Tab secondTab = tabLayout.newTab();
        secondTab.setText("Recieved");
        tabLayout.addTab(firstTab);
        tabLayout.addTab(secondTab);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
        simpleViewPager.setAdapter(adapter);
        simpleViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));


    }











    private void listener() {
        //this method sets onclick on tab

        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(simpleViewPager));

    }

继续我的viewpapt / tablayout的页面扩展:

public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                Given_reviews tab1 = new Given_reviews();
                return tab1;
            case 1:
                Received_reviews tab2 = new Received_reviews();
                return tab2;

            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }
}public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                Given_reviews tab1 = new Given_reviews();
                return tab1;
            case 1:
                Received_reviews tab2 = new Received_reviews();
                return tab2;

            default:
                return null;
        }
    }

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

2 个答案:

答案 0 :(得分:0)

在您的情况下,您需要做的就是响应按钮点击Recyclerview项目进行API调用。收到服务器的回复后更新,然后致电adapter.notifyItemChanged(itemPositionInAdapter)

您不需要重新创建活动/片段。

答案 1 :(得分:0)

不要把这个作为答案,而是对你的问题提出一个暗示性评论(因为我还不能评论)。如果我理解正确您希望在单击按钮时刷新布局,为什么不使用滑动刷新布局并覆盖onRefresh方法,在此方法中,您可以执行API调用和notifyDataSetChanged()。