I'm trying to load a list of objects inside a ListView, that is contained inside a Fragment. But I can't get it to work no matter what.
I've tried to load the same data and the same list directly inside the main activity and it works. But not in the fragment.
Here is my code:
MainActivity - The fragment loads correctly, data from the server is retrieved, everything is ok here.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
setContentView(R.layout.side_menu);
if (SharedPreferencesManager.isAuthenticated(getApplicationContext())) {
ServerComm.getInstance(getApplicationContext()).requestMenuItems(new BaseVolleyCallback() {
#Override
public void onMenuRequest(Menu result) {
leftSideMenu = (SideMenuFragment) getSupportFragmentManager().findFragmentById(R.id.left_side_menu);
leftSideMenu.menuItemList = result.top;
leftSideMenu.test();
}
});
}
}
Fragment: Again, everything looks to be fine
public class SideMenuFragment extends Fragment {
ArrayList<MenuItem> menuItemList;
View mView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.side_menu, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mView = view;
}
public void test(){
MenuItemAdapter adapter = new MenuItemAdapter(getContext(), menuItemList);
ListView listView = (ListView) mView.findViewById(R.id.listView);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final MenuItem item = (MenuItem) parent.getItemAtPosition(position);
MainActivity activity = (MainActivity)getActivity();
activity.loadContent(item);
}
});
}
}
Adapter: - Constructor called with the list of items, getCount() is called with the correct number of items. getView - NOT CALLED.
class MenuItemAdapter extends ArrayAdapter<MenuItem> {
private Context mContext;
private List<MenuItem> menuList = new ArrayList<>();
public MenuItemAdapter(#NonNull Context context, ArrayList<MenuItem> list) {
super(context, 0 , list);
mContext = context;
menuList = list;
}
#Override
public int getCount () {
return menuList.size();
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.menu_cell, parent, false);
}
MenuItem item = menuList.get(position);
TextView tvName = (TextView) convertView.findViewById(R.id.itemName);
tvName.setText(item.name);
return convertView;
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment android:name="com.example.dbl.SideMenuFragment"
android:id="#+id/left_side_menu"
android:layout_width="0dp"
android:layout_height="match_parent"
android:elevation="100dp"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent=".7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
side_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorAccent"
android:cacheColorHint="#android:color/transparent"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:paddingLeft="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
menu_cell.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/itemName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Test"
android:textAlignment="center"
android:textColor="#666"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Honestly, I don't know what I could try next.
Related
Probably I am missing something here, but I am animating a recyclerview to outside of its container and when it is in the final position, the click events are not triggered, neither the scroll works...
I am using the ObjectAnimator to translate it, so I thought the clickable areas were also translated. Here is the relevant code:
The Fragment
public class SlideListFragment extends Fragment {
private Button slideButton;
private RecyclerView listToSlide;
private DummyListAdapter listAdapter;
private LinearLayout listContainer;
public static SlideListFragment newInstance() {
return new SlideListFragment();
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.slide_list_fragment, container, false);
slideButton = root.findViewById(R.id.slide_up_btn);
slideButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
slideUpRoomsList();
}
});
listToSlide = root.findViewById(R.id.list_to_slide);
listContainer = root.findViewById(R.id.list_container);
setupList();
return root;
}
private void setupList() {
GridLayoutManager layoutManager = new GridLayoutManager(this.getContext(), 1);
layoutManager.setReverseLayout(true);
listToSlide.setLayoutManager(layoutManager);
listAdapter = new DummyListAdapter(this.getContext());
listToSlide.setAdapter(listAdapter);
setListData(5);
}
private void setListData(int i) {
ArrayList<DummyModel> items = new ArrayList<>();
for (int j = 0; j < i; j++) {
items.add(new DummyModel(j, "TEXT" + j));
}
listAdapter.refreshItems(items);
}
private void slideUpRoomsList() {
float height = listContainer.getHeight();
ObjectAnimator showAnimation = ObjectAnimator.ofFloat(listContainer, "translationY", -height);
showAnimation.setDuration(500);
showAnimation.start();
}
}
The Fragment Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/slidelist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
tools:context=".ui.slidelist.SlideListFragment">
<android.support.constraint.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.8" />
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#FF0000"
android:clipChildren="false"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/guideline">
<LinearLayout
android:id="#+id/list_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/list_to_slide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
<Button
android:id="#+id/slide_up_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="SlideUp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
The Adapter
public class DummyListAdapter extends RecyclerView.Adapter<DummyListAdapter.ViewHolder> {
private WeakReference<Context> mContext;
private ArrayList<DummyModel> data;
public DummyListAdapter(Context ctx) {
mContext = new WeakReference<>(ctx);
}
#NonNull
#Override
public DummyListAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.dummy_list_item, parent, false);
DummyListAdapter.ViewHolder vh = new DummyListAdapter.ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(#NonNull DummyListAdapter.ViewHolder holder, int position) {
// get element from your dataset at this position
final DummyModel item = data.get(position);
// replace the contents of the view with that element
holder.labelTV.setText(item.getText());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext.get(), "Item Clicked: "+ item.getText(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return data != null ? data.size() : 0;
}
public void refreshItems(ArrayList<DummyModel> items) {
this.data = items;
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView labelTV;
public ViewHolder(View itemView) {
super(itemView);
this.labelTV = itemView.findViewById(R.id.label_tv);
}
}
}
The List Item Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/label_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
android:textSize="20sp"
android:textColor="#0000FF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
What is happening in this case?
UPDATE
I am doing some debug and I noticed that the problem is because the end position is outside the list container. If I translate inside the container, it works properly. So my new question is: how can I translate a view outside of the container, while still having the click events triggered?
Basically, I just gave up on this approach. I changed my layout so it list has space to show up inside its container.
I created an Alert Dialog with a ListView in it, the ListView contains an EditText, the problem is when i click on the EditText, the keyboard doesn't show up. plz help me. here is my code:
MainActivity:
public class MainActivity extends AppCompatActivity {
View con_view;
ListView listView;
AlertDialog.Builder con_mBuilder;
MycustomAdapter mycustomAdapter;
ArrayList<ListItem> Item;
AlertDialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void show_dialog(View view){
con_view = getLayoutInflater().inflate(R.layout.dialog_layout, null);
listView = con_view.findViewById(R.id.listtvv);
Item = new ArrayList<ListItem>();
Item.add(new ListItem("", "", ""));
mycustomAdapter = new MycustomAdapter(Item);
listView.setAdapter(mycustomAdapter);
con_mBuilder = new AlertDialog.Builder(MainActivity.this);
con_mBuilder.setView(con_view);
dialog = con_mBuilder.create();
dialog.show();
}
class MycustomAdapter extends BaseAdapter {
ArrayList<ListItem> Item = new ArrayList<>();
public MycustomAdapter(ArrayList Item) {
this.Item = Item;
}
#Override
public int getCount() {
return Item.size();
}
#Override
public String getItem(int i) {
return Item.get(i).name;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater linflater = getLayoutInflater();
View view1 = linflater.inflate(R.layout.row, null);
return view1;
}
}
}
and here is the Alert Dialog xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray">
<ListView
android:id="#+id/listtvv"
android:layout_width="230dp"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
and finally here's the xml file for the view that contains the EditText
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/row_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_dark"
tools:layout_editor_absoluteY="81dp"
>
<EditText
android:id="#+id/bomb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPersonName"
android:selectAllOnFocus="true"
android:text="Name"
android:textColor="#android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
strings.xml
<string-array name="meal_array">
<item>Breakfast</item>
<item>Lunch</item>
<item>Dinner</item>
</string-array>
dietary.xml
<Spinner
android:id="#+id/ddl_meal_type"
android:layout_width="140dp"
android:layout_height="25dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:entries="#array/meal_array"
app:layout_constraintBottom_toBottomOf="#+id/lbl_meal_type"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.016"
app:layout_constraintStart_toEndOf="#+id/lbl_meal_type"
app:layout_constraintTop_toTopOf="#+id/lbl_meal_type"
app:layout_constraintVertical_bias="1.0"
tools:layout_conversion_absoluteHeight="24dp"
tools:layout_conversion_absoluteWidth="140dp" />
meal_layout.xml
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_vertical"
android:src="#drawable/ic_action_food"
/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_vertical"
/>
Dietary.java
public class Dietary extends Fragment{
View myView; //A view object called myView
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.dietary, container, false);
Spinner mySpinner = (Spinner)myView.findViewById(R.id.ddl_meal_type);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.meal_layout, R.id.txt, R.array.meal_array);
mySpinner.setAdapter(adapter);
return myView;
}
}
I want to add images beside the text in the spinner. The code in Dietary.java does not seems to work. I am doing it using fragment.
Can anybody help me with this?
Your Dietary.java
public class Dietary extends Fragment{
View myView; //A view object called myView
int img[] = {0,R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round};
Spinner mySpinner;
SpinnerAdapter spinnerAdapter;
String[] mealArry = {"Select Meal","Breakfast", "Lunch", "Dinner"};
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.dietary, container, false);
mySpinner = (Spinner)myView.findViewById(R.id.ddl_meal_type);
spinnerAdapter = new SpinnerAdapter(mealArry, img, Main2Activity.this);
spinner.setAdapter(spinnerAdapter);
return myView;
}
}
Your meal_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".2"
android:padding="10dp" />
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".8"
android:padding="10dp" />
</LinearLayout>
Your dietary.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="#+id/ddl_meal_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp" />
</LinearLayout>
Now for Image with Text for spinner you need to make a custom adapter.
For this first create a new java file which will extends BaseAdapter, baseAdapter has 4 override methods.
1.)
#Override
public int getCount() {
}
2.)
#Override
public Object getItem(int i) {
}
3.)
#Override
public long getItemId(int i) {
}
4.)
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
}
So below is your SpinnerAdapter:
public class SpinnerAdapter extends BaseAdapter {
String[] mealPlan;
int[] mealPlanImage;
LayoutInflater layoutInflater;
Context context;
public SpinnerAdapter(String[] mealPlan, int[] mealPlanImage, Context context) {
this.mealPlan = mealPlan;
this.mealPlanImage = mealPlanImage;
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return mealPlan.length;
}
#Override
public Object getItem(int i) {
return mealPlan[i];
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = layoutInflater.inflate(R.layout.meal_layout, null);
ImageView img = (ImageView) view.findViewById(R.id.img);
TextView txt = (TextView) view.findViewById(R.id.txt);
img.setImageResource(mealPlanImage[i]);
txt.setText(mealPlan[i]);
return view;
}
}
and finally your view will be like below image:
I hope you got your expected result.
I have a problem with spinner it do not let me select one item. I tried a lot of things and that still not working.
The picture shows that the spinner is in blank when the activity load
When I clicked the arrow it shows the items
but when I choose one, nothing happends.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.Inspeccion.DatosGeneralesActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
>
<TextView
android:id="#+id/tvSubestacionTitulo"
android:layout_below="#+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/strSubestacion"
android:textSize="18sp"
android:textColor="#color/colorPrimaryDark"
android:textStyle="bold"
/>
<TextView
android:id="#+id/tvSubestacionDato"
android:layout_below="#+id/tvSubestacionTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Prueba"
/>
<Spinner
android:id="#+id/spinnerSubEstacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvSubestacionDato"
>
</Spinner>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
This is the Layout of the activity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvNumeroOpcion"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:text="1"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/tvDescriptionOption"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:text="Guatemala"
android:textColor="#color/black"
android:textSize="14sp" />
</LinearLayout>
That is the custom layout for the spinner
Public class ComboAdapter extends BaseAdapter{
private List<Combo> combos;
private Activity activity;
private LayoutInflater inflater;
public ComboAdapter(List<Combo> combos, Activity activity) {
this.combos = combos;
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return combos.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null){
view = inflater.inflate(R.layout.combo_list_item, null);
TextView tvId = (TextView) view.findViewById(R.id.tvNumeroOpcion);
TextView tvDescripcion = (TextView) view.findViewById(R.id.tvDescriptionOption);
tvId.setText(combos.get(position).getId());
tvDescripcion.setText(combos.get(position).getDescripcion());
}
return view;
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getView(position, convertView,parent);
}
}
That is my Adapter
And below is my activity.
public class DatosGeneralesActivity extends AppCompatActivity {
private TextView tvSubestacionDato;
private List<Combo> listaCombo;
private Spinner spinnerSubestacion;
private ArrayAdapter<Combo> adapterSubestacion;
String seleccion;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datos_generales);
//Inicializando textos
tvSubestacionDato = (TextView) findViewById(R.id.tvSubestacionDato);
//Inicializanco listas
listaCombo = new ArrayList<>();
//Inivializando spinners
spinnerSubestacion = (Spinner) findViewById(R.id.spinnerSubEstacion);
AppService service = API.getCombos().create(AppService.class);
Call<List<Combo>> subestacionCall = service.getSubestacion();
subestacionCall.enqueue(new Callback<List<Combo>>() {
#Override
public void onResponse(Call<List<Combo>> call, Response<List<Combo>> response) {
listaCombo.clear();
listaCombo.addAll(response.body());
}
#Override
public void onFailure(Call<List<Combo>> call, Throwable t) {
}
});
//final ComboAdapter adapter = new ComboAdapter(listaCombo, DatosGeneralesActivity.this);
final ArrayAdapter<Combo> adapter = new ArrayAdapter<Combo>(this, R.layout.support_simple_spinner_dropdown_item, listaCombo);
spinnerSubestacion.setAdapter(adapter);
spinnerSubestacion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
adapter.notifyDataSetChanged();
Toast.makeText(DatosGeneralesActivity.this, ""+position, Toast.LENGTH_SHORT).show();
tvSubestacionDato.setText(listaCombo.get(position).getDescripcion());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Try this changes:
Call adapter like:
ComboAdapter adapter = new ComboAdapter(DatosGeneralesActivity.this,
R.layout.combo_list_item, R.id.tvDescriptionOption, listaCombo);
now in adapter class:
public ComboAdapter(Activity context,int resouceId, int textviewId, List<Combo> list){
super(context,resouceId,textviewId, list);
this.combos = list;
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Also inside your getView() method inflate layout like:
if (convertView == null){
view = inflater.inflate(R.layout.combo_list_item, parent , false);
My application has a couple tabs of fragments with a viewpager and it does not start on top and I do not know why. Even when I scroll the fragment to the top, refreshing(going back and forth tabs) resets the fragment's start position right above the recyclerview.
fragment2.xml
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:isScrollContainer="false"
android:layout_gravity="fill_vertical"
android:clipToPadding="false"
android:fillViewport="true"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="79dp"
android:scaleType="fitXY"
android:src="#drawable/sub4_visual"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="79dp"
android:alpha="0.3"
android:background="#000000"/>
</RelativeLayout>
<TextView
android:id="#+id/frag2_desc"
android:layout_margin="11dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#color/color_333333"
android:text=""/>
<Spinner
android:id="#+id/frag2_dropbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="ifContentScrolls"
android:scrollbars="none"
android:gravity="left"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_subfrag2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Fragment2.java
public class Fragment2 extends Fragment{
...
public static Frag_BoardContent[] fragsList;
public static RecyclerView mRecyclerView;
public static Frag234_Adapter4 board_adapter;
public Fragment2() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View v = inflater.inflate(R.layout.fragment2, container, false);
tv_frag2Desc = (TextView) v.findViewById(R.id.frag2_desc);
spinner = (Spinner) v.findViewById(R.id.frag2_dropbox);
ArrayList<String> categoryList = new ArrayList<>();
if (connect) makeDynamicArray(categoryList);
adapter = new ArrayAdapter<> (getActivity(), R.layout.activity_board_spinner,categoryList);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(final AdapterView<?> parent, final View view, int position, long id) {
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.context));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setNestedScrollingEnabled(false);
board_adapter = new Frag234_Adapter4(MainActivity.context, fragsList);
mRecyclerView.setAdapter(board_adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_subfrag2);
mRecyclerView.setAdapter(board_adapter);
return v;
}
public ArrayList<String> makeDynamicArray(){...}
}
It is a problem about focus. you need to do requestFocus() to your scrollview and
setFocusable(false)
to fragment;