Я пытаюсь добавить новый вид карты/новую карту thingy (извините, что я новичок) в моем приложении. прямо сейчас, все работает отлично, но как бы добавить больше? Я также хотел бы знать, как это сделать по щелчку FAB. Вот мой кодКак создать новый CardView?
public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.ViewHolder>{
private List<WeatherData> weatherDataList;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//?
View view1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_weather_card,parent,false);
View view2 = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_weather_card,parent,false);
return new ViewHolder(view2);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
WeatherData weatherData = this.weatherDataList.get(position);
holder.locationTextView.setText(weatherData.getName());
holder.tempTextView.setText("Temperature: "+weatherData.getWeatherDetails().getTemperature());
holder.humidTextView.setText("Humidity: "+ weatherData.getWeatherDetails().getHumidity());
}
@Override
public int getItemCount() {
return this.weatherDataList.size();
}
protected static class ViewHolder extends RecyclerView.ViewHolder{
public TextView locationTextView;
public TextView tempTextView;
public TextView humidTextView;
public ViewHolder(View v){
super(v);
locationTextView = (TextView) v.findViewById(R.id.locationTextView);
tempTextView = (TextView) v.findViewById(R.id.tempTextView);
humidTextView= (TextView) v.findViewById(R.id.humidText);
}
}
public WeatherListAdapter(List<WeatherData> weatherDataList){
this.weatherDataList = weatherDataList;
}
}
Если мне нужно добавить больше, чтобы помочь вам разобраться, просто оставьте комментарий. Благодаря! WeatherListActivity-
private RecyclerView recyclerView;
List<WeatherData> mWeatherDataList;
WeatherListAdapter mAdpapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mWeatherDataList = new ArrayList<WeatherData>();
final WeatherApiManager weatherApiManager = new WeatherApiManager();
weatherApiManager.getWeatherByCoordinate(35.744644099999995, -78.86395929999999, new WeatherCallback() {
@Override
public void weatherDownloadCompleted(boolean success, WeatherData weatherData) {
List<WeatherData> weatherDataList = new ArrayList<WeatherData>();
// weatherDataList.add(weatherData);
// weatherDataList.add(weatherData);
mAdpapter = new WeatherListAdapter(weatherDataList);
//why does this have to be in here?
recyclerView.setAdapter(mAdpapter);
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final WeatherApiManager weatherApiManager = new WeatherApiManager();
weatherApiManager.getWeatherByCoordinate(35.744644099999995, -78.86395929999999, new WeatherCallback() {
@Override
public void weatherDownloadCompleted(boolean success, WeatherData weatherData) {
List<WeatherData> weatherDataList = new ArrayList<WeatherData>();
weatherDataList.add(weatherData);
mAdpapter = new WeatherListAdapter(weatherDataList);
//why does this have to be in here?
recyclerView.setAdapter(mAdpapter);
}
});
updateUI();
}
});
}
Вы пытаетесь добавить только одну карту или динамическое число или карты? –
1 за раз, но я бы хотел, чтобы пользователь мог добавить его с помощью FAB –