适配器OnClick RecyclerView的旧位置

时间:2015-09-12 21:54:43

标签: android adapter android-recyclerview

我正在尝试在应用过滤器时更改recycleview中的数据。一切都按原样运行,但是当我点击新适配器的项目时,我会得到旧适配器的项目。这个例子描述了会发生什么。

Adapter items : x , y , z
After filter adapter items : x , z
When i click filtered item z it opens y

DataFilters只是一个过滤或不是数组的返回,homeFragment.companyFilters是从一个类内部调用的,它再次管理微调器选择,不再显示。我认为这就是全部。

HomeFragment

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.home_layout, null);
    mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.home_refresh);
    fab = (FloatingActionButton) rootView.findViewById(R.id.fabhome);
    sheet = (View) rootView.findViewById(R.id.sheethome);
    overlay = (View) rootView.findViewById(R.id.overlayhome);
    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.home_recycler_view);
    context = container.getContext();

    mRecyclerView.setHasFixedSize(false);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mHomeAdapter = new RecyclerViewHomeAdapter(dataFilters.getAllHomeDataSet());

    PreCachingLayoutManager layoutManager = new PreCachingLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    layoutManager.setSmoothScrollbarEnabled(true);

    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setAdapter(mHomeAdapter);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mHomeAdapter = new RecyclerViewHomeAdapter(dataFilters.getAllHomeDataSet());
            mRecyclerView.setAdapter(mHomeAdapter);
            mSwipeRefreshLayout.setRefreshing(false);
        }
    });

    fab.setOnClickListener(this);
    overlay.setOnClickListener(this);
    sheet.setOnClickListener(this);

    addItemsOnCompanySpinner();
    spinnerCompany.setOnItemSelectedListener(new HomeSpinnerHandler(this));

    return rootView;
}

@Override
public void onResume() {
    super.onResume();
    ((RecyclerViewHomeAdapter) mHomeAdapter).setOnItemClickListener(new RecyclerViewHomeAdapter.MyClickListener() {
        @Override
        public void onItemClick(int position, View v) {

            String ObjectType = dataFilters.getAllHomeDataSet().get(position).homeObjectType;
            if (ObjectType.contains("Product")) {
                ((MainActivity) getActivity()).onProductItemClick(dataFilters.getAllHomeDataSet().get(position).homeObjectID);
            } else {
                ((MainActivity) getActivity()).onPostItemClick(dataFilters.getAllHomeDataSet().get(position).homeObjectID);
            }
        }
    });
}

@Override
public void onClick(View v) {
    if (fab.getVisibility() == View.VISIBLE) {
        FabTransformation.with(fab).setOverlay(overlay).transformTo(sheet);
    }
    if (fab.getVisibility() != View.VISIBLE) {
        FabTransformation.with(fab).setOverlay(overlay).transformFrom(sheet);
    }
}


public void addItemsOnCompanySpinner() {

    spinnerCompany = (MaterialSpinner) rootView.findViewById(R.id.spinnercompany);
    List<String> list = new ArrayList<String>();
    for (companyListCounter = 0;companyListCounter<companies.size();companyListCounter++){
        list.add(companies.get(companyListCounter).getCompanyName());
    }

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
            android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerCompany.setAdapter(dataAdapter);
    spinnerCompany.setHint("Επιλέξτε Εταιρεία");

}

public void companyFilter(String companyFilter) {
    FabTransformation.with(fab).setOverlay(overlay).transformFrom(sheet);
    addItemsOnCompanySpinner();
    mHomeAdapter = new RecyclerViewHomeAdapter(dataFilters.getHomeCompanyDataSet(companyFilter));
    mRecyclerView.setAdapter(mHomeAdapter);
}

public void  updateView(){
    mHomeAdapter = new RecyclerViewHomeAdapter(dataFilters.getAllHomeDataSet());
    mRecyclerView.setAdapter(mHomeAdapter);
}

RecyclerViewHomeAdapter

public class RecyclerViewHomeAdapter extends RecyclerView.Adapter<RecyclerViewHomeAdapter.HomeDataObjectHolder> {

private ArrayList<HomeObject> mDataset;
private static MyClickListener myClickListener;



public static class HomeDataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView label;
    TextView company;
    TextView description;
    ImageView imageDrawable;
    CardView cardview;

    public HomeDataObjectHolder(View itemView) {
        super(itemView);
        label = (TextView) itemView.findViewById(R.id.homelabel);
        company = (TextView) itemView.findViewById(R.id.homecompany);
        description = (TextView) itemView.findViewById(R.id.homedescr);
        imageDrawable = (ImageView) itemView.findViewById(R.id.homeimage);
        cardview = (CardView) itemView.findViewById(R.id.cardview);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        myClickListener.onItemClick(getAdapterPosition(), v);
    }
}

public void setOnItemClickListener(MyClickListener myClickListener) {
    this.myClickListener = myClickListener;
}

public RecyclerViewHomeAdapter(ArrayList<HomeObject> myDataset) {
    mDataset = myDataset;
}

@Override
public HomeDataObjectHolder onCreateViewHolder(ViewGroup parent,
                                           int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.recyclerview_home_item, parent, false);

    HomeDataObjectHolder dataObjectHolder = new HomeDataObjectHolder(view);
    return dataObjectHolder;
}

@Override
public void onBindViewHolder(HomeDataObjectHolder holder, int position) {
    holder.label.setText(mDataset.get(position).getLabel());
    holder.company.setText(mDataset.get(position).getCompany());
    holder.description.setText(mDataset.get(position).getTextDescr());

    if (Arrays.asList(postTypeRef).contains(mDataset.get(position).getLabel().replace("Είδος Ενημέρωσης : \r\n",""))){
        Picasso.with(MyApp.applicationContext).load(mDataset.get(position)
                .imageIDFetch()).into(holder.imageDrawable);
    }
    else{
        Picasso.with(MyApp.applicationContext).load(MyApp.imageURL+mDataset.get(position)
                .getImageCached()).into(holder.imageDrawable);
    }

}


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

public interface MyClickListener {
    public void onItemClick(int position, View v);
}}

在MainActivity中我有两个函数,所以我可以从片段打开另一个片段。

public void onProductItemClick(int id) {
    Fragment productFragment = new ProductItemFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("id", id);
    productFragment.setArguments(bundle);
    android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.containerView, productFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

public void onPostItemClick(int id) {
    Fragment postFragment = new NewsItemFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("id", id);
    postFragment.setArguments(bundle);
    android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.containerView, postFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

0 个答案:

没有答案