Viewpager错误的OnItemClick位置

时间:2017-02-26 20:20:12

标签: android firebase android-viewpager firebase-realtime-database

我的应用程序有一个托管viewPager的片段(每次添加一个片段)所有视图都链接到同一个布局。

每个片段都有一个固定按钮+ recylerview,用于从firebase适配器检索其数据。

当我点击按钮时,它会显示正确的viewPager位置。但是如果我点击listview项目(viewHolder),它会在viewPager中显示下一个预加载的位置。

这里是viewpager和根片段的分页器适配器

Explore.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_explore, container, false);
    array = controller.SELECTED_CATEGORIES;
    viewPager = (ViewPager) root.findViewById(R.id.viewPager);
    tabLayout = (TabLayout) root.findViewById(R.id.categories_tablayout);
    tabLayout.setupWithViewPager(viewPager);
    mAdapter = new MyAdapter(getFragmentManager());
    viewPager.setAdapter(mAdapter);
    return root;
}

public static class MyAdapter extends FragmentStatePagerAdapter {

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

    @Override
    public int getCount() {
        return controller.SELECTED_CATEGORIES.length;
    }

    @Override
    public Fragment getItem(int position) {
        return CategoriesFragment.newInstance(position);
    }
    @Override
    public CharSequence getPageTitle(final int position) {
        return controller.SELECTED_CATEGORIES[position];
    }
}

CategoriesFragment.java

static CategoriesFragment newInstance(int num) {
    CategoriesFragment f = new CategoriesFragment();
    Bundle args = new Bundle();
    args.putInt("num", num);
    f.setArguments(args);

    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_categories, container, false);
    initialize();
    listCategoriesFeed = (RecyclerView) rootView.findViewById(R.id.listCategoriesFeeds);
    categoriesFeedAdapter = new CategoriesFeedAdapter(CategoriesFeedItem.class, R.layout.categoriesfeed_list_item, CategoriesFeedHolder.class, myref);
    llm = new LinearLayoutManager(rootView.getContext());
    llm.setOrientation(LinearLayoutManager.VERTICAL);

    listCategoriesFeed.setLayoutManager(llm);
    listCategoriesFeed.setAdapter(categoriesFeedAdapter);

    swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout);
    swipeRefreshLayout.setOnRefreshListener(this);

    //Holder itemClickListener
    CategoriesFeedHolder.registerOnItemClickListener(this);

    //TextView onclick listener
    View tv = rootView.findViewById(R.id.text);
    ((TextView)tv).setText("Fragment #" + mNum);
    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e("position",mNum + "");
        }
    });
    return rootView;
}

@Override
public void onItemClick(View view, int position) {
super.onItemClick(view, position);
    switch (view.getId()) {
        case R.id.firstId:  
            Log.e("position",mNum+ "");
            break;
        case R.id.secondId:
            secondAction....
            .....
            .....
            .....
            break;
    }
}


private void initialize() {
    controller.showProgressDialog();
    list = controller.getIniCategoriesDetailsList();
    list.clear();
    controller.categoriesDetailsList.put(controller.SELECTED_CATEGORIES[mNum],list);
    reference = FirebaseDatabase.getInstance().getReference("firebase-path/" + controller.SELECTED_CATEGORIES[mNum]);
    myref = reference.orderByChild("order-by").limitToFirst(i);
    myref.addChildEventListener(this);
}

CategoriesFeedHolder.java

private CategoriesFeedItem model;
private final ImageView mImage;

private static OnItemClickListener onItemClickListener;

public CategoriesFeedHolder(View itemView) {
    super(itemView);
    this.context = itemView.getContext();

    mImage = (ImageView) itemView.findViewById(R.id.m_image);
    mImage.setOnClickListener(this);
}

public static void registerOnItemClickListener(OnItemClickListener onItemClickListener) {
    CategoriesFeedHolder.onItemClickListener = onItemClickListener;
}

public void setModel(CategoriesFeedItem model) {
    this.model = model;
}

@Override
public void onClick(View view) {
    onItemClickListener.onItemClick(view, getAdapterPosition());
}

有谁知道为什么会这样?

1 个答案:

答案 0 :(得分:0)

问题在于viewholder类,其中没有数据绑定。因此,当我点击视图中的任何项目时,它会对当前适配器的数据(预加载的数据)执行操作。

viewholder类应该是这样的:

public ViewHolder(View itemView) {
    super(itemView);
    share = (ImageView) itemView.findViewById(R.id.share);
}

public void bindToPost(Post post, View.OnClickListener onClickListener) {
    share.setOnClickListener(onClickListener);
}