TabLayout中的RecyclerView适配器冲突

时间:2016-12-02 19:04:26

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

我的Activity中有一个带有4个标签的TabLayout,我为每个标签制作了一个片段。

以下是我的活动代码:

public class MyActivity extends AppCompatActivity {

private static final String TAG = "RecyclerView";
private Toolbar toolbar;
private static ViewPager viewPager;
private static TabLayout tabLayout;

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

    /* Allow activity to show indeterminate progressbar */
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    setContentView(R.layout.student_regulations_list);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewPager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setOnTabSelectedListener(onTabSelectedListener(viewPager));
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new Fragment1(), getString(R.string.titleA));
    adapter.addFragment(new Fragment2(), getString(R.string.titleB));
    adapter.addFragment(new Fragment3(), getString(R.string.titleC));
    adapter.addFragment(new Fragment4(), getString(R.string.titleD));
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(3);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}

private TabLayout.OnTabSelectedListener onTabSelectedListener(final ViewPager viewPager) {
    return new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    };
}

//View Pager fragments setting adapter class
class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();//fragment arraylist
    private final List<String> mFragmentTitleList = new ArrayList<>();//title arraylist

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    //adding fragments and title method
    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

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

我在每个片段中都有一个RecyclerView,我使用AsyncTask从Web服务获取数据,以显示在RecyclerView中。 RecyclerView中的每个项目都有一个ID,我想当我在选项卡之间切换并点击RecyclerView中的项目时,Toast会显示它的ID。

下面是片段代码:

public class Fragment1 extends Fragment implements ClickListener {

private static final String TAG = "RecyclerView";
private List<FeedItem> feedItemList;
private RecyclerView mRecyclerView;
private FeedsRecyclerAdapter mAdapter;
final String url = "a valid url";
private View rootView;
private ProgressBar progressBar;

public StuRegGeneralListFragment() {
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_student_regulations_list, container, false);
    return rootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    progressBar = (ProgressBar) rootView.findViewById(R.id.progress_bar);

    /* Initialize recyclerview */
    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    /*Downloading data from below url*/
    new AsyncHttpTask().execute(url);
}

@Override
public void itemClicked(View view, int position) {
    FeedItem item = feedItemList.get(position);

    Intent intent = new Intent(getActivity(), JsonRequestFeedActivity.class);
    intent.putExtra("url", Const.URL_JSON_OBJECT_REGS + item.getID());
    intent.putExtra("pid", item.getID());
    //startActivity(intent);
    Toast.makeText(getActivity(), "Item ID: " + item.getID(), Toast.LENGTH_SHORT).show();
}

public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {

    @Override
    protected void onPreExecute() {
        progressBar.setVisibility(View.VISIBLE);
        feedItemList = new ArrayList<FeedItem>();
    }

    @Override
    protected Integer doInBackground(String... params) {
        InputStream inputStream = null;
        Integer result = 0;
        HttpURLConnection urlConnection = null;

        try {
            /* forming th java.net.URL object */
            URL url = new URL(params[0]);

            urlConnection = (HttpURLConnection) url.openConnection();

            /* for Get request */
            urlConnection.setRequestMethod("GET");

            int statusCode = urlConnection.getResponseCode();

            /* 200 represents HTTP OK */
            if (statusCode == 200) {

                BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = r.readLine()) != null) {
                    response.append(line);
                }

                parseResult(response.toString());
                result = 1; // Successful
            } else {
                result = 0; //"Failed to fetch data!";
            }

        } catch (Exception e) {
            Log.d(TAG, e.getLocalizedMessage());
        }

        return result; //"Failed to fetch data!";
    }

    @Override
    protected void onPostExecute(Integer result) {
        progressBar.setVisibility(View.GONE);

        /* Download complete. Lets update UI */
        if (result == 1) {
            updateRecyclerView();
        } else {
            Log.e(TAG, "Failed to fetch data!");
        }
    }
}

private void parseResult(String result) {
    try {
        JSONObject response = new JSONObject(result);
        JSONArray posts = response.optJSONArray("posts");

        /*Initialize array if null*/
        if (null == feedItemList) {
            feedItemList = new ArrayList<FeedItem>();
        }

        for (int i = 0; i < posts.length(); i++) {
            JSONObject post = posts.optJSONObject(i);

            FeedItem item = new FeedItem();
            item.setID(post.optString("id"));
            item.setDate(post.optString("date"));
            item.setTitle(post.optString("title"));
            item.setThumbnail(post.optString("thumbnail"));
            feedItemList.add(item);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

public void updateRecyclerView() {
    mAdapter = new FeedsRecyclerAdapter(getActivity(), feedItemList);
    mRecyclerView.setAdapter(mAdapter);

    mAdapter.setClickListener(this);
}

}

假设每个片段在RecyclerView中有5个项目(在片段1中从ID 1到5,从片段2中的ID 6到10,......)我希望当我进入我的Activity并单击fragment1中的项目时(例如第一项),ID = 1显示在Toast中,但显示ID = 16!

实际上是因为viewPager.setOffscreenPageLimit(3);在我的Activity中,创建了fragment1和fagment2以及fragment3和fragment4,最后,mAdapter包含项目ID 16-20,尽管我在fragment1中。 我希望在viewPager.setOffscreenPageLimit(int)创建指定的片段之后;每个片段都有它自己的适配器,但它没有发生。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我发现了我的错误!

我的活动和片段代码是正确的,我的错误在于RecyclerView的ClickListener。它的实施并不适合我的情况。

我在其中使用过静态clickListener,这就是重点!