0

llevo atascado con un codigo, al momento funciona bien al manejarlo con los dedos en el telefono, pero lo estoy haciendo para compatibilidad para tv, y por ende que sea manejable con los botones del control remoto, pero no consigo que al presional el boton seleccionar ejecute ninguna accion dentro del Recyclerview, espero me puedan ayudar, gracias

RecyclerTouchListener.class

public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

private GestureDetector gestureDetector;
private ClickListener clickListener;

public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
    this.clickListener = clickListener;
    gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null) {
                clickListener.onLongClick(child, recyclerView.getChildPosition(child));
            }
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

    View child = rv.findChildViewUnder(e.getX(), e.getY());
    if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
        clickListener.onClick(child, rv.getChildPosition(child));
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {

}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}

public interface ClickListener {
    void onClick(View view, int position);

    void onLongClick(View view, int position);
}

}

CategoryFragment.class

public class CategoryFragment extends Fragment {

ArrayList<ItemCategory> mListItem;
public RecyclerView recyclerView;
CategoryAdapter adapter;
private ProgressBar progressBar;
private LinearLayout lyt_not_found;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.row_recyclerview, container, false);
    mListItem = new ArrayList<>();
    TextView txt_refencia = rootView.findViewById(R.id.tv_reference_ChanCat);
    int grillacant = 2;
    if(txt_refencia.isEnabled()){
        grillacant = 5;
    }
    lyt_not_found = rootView.findViewById(R.id.lyt_not_found);
    progressBar = rootView.findViewById(R.id.progressBar);
    recyclerView = rootView.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), grillacant));
    ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(requireActivity(), R.dimen.item_offset);
    recyclerView.addItemDecoration(itemDecoration);
    if (NetworkUtils.isConnected(getActivity())) {
        getCategory();
    } else {
        Toast.makeText(getActivity(), getString(R.string.conne_msg1), Toast.LENGTH_SHORT).show();
    }

    recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {
            String categoryName = mListItem.get(position).getCategoryName();
            Bundle bundle = new Bundle();
            bundle.putString("name", categoryName);
            bundle.putString("Id", mListItem.get(position).getCategoryId());

            FragmentManager fm = getFragmentManager();
            CategoryItemFragment categoryItemFragment = new CategoryItemFragment();
            categoryItemFragment.setArguments(bundle);
            assert fm != null;
            FragmentTransaction ft = fm.beginTransaction();
            ft.hide(CategoryFragment.this);
            ft.add(R.id.Container, categoryItemFragment, categoryName);
            ft.addToBackStack(categoryName);
            ft.commit();
            ((MainActivity) requireActivity()).setToolbarTitle(categoryName);
        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));
    return rootView;
}

private void getCategory() {
    AsyncHttpClient client = new AsyncHttpClient();
    client.get(Constant.CATEGORY_URL, new AsyncHttpResponseHandler() {

        @Override
        public void onStart() {
            super.onStart();
            showProgress(true);
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            showProgress(false);
            String result = new String(responseBody);
            try {
                JSONObject mainJson = new JSONObject(result);
                JSONArray jsonArray = mainJson.getJSONArray(Constant.ARRAY_NAME);
                JSONObject objJson;
                for (int i = 0; i < jsonArray.length(); i++) {
                    objJson = jsonArray.getJSONObject(i);
                    ItemCategory objItem = new ItemCategory();
                    objItem.setCategoryId(objJson.getString(Constant.CATEGORY_CID));
                    objItem.setCategoryName(objJson.getString(Constant.CATEGORY_NAME));
                    objItem.setCategoryImage(objJson.getString(Constant.CATEGORY_IMAGE));
                    mListItem.add(objItem);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            displayData();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            showProgress(false);
            lyt_not_found.setVisibility(View.VISIBLE);
        }

    });
}

private void displayData() {
    adapter = new CategoryAdapter(getActivity(), mListItem, R.layout.row_category_item);
    recyclerView.setAdapter(adapter);
    if (adapter.getItemCount() == 0) {
        lyt_not_found.setVisibility(View.VISIBLE);
    } else {
        lyt_not_found.setVisibility(View.GONE);
    }
    //#animacion en focus
    if(!getActivity().getResources().getBoolean(R.bool.portrait_only)) {
        recyclerView.requestFocusFromTouch();
    }
}


private void showProgress(boolean show) {
    if (show) {
        progressBar.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);
        lyt_not_found.setVisibility(View.GONE);
    } else {
        progressBar.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);
    }
}

}

La parte que invoca al clic es esta y es la cual quisiera adaptar para que al presionar la tecla de navegacion ejecute el fragmento, recuerdo que funciona bien con los dedos pero con los controles no.

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new RecyclerTouchListener.ClickListener()

1 respuesta 1

0

por si a alguien le sirve, lo resolvi añadiendo el on clic listener en OnBind de esta forma.

public void onBindViewHolder(@NonNull final ItemRowHolder holder, final int position) {
    final ItemCategory singleItem = dataList.get(position);
    holder.text.setText(singleItem.getCategoryName());
    Picasso.get().load(singleItem.getCategoryImage()).placeholder(R.drawable.placeholder).into(holder.image);
    if(!mContext.getResources().getBoolean(R.bool.portrait_only)) {
    holder.lyt_parent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String categoryName = dataList.get(position).getCategoryName();
            Bundle bundle = new Bundle();
            bundle.putString("name", categoryName);
            bundle.putString("Id", dataList.get(position).getCategoryId());
            FragmentManager fm = ((FragmentActivity) v.getContext()).getSupportFragmentManager();
            CategoryItemFragment categoryItemFragment = new CategoryItemFragment();
            categoryItemFragment.setArguments(bundle);
            assert fm != null;
            FragmentTransaction ft = fm.beginTransaction();
            Fragment currentFragment = ((FragmentActivity) v.getContext()).getSupportFragmentManager().findFragmentById(R.id.Container);
            ft.hide(currentFragment);
            ft.add(R.id.Container, categoryItemFragment, categoryName);
            ft.addToBackStack(categoryName);
            ft.commit();
            ((MainActivity) currentFragment.requireActivity()).hideShowBottomView(false);
            ((MainActivity) currentFragment.requireActivity()).navigationItemSelected(2);
            ((MainActivity) currentFragment.requireActivity()).setToolbarTitle(categoryName);
        }
    });
    }
}

ya asi me responde con el control y el touch.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.