RecyclerViewで、アダプタからではなくアクティビティからonClickの処理を行う
情報のリストを表示する際にRecyclerViewを使いました。
リスト表示の後、消去ボタンを押すと外部ストレージのデータを消去し、リストのデータを更新して表示するようにしたいのですが、
このときMainActivityで消去ボタンの処理を行なわなければいけません。
どのように行えば良いのでしょうか?
MainActivity.java
public class MainActivity extends AppCompatActivity {
Activity thisActivity = this;
List<HashMap<String, String>> worldDatas = new ArrayList<>();
String sdPath = getExternalStoragePath();
final String WORLD_NAME_KEY = "name";
final String WORLD_PATH_KEY = "path";
final String WORLD_SIZE_KEY = "size";
RecyclerView worldInfoContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
updateWorldsList();
}
//端末のフォルダからリストのデータを更新
private void updateWorldsList() {
worldInfoContainer = (RecyclerView) findViewById (R.id.worldInfoContainer);
worldDatas = new ArrayList<>();
//...
worldInfoContainer.setLayoutManager(new LinearLayoutManager(thisActivity));
worldInfoContainer.setHasFixedSize(true);
worldInfoContainer.setAdapter(new WorldDataAdapter(thisActivity, worldDatas));
}
//ディレクトリを消去
public void deleteDirectory(File file) {
//...
}
public static String getExternalStoragePath() {
//...
}
}
WorldDataAdapter.java
public class WorldDataAdapter extends RecyclerView.Adapter<WorldDataAdapter.ViewHolder> {
private LayoutInflater mLayoutInflater;
List<HashMap<String, String>> worldDatas = new ArrayList<>();
final String WORLD_NAME_KEY = "name";
final String WORLD_PATH_KEY = "path";
final String WORLD_SIZE_KEY = "size";
public WorldDataAdapter(Context context, List<HashMap<String, String>> dataList) {
super();
mLayoutInflater = LayoutInflater.from(context);
worldDatas = dataList;
}
@Override
public WorldDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = mLayoutInflater.inflate(R.layout.template_world_info, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public int getItemCount() {
return worldDatas.size();
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final HashMap<String, String> positionData = worldDatas.get(position);
holder.worldNameText.setText(positionData.get(WORLD_NAME_KEY));
holder.worldPathText.setText(positionData.get(WORLD_PATH_KEY));
holder.worldSizeText.setText(positionData.get(WORLD_SIZE_KEY));
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//MainActivityで実行したい
}
});
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView worldNameText;
TextView worldPathText;
TextView worldSizeText;
ImageButton deleteButton;
public ViewHolder(final View v) {
super(v);
worldNameText = (TextView) v.findViewById(R.id.worldNameText);
worldPathText = (TextView) v.findViewById(R.id.worldPathText);
worldSizeText = (TextView) v.findViewById(R.id.worldSizeText);
deleteButton = (ImageButton) v.findViewById(R.id.deleteButton);
}
}
}