一项任务: 当您单击RecyclerView元素时,该项目将被删除。
MainActivity.java
public class MainActivity extends AppCompatActivity implements MultipleAdapter.OnItemClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
// SOME CODE
}
}
@Override
public void onItemClick(int position) {
// Here we can get the item's position in RecyclerView
Toast.makeText(this, "Position: " + position, Toast.LENGTH_SHORT).show();
}
}
MultipleAdapter.java
public class MultipleAdapter extends RecyclerView.Adapter{
private List<RowType> dataSet;
OnItemClickListener mListener;
public MultipleAdapter(List<RowType> dataSet) {
this.dataSet = dataSet;
}
@Override
public int getItemViewType(int position) {
// SOME CODE
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// SOME CODE
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof CountryHolder){
((CountryHolder) holder).mName
.setText(((CountryBase) dataSet.get(position)).getName());
}
}
@Override
public int getItemCount() {
return dataSet.size();
}
public void setListener(OnItemClickListener listener) {
mListener = listener;
}
public static class CountryHolder extends RecyclerView.ViewHolder{
public TextView mName;
public TextView mPopulation;
public CountryHolder(View itemView) {
super(itemView);
mName = itemView.findViewById(R.id.tv_name);
}
public void setListener(final OnItemClickListener mListener) {
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onItemClick(getLayoutPosition());
}
});
}
}
public static class FlagHolder extends RecyclerView.ViewHolder{
// SOME CODE (COPY CountryHolder)
}
public interface OnItemClickListener{
void onItemClick(int position);
}
}
RecyclerFragment.java
public class RecyclerFragment extends Fragment{
Toolbar toolbar;
RecyclerView recyclerView;
MultipleAdapter adapter;
List<RowType> items = new ArrayList<>();
CountryGenerator cG = new CountryGenerator();
MultipleAdapter.OnItemClickListener mListener;
public static RecyclerFragment newInstance() {
return new RecyclerFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_recycler, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
recyclerView = view.findViewById(R.id.recycler);
items.add(new CountryBase(cG.generatorCountryName(), cG.generatorCountryPopulation()));
items.add(new FlagBase("MultiFLAG"));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new MultipleAdapter(items);
recyclerView.setAdapter(adapter);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
adapter.setListener(mListener);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// SOME CODE
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// SOME CODE
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if(context instanceof MultipleAdapter.OnItemClickListener){
mListener = (MultipleAdapter.OnItemClickListener) context;
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
}
收到MainActivity.java
中的职位后,我在RecyclerFragment.java
removeItem(int position);
中创建了方法,但是当我在MainActivity.java
中应用此方法时,该应用程序被删除。
我能以某种简单的方式处理单击元素吗?如果您能帮助我理解此代码中的错误,将不胜感激。