我的英语水平不是最好,但是我希望您能理解,我是开发Android应用程序的新手,目前我有一个回收站视图,其中充满了WebService
,我正在尝试使这些recyclerview项可选,这时我已经尝试过:
有了flexibleAdapter
,我有了Item和Fragment,但它根本不起作用。在第二个中,我尝试了其他片段,它可以监听点击,但是有一个监听器用于启动选择模式,并且该选择模式由活动模式控制,并且此动作模式不起作用o,不知道现在该怎么做
这是我带“ FlexibleAdapter”的代码。 我的“物品”对象
public class TrazaFlex extends AbstractFlexibleItem<TrazaFlex.ViewHolderTrazaFlex> {
public TrazaFlex(){
}
...
...
@Override
public void bindViewHolder(FlexibleAdapter<IFlexible> adapter, ViewHolderTrazaFlex holder, int position, List<Object> payloads) {
holder.mTitle.setText(nombreTraza);
// Title appears disabled if item is disabled
holder.mTitle.setEnabled(isEnabled());
Context context = holder.itemView.getContext();
Drawable drawable = DrawableUtils.getSelectableBackgroundCompat(
Color.WHITE, Color.parseColor("#dddddd"), // Same color of divider
DrawableUtils.getColorControlHighlight(context));
DrawableUtils.setBackgroundCompat(holder.itemView, drawable);
}
public class ViewHolderTrazaFlex extends FlexibleViewHolder {
public TextView mTitle;
public ViewHolderTrazaFlex(View view, FlexibleAdapter adapter) {
super(view, adapter);
mTitle = (TextView) view.findViewById(R.id.id_nombre_traza);
}
}
这是我的片段:
public class TrazaFragment extends Fragment implements ActionMode.Callback, FlexibleAdapter.OnItemClickListener, FlexibleAdapter.OnItemLongClickListener {
ArrayList<Traza> listaTrazas;
RecyclerView recyclerTrazas;
View vista;
private FlexibleAdapter mAdapter;
private ActionMode mActionMode;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getActivity().setTitle("Trazas");
vista = inflater.inflate(R.layout.fragment_traza, container, false);
listaTrazas = new ArrayList<>();
recyclerTrazas = (RecyclerView) vista.findViewById(R.id.recyclerTrazas);
recyclerTrazas.setLayoutManager(new LinearLayoutManager(getContext()));
GetTrazas mAttempTraza = new GetTrazas();
mAttempTraza.execute((Void) null);
return vista;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.selected_menu, menu);
mAdapter.setMode(Mode.MULTI);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mAdapter.setMode(Mode.IDLE);
mActionMode = null;
}
@Override
public boolean onItemClick(View view, int position) {
if (mActionMode != null && position != RecyclerView.NO_POSITION) {
// Mark the position selected
toggleSelection(position);
Toast.makeText(getContext(),"OnItemLongClick ", Toast.LENGTH_LONG).show();
return true;
} else {
Toast.makeText(getContext(),"OnItemLongClick ", Toast.LENGTH_LONG).show();
// Handle the item click listener
// We don't need to activate anything
return false;
}
}
@Override
public void onItemLongClick(int position) {
if (mActionMode == null) {
getActivity().startActionMode(this);
}
Toast.makeText(getContext(),"OnItemLongClick ", Toast.LENGTH_LONG).show();
toggleSelection(position);
}
/**
* Toggle the selection state of an item.
* If the item was the last one in the selection and is unselected, the ActionMode
* is stopped.
*/
private void toggleSelection(int position) {
// Mark the position selected
mAdapter.toggleSelection(position);
int count = mAdapter.getSelectedItemCount();
if (count == 0) {
mActionMode.finish();
} else {
setContextTitle(count);
}
}
private void setContextTitle(int count) {
mActionMode.setTitle(String.valueOf(count) + " " + (count == 1 ?
"Action Selected One" :
"Action Selected Many"+count));
}
public class GetTrazas extends AsyncTask<Void, Void, Boolean> {
//connects to service and get json
return null;
}
@Override
protected void onPostExecute(final Boolean success) {
recyclerTrazas = (RecyclerView) vista.findViewById(R.id.recyclerTrazas);
//// recyclerTrazas.setLayoutManager(new LinearLayoutManager(getContext()));
List<IFlexible> listaTrazasFlex = getDatabaseList();
FlexibleAdapter<IFlexible> adapterFlexTraza = new FlexibleAdapter<>(listaTrazasFlex);
recyclerTrazas.setAdapter(adapterFlexTraza);
}
public List<IFlexible> getDatabaseList() {
//connects to json and returns a list
}
}
catch (JSONException e) {
}
return list;
}
}
}