Получить позицию выбранного элемента в RecyclerView из Activity
GoodsAdapter.java
public class GoodsAdapter extends MultiChoiceAdapter<GoodsAdapter.ViewHolder> {
private final String TAG = "TDT-GoodsAdapter";
private final LayoutInflater mInflater;
private final List<GoodModel> mGoodsList;
private int mRowSelected = -1;
// private final getSelectedItem mSelectedItem;
// public interface getSelectedItem {
// void onItemClick(GoodsAdapter holder, int position);
// }
public GoodsAdapter(List<GoodModel> mGoodsList, Context mContext) {
this.mInflater = LayoutInflater.from(mContext);
this.mGoodsList = mGoodsList;
// this.mSelectedItem = selectedItem;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
GoodModel goodItem = mGoodsList.get(position);
Log.d(TAG, "onBindViewHolder: " + goodItem.getName());
holder.goodName.setText(goodItem.getName());
holder.goodName.setOnClickListener(view -> {
mRowSelected = position;
// mSelectedItem.onItemClick(this, position);
notifyDataSetChanged();
});
if (mRowSelected == position) {
holder.goodName.setTextColor(Color.RED);
} else {
holder.goodName.setTextColor(Color.BLACK);
}
}
@NonNull
@Override
public GoodsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.good_item, parent, false);
return new ViewHolder(view);
}
@Override
public int getItemCount() {
return mGoodsList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
final TextView goodName;
ViewHolder(View view) {
super(view);
goodName = view.findViewById(R.id.good_name);
}
}
}
Goods.java
public class Goods extends AsyncTask<String, Void, List<GoodModel>> {
private final String TAG = "TDT-Goods";
@SuppressLint("StaticFieldLeak")
private final Context mContext;
@SuppressLint("StaticFieldLeak")
private final RecyclerView mRecyclerView;
private final ACProgressFlower mDialog;
public Goods(RecyclerView recyclerView, Activity activity) {
mRecyclerView = recyclerView;
mContext = activity;
mDialog = new ACProgressFlower.Builder(activity)
.direction(ACProgressConstant.DIRECT_CLOCKWISE)
.themeColor(Color.WHITE)
.text("Работаем")
.fadeColor(Color.DKGRAY).textSize(25).build();
}
@Override
protected void onPreExecute() {
mDialog.show();
}
@Override
protected List<GoodModel> doInBackground(String... params) {
TDTApi tdtService = TDTClient.getTDTClient().create(TDTApi.class);
Call<GoodsModel> call = tdtService.getGoods(params[0], params[1]);
Log.d(TAG, "doInBackground: params[0]: " + params[0] + ", params[1]: " + params[1]);
try {
Response<GoodsModel> rp = call.execute();
return rp.body().getGoods();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<GoodModel> result) {
super.onPostExecute(result);
if (mDialog.isShowing()) {
mDialog.dismiss();
}
if (result != null) {
Log.d(TAG, "doInBackground: params[0]: " + result.toString());
GoodsAdapter goodsAdapter = new GoodsAdapter(result, mContext);
mRecyclerView.setAdapter(goodsAdapter);
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private final String TAG = MainActivity.class.getSimpleName();
private RecyclerView mGoodsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner foldersSpinner = findViewById(R.id.spinner);
mGoodsList = findViewById(R.id.goods_list);
Button findGood = findViewById(R.id.find_good);
Button print = findViewById(R.id.print);
EditText goodSearchText = findViewById(R.id.good_search_text);
// Поиск складов
new Folders(foldersSpinner, this).execute();
// Поиск товара
findGood.setOnClickListener(view -> {
String searchGoodText = goodSearchText.getText().toString();
if (foldersSpinner.getCount() > 0 && !TextUtils.isEmpty(searchGoodText)) {
FolderModel selectedFolder = (FolderModel) foldersSpinner.getSelectedItem();
String idFolder = selectedFolder.getId();
mGoodsList.setAdapter(null);
new Goods(mGoodsList, this).execute(idFolder, searchGoodText);
}
});
// Печать ценника
print.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// КАК ПОЛУЧИТЬ ПОЗИЦИЮ ИЗ RecyclerView??
}
});
}
}
Вопрос. Как получить номер позиции из RecyclerView в Activity?